【问题标题】:ASP MVC 3 OutputCache avoid cacheing current requestASP MVC 3 OutputCache 避免缓存当前请求
【发布时间】:2012-02-07 15:57:20
【问题描述】:

我开始为我的网站使用 OutputCache。 我遇到的问题是,当用户更新一个项目时,我需要重置该项目的缓存。

我是这样做的:

var urlToRemove = Url.Action("Details", "Dress", new {id = model.Id});
Response.RemoveOutputCacheItem(urlToRemove);

在编辑操作中,我还将更新成功消息设置为 TempData,并在下一个请求中显示它。问题是消息保留在缓存的响应中。

您知道如何避免在操作中进行缓存吗?比如:

[OutputCache(Duration = 3600, VaryByParam = "id")]
public ViewResult Details(int id)
{  
  if(NotificationHelper.HasNotifications)
    Response.DoNotCache();
    .....

我不能使用相同的技巧......因为页面在呈现后被添加到缓存中。所以我不能从缓存中排除一个动作。

【问题讨论】:

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


    【解决方案1】:

    您所描述的内容有时被称为“甜甜圈孔缓存”,因为您想要缓存除了中间的一些动态内容之外的所有内容。

    这里有一些您可能想要查看的资源:

    【讨论】:

      【解决方案2】:

      我不知道是否有一种简单的方法可以满足您的要求。但是,我对消息和 OutputCache 的经验是不要将它们放在响应中。我最终确保缓存页面上没有显示任何消息。如果我绝对必须在缓存页面上有一条消息,我会设置一个 ajax 调用,一旦响应到达客户端就会抓取消息。

      【讨论】:

      • Erv 建议的甜甜圈隐藏消息也是一个好主意,如果您能够集成第三方缓存库。
      【解决方案3】:

      这个呢?

      class CustomOutputCacheAttribute : OutputCacheAttribute
      {
          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
              if (filterContext.HttpContext.Response.StatusCode >= 400)
              {
                  this.Location = System.Web.UI.OutputCacheLocation.None;
                  this.Duration = -1;
              }
      
              filterContext.HttpContext.Response.Cache.SetOmitVaryStar(true);
              base.OnActionExecuted(filterContext);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 2014-12-01
        • 1970-01-01
        • 2012-05-01
        • 2014-03-25
        • 2014-12-30
        • 1970-01-01
        相关资源
        最近更新 更多