【问题标题】:Ignore url values with Outputcache使用 Outputcache 忽略 url 值
【发布时间】:2011-04-14 08:11:19
【问题描述】:

当我在动作方法上使用输出缓存时,它被完整的 url 缓存,我想做的是缓存页面但忽略 url 的某些部分。

Global.asax 中定义的自定义路由:

routes.MapRoute(
  "Template",
  "Report/{reportid}/{reportname}/Template/{templateid}/{action}",
  new { controller = "Template", action = "Index" }
);

我的模板控制器

public class TemplateController : Controller
{
   [OutputCache(Duration=60*60*2)]
   public ActionResult Index(Template template)
   {
      /* some code */
   }
}

例如,当我访问以下网址时:

http://mywebsite.com/Report/789/cacheme/Template/5
-> 根据 url 缓存 2 小时

http://mywebsite.com/Report/777/anothercacheme/Template/5
-> 还会根据该 url 缓存 2 小时

我想要的是OutputCache 忽略reportname 和reportid 值,所以当我转到上面的网址时,它会返回相同的缓存版本。这可以通过 OutputCache 属性实现吗?还是我必须编写自定义的 OutputCache FilterAttribute?

【问题讨论】:

  • 也许,其中一种方法是将 reportname、reportid 作为您的方法的参数,然后仅将 VaryByParam 用于模板 ID。除了那个自定义过滤器属性将是要走的路!
  • 我试过了,但它仍然为请求的模板返回不同的缓存版本

标签: asp.net-mvc asp.net-mvc-2 outputcache


【解决方案1】:

最终得到以下结果(灵感来自 http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/):

 public class ResultCacheAttribute : ActionFilterAttribute
    {
        public ResultCacheAttribute()
        {

        }

        public string CacheKey
        {
            get;
            private set;
        }

        public bool AddUserCacheKey { get; set; }
        public bool IgnoreReport { get; set; }

        /// <summary>
        /// Duration in seconds of the cached values before expiring.
        /// </summary>
        public int Duration
        {
            get;
            set;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string url = "";
            foreach (var item in filterContext.RouteData.Values)
            {
                if (IgnoreReport)
                    if (item.Key == "reportid" || item.Key == "reportname")
                        continue;

                url += "." + item.Value;
            }
            if (AddUserCacheKey)
                url += "." + filterContext.HttpContext.User.Identity.Name;

            this.CacheKey = "ResultCache-" + url;

            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                this.CacheKey += "-ajax";

            if (filterContext.HttpContext.Cache[this.CacheKey] != null)
            {
                filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.CacheKey];
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewData["CachedStamp"] = DateTime.Now;
            filterContext.HttpContext.Cache.Add(this.CacheKey, filterContext.Result, null, DateTime.Now.AddSeconds(Duration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);

            base.OnActionExecuted(filterContext);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2015-11-21
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多