41

Using ASP.Net MVC 3 I have a Controller which output is being cached using attributes [OutputCache]

[OutputCache]
public controllerA(){}

I would like to know if it is possible to invalidate the Cache Data (SERVER CACHE) for a Specific Controller or generally all the Cache data by calling another controller

public controllerB(){} // Calling this invalidates the cache
Wibble
  • 796
  • 1
  • 8
  • 23
GibboK
  • 71,848
  • 143
  • 435
  • 658

2 Answers2

61

You could use the RemoveOutputCacheItem method.

Here's an example of how you could use it:

public class HomeController : Controller
{
    [OutputCache(Duration = 60, Location = OutputCacheLocation.Server)]
    public ActionResult Index()
    {
        return Content(DateTime.Now.ToLongTimeString());
    }

    public ActionResult InvalidateCacheForIndexAction()
    {
        string path = Url.Action("index");
        Response.RemoveOutputCacheItem(path);
        return Content("cache invalidated, you could now go back to the index action");
    }
}

The Index action response is cached on the server for 1 minute. If you hit the InvalidateCacheForIndexAction action it will expire the cache for the Index action. Currently there's no way to invalidate the entire cache, you should do it per cached action (not controller) because the RemoveOutputCacheItem method expects the url of the server side script that it cached.

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin, I really appreciate your help on this! – GibboK Apr 25 '13 at 13:11
  • 3
    How do you achieve this for the -> Location = OutputCacheLocation.Client , is there other specific parameters/method ? – Anuj Pandey Jul 05 '13 at 06:54
  • 3
    e10, you cannot remove data that is cached on the client browser from the server. This doesn't make sense. – Darin Dimitrov Jul 05 '13 at 06:56
  • sorry if not related, a question, how to invalidate [memory cache](http://stackoverflow.com/questions/385986/caching-in-asp-net-mvc) when there is update in the database , do you use a service to check often , then how you clear the cache ? – Shaiju T Mar 19 '16 at 17:46
  • 3
    @DarinDimitrov Can I clear the cache on client side? I am finding it difficult to find any articles with that info. – Rajshekar Reddy Nov 09 '16 at 12:25
  • @DarinDimitrov - How does ASP.NET stores output cached pages? Because I can not find those pages in HttpContext.Cache object. Is it possible to see a list of pages that have been cached by the application using OutputCache attribute? – Ankush Jain Sep 30 '18 at 18:56
  • 1
    @AnkushJain , I am using the Redis Cache to store the cached pages via Output caching, For each action (including parameter in action) response which is supposed to be cached, it generates the uniquekey and stores the serialized content on that key in redis. – Ashish Shukla Jun 06 '19 at 11:12
0

You can do that by using a custom attribute, like so:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}

Then on your controllerb you can do:

[NoCache]
public class controllerB
{
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • 3
    This will invalidate the client side cache. But what if ControllerA's views are cached on the server (which is the default behavior)? – Darin Dimitrov Apr 24 '13 at 14:20
  • @DarinDimitrov This will force the client to get a new *uncached* version from the server. – Mathew Thompson Apr 24 '13 at 14:23
  • 1
    Yes, of ControllerB. But he is asking how to do that with ControllerA which was the one decorated with the OutputCache attribute in the first place. Since you decorated ControllerB with the NoCache attribute, it will never be cached but I don't think that this is what is being asked here. He wants to know how to invalidate the cache of ControllerA when someone sends a request to ControllerB so that subsequent requests to ControllerA are no longer cached. – Darin Dimitrov Apr 24 '13 at 14:24
  • 3
    I would need invalidate the SERVER CACHE, also if possible I would like to know if would be possible to INVALIDATE ALL CACHE for all Controllers for instance – GibboK Apr 24 '13 at 14:37