繁体   English   中英

自定义输出缓存mvc4

[英]Custom output cache mvc4

我正在尝试在MVC4项目上利用缓存,并且我在主页上设置了以下属性:

[OutputCache(Location=OutputCacheLocation.ServerAndClient,Duration=14400)]

效果很好,但是持续时间给我带来了问题。 我需要的是缓存在新的一天(每天午夜)开始时过期。 我可以将持续时间设置为24小时,但是这不能解决我的问题,因为每天的开始我的页面都有新内容。 我已经研究了variant by param方法,并且我知道可以将日期附加到URL,但这很混乱。 有人知道替代品吗?

提前致谢

一种解决方案是扩展OutputCacheAttribute并创建一个适用于午夜的新方法,因为您可以在构造函数中设置Duration

public class OutputCacheMidnightAttribute : OutputCacheAttribute
{
    public OutputCacheMidnightAttribute()
    {
        // remaining time to midnight
        Duration = (int)((new TimeSpan(24, 0, 0)) - DateTime.Now.TimeOfDay).TotalSeconds;
    }
}

你这样使用它

[OutputCacheMidnight(Location=OutputCacheLocation.ServerAndClient)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM