Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 576
AntonM Avatar asked Feb 01 '26 21:02

AntonM


1 Answers

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);
like image 185
AntonM Avatar answered Feb 04 '26 11:02

AntonM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!