AntonM
AntonM

Reputation: 216

How to invalidate cache using ASP.NET MVC 4.0 DonutOutputCache VaryByCustom

I'm using DevTrends.MvcDonutCaching package for my ASP.NET application and it works great. One problem that I have at the moment is with invalidating VaryByCustom cache I set up for a child action.

That's some code that I have for VaryByCustom setup:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
  if (arg == "userlogin" && context.User.Identity.IsAuthenticated)
  {
     return "UserLogin=" + context.User.Identity.Name;
  }

  return base.GetVaryByCustomString(context, arg);
}

That's how my action is decorated:

[Authorize]
[DonutOutputCache(Duration = 3600, VaryByCustom = "userlogin")]
public ActionResult UserProfile()
{ ... }

And that's how I tried to clean up that cache (I also tried it without any params and with 'userlogin' but none of these worked:

OutputCacheManager om = new OutputCacheManager();
om.RemoveItem("Customer", "UserProfile", new { UserLogin = User.Identity.Name });

That is the razor view part:

<div id="cabinetMain">
 @{Html.RenderAction("UserProfile", true);}
</div>

Any help will be much appreciated.

Thanks.

Upvotes: 3

Views: 1539

Answers (1)

AntonM
AntonM

Reputation: 216

Solution that worked for me is using OutputCacheManager's RemoveItems method instead of RemoveItem. It is a bit more tricky because requires using RouteValueDictionary. Example that worked for me below:

OutputCacheManager om = new OutputCacheManager();
RouteValueDictionary rv = new RouteValueDictionary();                    
rv.Add("userlogin", User.Identity.Name);
om.RemoveItems("Customer", "UserProfile", rv);

Upvotes: 5

Related Questions