我使用的是Microsoft RedisOutputCacheProvider,并且有一个非常简单的PartialView,它通过以下方式基于当前用户的SessionId进行缓存VaryByCustom
:
[OutputCache(VaryByCustom = "User", Duration = 3600)] [ChildActionOnly] public ActionResult Notifications() { return PartialView("Partials/Notifications"); }
这可以很好地工作并按预期进行缓存,但是我想从另一个页面手动使此OutputCache过期。我试过了:
Response.RemoveOutputCacheItem("/Controller/Notifications");
但这似乎不起作用。我也无法通过Redis存储或后端代码看到任何OutputCache键,但是我绝对可以看到正在缓存的视图。
你尝试过这样的事情吗?
// Get the url for the action method: var staleItem = Url.Action("Action", "Controller"); // Remove the item from cache Response.RemoveOutputCacheItem(staleItem);
我认为您需要保留对ActionResult的引用。
祝你好运:)
PS:也许此链接对您有帮助:Dan Esparza的博客