与输出缓存和其他措施筛选工作 [英] Working with the Output Cache and other Action Filters

查看:139
本文介绍了与输出缓存和其他措施筛选工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经添加输出缓存一对夫妇的行动,我的应用程序进行一些简单的性能提升。不过,这些行动也需要通过敲击的Redis数据库的每个请求后递增计数器(这是一个观点计数器)。

I have added Output Caching to a couple of actions in my app for some easy performance boosts. However, these actions also need to increment a counter after each request (it's a views counter) by hitting a Redis db.

起初,我想我可能只是调整中的操作筛选器执行,以确保查看订单数:

At first, I figured I could just adjust the order in which the action filters execute to ensure the view is counted:

public class CountersAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //increment my counter all clever like
        base.OnResultExecuted(filterContext);
    }
}

但没有工作;显然OutputCacheAttribute并不像一个正常的行为过滤器。然后我试图实现自定义输出缓存:

But that didn't work; apparently the OutputCacheAttribute doesn't behave like a normal action filter. Then I tried implementing a custom output cache:

public class OutputCacheWithCountersAttribute : OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        //straight to the source to get my headcount!
        base.OnResultExecuted(filterContext);
    }
}

不,没有工作,要么;行动过滤器出现一次的动作被缓存被完全忽略。令人失望的。

Nope, didn't work either; action filters appear to be entirely ignored once an action is cached. Bummer.

所以,呃,有没有什么方法(不实现自定义输出缓存提供者)对我来说,确保我的意见是否正确计算的是干净的,合理的?

So, uh, is there any way (without implementing a custom output caching provider) for me to ensure my views are counted properly that is clean and sensible?

推荐答案

OutputCacheAttribute 的方式有局限性,有一个名为自定义属性的 DonutOutputCache 的开发由保罗·海尔斯有助于克服的局限性

The OutputCacheAttribute has limitations by the way and there is a custom attribute named DonutOutputCache developed by Paul Hiles helps to overcome the limitations.

一个支持的重要特点之一是你可以有一个动作的过滤器,可以被称为所有的时间,即使在行动上都标有缓存的属性或没有。

One of the important feature it supports is you can have an action filter that can be called all the times even the action is marked with cache attribute or not.

有关前。要缓存的持续时间5秒,在要记录每一个动作接收使用请求的时间,同时一个动作一个 LogThis 过滤器就可以实现,仅仅通过下面,

For ex. you want to cache an action for the duration 5 seconds and at the same time you want to log every time the action receives a request using a LogThis filter you can achieve that simply by below,

[LogThis]
[DonutOutputCache(Duration=5, Order=100)]
public ActionResult Index()

是的,不像内置OutputCacheAttribute,动作过滤器将
  即使执行页面时从缓存中检索。唯一需要注意的
  补充的是,你确实需要小心滤波器的阶数。如果
  你的行动过滤器实现OnResultExecuting或OnResultExecuted
  然后这些方法将在所有情况下被执行,但对
  OnActionExecuting和OnActionExecuted,它们将只被执行
  过滤器的DonutOutputCacheAttribute之前运行。这是因为
  的方式,MVC prevents随后从执行你的时候过滤器
  设置filterContext.Result属性这是我们需要做的
  输出缓存。

Yes, unlike the built-in OutputCacheAttribute, the action filters will execute even when a page is retrieved from the cache. The only caveat to add is that you do need to be careful about the filter order. If your action filter implements OnResultExecuting or OnResultExecuted then these methods will be executed in all cases, but for OnActionExecuting and OnActionExecuted, they will only be executed if the filter runs before the DonutOutputCacheAttribute. This is due to the way that MVC prevents subsequent filters from executing when you set the filterContext.Result property which is what we need to do for output caching.

我不认为你可以依靠的顺序在行动过滤器
  上的操作或控制器的定义。为了确保一个过滤器运行
  前一个,你可以利用Order属性是present的
  所有ActionFilterAttribute实现。没有任何行动
  订购属性设置,默认为-1的值,这意味着他们将
  执行前,它有明确的顺序值过滤器。

I do not think that you can rely on the order in which action filters are defined on an action or controller. To ensure that one filter runs before another, you can make use of the Order property that is present on all ActionFilterAttribute implementations. Any actions without the order property set, default to an value of -1, meaning that they will execute before filters which have an explicit Order value.

因此​​,在你的情况,你可以添加顺序= 100到
  DonutOutputCache属性和所有其他过滤器将前执行
  缓存过滤器。

Therefore, in your case, you can just add Order=100 to the DonutOutputCache attribute and all other filters will execute before the caching filter.

这篇关于与输出缓存和其他措施筛选工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆