【问题标题】:Using Response.RemoveOutputCacheItem with RedisOutputCacheProvider将 Response.RemoveOutputCacheItem 与 RedisOutputCacheProvider 一起使用
【发布时间】:2014-11-12 22:29:01
【问题描述】:

我正在使用 Microsoft RedisOutputCacheProvider 并有一个非常简单的 PartialView,我通过 VaryByCustom 根据当前用户的 SessionId 缓存它:

[OutputCache(VaryByCustom = "User", Duration = 3600)]
[ChildActionOnly]
public ActionResult Notifications()
{
    return PartialView("Partials/Notifications");
}

这很好用并且可以按预期进行缓存,但是我想从另一个页面手动使此 OutputCache 过期。我试过了:

Response.RemoveOutputCacheItem("/Controller/Notifications");

但这似乎不起作用。我也无法通过我的 Redis 存储或后端代码看到任何 OutputCache 键,但我绝对可以看到正在缓存的视图。

【问题讨论】:

  • @MarcGravell 谢谢!这在我的搜索中没有出现,因为我认为它与特定的 OutputCacheProvider 有关。明天我会试一试,不是 ChildOnly 行动对我来说并不是那么重要。

标签: c# caching redis outputcache stackexchange.redis


【解决方案1】:

你尝试过这样的事情吗?

//  Get the url for the action method:
var staleItem = Url.Action("Action", "Controller");

//  Remove the item from cache
Response.RemoveOutputCacheItem(staleItem);

我认为您需要保留对 ActionResult 的引用。

祝你好运:)

PS:也许这个链接会对你有所帮助:The Blog of Dan Esparza

【讨论】:

    【解决方案2】:

    如果您正在执行自定义缓存清除逻辑,您可能会发现这也很有用:

        private void ClearResponseCache(ActionExecutingContext filterContext)
        {
            if (filterContext == null)
                return;
    
            var urlHelper = new UrlHelper(filterContext.RequestContext);
            var resolvedAction = urlHelper.Action(
                filterContext.ActionDescriptor.ActionName, 
                filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                new RouteValueDictionary(filterContext.ActionParameters));
    
            if (resolvedAction != null)
                filterContext.HttpContext.Response.RemoveOutputCacheItem(resolvedAction);
        }
    

    【讨论】:

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