如何在输出缓存中使用动态持续时间值? [英] How to use dynamic duration value in Output Caching?

查看:119
本文介绍了如何在输出缓存中使用动态持续时间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using ASP.NET MVC3.
I've used Output Caching on controller method.

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

我想在输出缓存中使用一些静态变量或其他内容来设置动态持续时间.

I want to put dynamic duration using some static variable or something else in Output Caching.

我该怎么做?

推荐答案

我将从OutputCache属性继承并在其中设置Duration:

I would inherit from the OutputCache attribute and set there the 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

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

And you can still override the global setting on every action if you want:

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

这篇关于如何在输出缓存中使用动态持续时间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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