How to programmatically clear output cache for controller action method

If the controller action has the OutputCache attribute specified in the action, is there a way to clear the output cache without restarting IIS?

[OutputCache (Duration=3600,VaryByParam="param1;param2")] public string AjaxHtmlOutputMethod(string param1, string param2) { var someModel = SomeModel.Find( param1, param2 ); //set up ViewData ... return RenderToString( "ViewName", someModel ); } 

I use HttpResponse.RemoveOutputCacheItem(string path) to clear it, but it's hard for me to figure out what the path should be in order to map it to the action method. I will try again to use the aspx page that appears in the ViewName view.

Perhaps I will just manually insert the output of the HttpContext.Cache into the HttpContext.Cache instead if I cannot do this.

Update

Note that OutputCache is VaryByParam, and testing the hard path "/ controller / action" does not actually clear the output cache, so it looks like it should match "/ controller / action / param1 / param2".

This means that I probably have to go back to object level caching and manually cache the output for RenderToString() : (

+60
asp.net-mvc
Jul 22 '09 at 20:04
source share
5 answers

Try this

 var urlToRemove = Url.Action("AjaxHtmlOutputMethod", "Controller"); HttpResponse.RemoveOutputCacheItem(urlToRemove); 

UPDATED:

 var requestContext = new System.Web.Routing.RequestContext( new HttpContextWrapper(System.Web.HttpContext.Current), new System.Web.Routing.RouteData()); var Url = new System.Web.Mvc.UrlHelper(requestContext); 

UPDATED:

Try this:

 [OutputCache(Location= System.Web.UI.OutputCacheLocation.Server, Duration=3600,VaryByParam="param1;param2")] 

Otherwise, deleting the cache will not work because you cached the HTML output on the user computer.

+52
Jul 23. '09 at 4:55
source share

In addition to the accepted answer, to support VaryByParam parameters:

  [OutputCache (Duration=3600, VaryByParam="param1;param2", Location = OutputCacheLocation.Server)] public string AjaxHtmlOutputMethod(string param1, string param2) { object routeValues = new { param1 = param1, param2 = param2 }; string url = Url.Action("AjaxHtmlOutputMethod", "Controller", routeValues); Response.RemoveOutputCacheItem(url); } 

However, Egor's answer is much better, because it supports all the values ​​of OutputCacheLocation:

  [OutputCache (Duration=3600, VaryByParam="param1;param2")] public string AjaxHtmlOutputMethod(string param1, string param2) { if (error) { Response.Cache.SetNoStore(); Response.Cache.SetNoServerCaching(); } } 

When SetNoStore () and SetNoServerCaching () , they prevent caching of the current request. Further requests will be cached if functions are also not called for these requests.

This is the ideal solution for handling error situations - when you usually want to cache responses, but not if they contain error messages.

+5
May 04 '16 at 10:01
source share

I think the correct thread is:

 filterContext.HttpContext.Response.Cache.SetNoStore() 
+4
Dec 22 '09 at 11:45
source share

Another option is to use VaryByCustom for OutputCache and handle the invalidation of certain cache elements.

It may work for you, but this is not a general solution to your problem.

+3
Jul 23 '09 at 8:20
source share

Add code to AjaxHtmlOutputMethod

 HttpContext.Cache.Insert("Page", 1); Response.AddCacheItemDependency("Page"); 

To clear the output cache, you can now use

 HttpContext.Cache.Remove("Page"); 
+3
Oct. 13 '12 at 22:00
source share



All Articles