【问题标题】:Multiple OutputCache in MVCMVC中的多个OutputCache
【发布时间】:2016-03-19 17:37:30
【问题描述】:

我们可以为一个动作定义多个OutputCache 吗?例如。假设我想存储OutputCache 一个复制到Server 和另一个在Client。我知道我们可以用Location=OutputCacheLocation.ServerAndClient 来做到这一点,但我想为ClientServer 指定不同的Duration,为Server 指定更大的Duration,为Client 指定更小的Duration?那么有了这个要求,我可以有如下吗?

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Server, VaryByParam = "pID", NoStore = true)] //Server Cache for 1 hour
[OutputCache(Duration = 600, Location = OutputCacheLocation.Client, VaryByParam = "pID", NoStore = true)] //Client Cache for 10 minutes
public ActionResult GetDetails(string pID)
{
    //some code
    return View(model);
}

这是否有效或MVC 会考虑最新的OutputCache

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 outputcache


    【解决方案1】:

    如果您查看OutputCacheAttribute 的来源,您会注意到该属性是在将AllowMultiple 属性设置为false 的情况下定义的:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class OutputCacheAttribute : ActionFilterAttribute, IExceptionFilter
    

    这样就不行了

    【讨论】:

    • 我们可以覆盖这个吗?
    • 另外,我的要求或想法有什么替代方案吗?
    • 您不能通过简单地扩展默认的 OutputCacheAttribute 来覆盖此行为。这个线程可能很有用:stackoverflow.com/questions/14823057/…
    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    相关资源
    最近更新 更多