71

If the controller action has the OutputCache attribute specified on an action, is there any way to clear the output cache without having to restart 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'm looking at using HttpResponse.RemoveOutputCacheItem(string path) to clear it, but I'm having trouble figuring out what the path should be to map it to the action method. I'm going to try again with the aspx page that is rendered by ViewName.

Possibly I'll just manually insert the output of RenderToString into the HttpContext.Cache instead if I can't figure this one out.

Update

Please note that the OutputCache is VaryByParam, and testing out a hardcoded path "/controller/action" does not actually clear the outputcache, so it looks like it has to match "/controller/action/param1/param2".

That means I'll probably have to revert to object level caching and manually cache the output for RenderToString() :(

1
  • 1
    Try to add location="Server" to OutputCache attribute - you can't clear client's cache from the server Commented Jul 27, 2009 at 19:29

5 Answers 5

61

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 the cache deletion won't work because you've cached the HTML output on the user's machine

Sign up to request clarification or add additional context in comments.

10 Comments

Of course, now I'm having trouble figuring out how to instantiate a UrlHelper inside the webservice that has the function. Arg.
I gave up on that attempt. I couldn't find any way to get the RequestContext to pass into the UrlHelper constructor method. So I tested it out by hard coding in "/controller/action". This shows no error, but does not clear the cache.
Thanks, didn't know about the HttpContextWrapper(), so that actually builds and runs. Unfortunately, it still doesn't clear the outputcache due to the varybyparam that's been set on the method attribute, so I've ended up caching the output of RenderToString using a key that includes the params to differentiate it.
I think I answered my own question: stackoverflow.com/questions/11585/…
Just a note. In MVC3 you now need to use Location = OutputCacheLocation.Server and include System.Web.UI. 'Location="Server" will no longer compile.
|
8

Further 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 OutputCacheLocation values:

  [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() are called, they prevent the current Request being cached. Further requests will be cached, unless the functions are called for those requests as well.

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

Comments

5

I think correct flow is:

filterContext.HttpContext.Response.Cache.SetNoStore()

1 Comment

Thanks, this is a much better answer, because it works for all Locations. Response.Cache.SetNoStore()
4

Add code to AjaxHtmlOutputMethod

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

To clear output cache you can now use

HttpContext.Cache.Remove("Page");

Comments

3

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

Maybe it works for you, but it's not a general solution to your problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.