5

我正在使用 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 键,但我绝对可以看到正在缓存的视图。

4

2 回答 2

5

你有没有尝试过这样的事情?

//  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 的博客

于 2014-09-25T08:25:42.683 回答
0

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

    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-05T08:35:30.453 回答