OutputCache属性始终设置先前的持续时间

时间:2018-04-23 16:35:25

标签: c# asp.net .net caching outputcache

由于某种原因,输出缓存会保留我在未来事件中应用于过期事件的两周持续时间。我是否在自定义输出提供程序中遗​​漏了某些内容以停止保存期限?

[ExpiredOutputCache(Duration = 3600, VaryByParam = "eventid;expired")]
        public virtual ActionResult GetDivisionsViews(int eventId, EventDivisionsViewModel viewModel)
        {

}

    public class ExpiredOutputCacheAttribute : OutputCacheAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if((filterContext.HttpContext.Request.QueryString[Config.QueryString.Expired] != null && filterContext.HttpContext.Request.QueryString[Config.QueryString.Expired].To<DateTime>().Date < DateTime.Now.Date.Date) || 
                   (filterContext.RouteData.Values[Config.QueryString.Expired] != null && filterContext.RouteData.Values[Config.QueryString.Expired].To<DateTime>().Date < DateTime.Now.Date.Date))
                {
                    Duration = (int)Config.Cache.TwoWeeks * 60;
                }

                base.OnActionExecuting(filterContext);
            }
        }

1 个答案:

答案 0 :(得分:0)

我刚刚这样做了。

protected int OriginalDuration { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(OriginalDuration == 0)
            {
                OriginalDuration = Duration;
            }

            if ((filterContext.HttpContext.Request.QueryString[Config.QueryString.Expired] != null && filterContext.HttpContext.Request.QueryString[Config.QueryString.Expired].To<DateTime>().Date < DateTime.Now.Date.Date) || 
               (filterContext.RouteData.Values[Config.QueryString.Expired] != null && filterContext.RouteData.Values[Config.QueryString.Expired].To<DateTime>().Date < DateTime.Now.Date.Date))
            {
                Duration = (int)Config.Cache.TwoWeeks * 60;
            }
            else
            {
                Duration = OriginalDuration;
            }

            base.OnActionExecuting(filterContext);
        }