【问题标题】:How to use dynamic duration value in Output Caching?如何在输出缓存中使用动态持续时间值?
【发布时间】:2012-05-04 00:12:55
【问题描述】:

我正在使用 ASP.NET MVC3。
我在控制器方法上使用了输出缓存。

   [OutputCache(Duration = 3660, VaryByParam = "none")]
   public ActionResult Index()
   {
       some code;
       return View();
   }

我想在输出缓存中使用一些静态变量或其他东西来放置动态持续时间。

我该怎么做?

【问题讨论】:

    标签: asp.net-mvc-3 output-caching


    【解决方案1】:

    我将从OutputCache 属性继承并在那里设置Duration

    public static class CacheConfig
    {
        public static int Duration = 36600;
    }
    
    public class MyOutputCacheAttribute : OutputCacheAttribute
    {
        public MyOutputCacheAttribute()
        {
            this.Duration = CacheConfig.Duration;
        }
    }
    
    [MyOutputCache(VaryByParam = "none")]
    public ActionResult Index()
    {
        return View();
    }
    

    然后您可以通过CacheConfig.Duration 动态全局更改Duration

    如果需要,您仍然可以覆盖每个操作的全局设置:

    [MyOutputCache(Duration = 100, VaryByParam = "none")]
    public ActionResult OtherAction()
    {
        return View();
    }
    

    【讨论】:

    • 不起作用,属性仅实例化一次,在 runtime 更改 CacheConfig.Duration 不会神奇地重新执行您的构造函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多