5

I am using the Microsoft RedisOutputCacheProvider and have a very simple PartialView which I am caching based on the current user's SessionId via VaryByCustom:

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

This works great and caches as expected, however I wanted to manually expire this OutputCache from another page. I tried:

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

But that doesn't seem to work. I also can't see any of the OutputCache keys via either my Redis store, or via my backend code, but I can definitely see the view being cached.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 2
    Have you looked at: http://stackoverflow.com/questions/8968508/httpresponse-removeoutputcacheitem-is-not-working ? – Marc Gravell Sep 22 '14 at 19:26
  • @MarcGravell Thank you! That hadn't turned up in my search as I thought it was related to the specific OutputCacheProvider. I'll give this a try tomorrow, not being a ChildOnly action isn't really that important for me. – CodingIntrigue Sep 22 '14 at 20:27

2 Answers2

5

Have you try something like this?

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

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

I think that you need to hold a reference to your ActionResult.

Best of luck :)

PS: Maybe this link will help you : The Blog of Dan Esparza

Nick De Beer
  • 5,232
  • 6
  • 35
  • 50
Philippe Matray
  • 1,511
  • 1
  • 13
  • 22
0

If you're doing custom cache clear logic you may find this useful too:

    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);
    }
Dbl
  • 5,634
  • 3
  • 41
  • 66