Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invalidate cache data [OutputCache] from a Controller?

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 
like image 705
GibboK Avatar asked Apr 24 '13 14:04

GibboK


People also ask

What happens when cache is invalidated?

Cache invalidation describes the process of actively invalidating stale cache entries when data in the source of truth mutates. If a cache invalidation gets mishandled, it can indefinitely leave inconsistent values in the cache that are different from what's in the source of truth.

When should cache be invalidated?

Cache invalidation is a process where the computer system declares the cache entries as invalid and removes or replaces them. The basic objective of using cache invalidation is that when the client requests the affected content, the latest version is returned.

Why do we need to invalidate cache?

Cache invalidation is the strategy which we will utilise in order to decide which items to evict and when, in order to make space for newer items which have a higher likelihood of being required again.


2 Answers

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.

like image 195
Darin Dimitrov Avatar answered Oct 04 '22 01:10

Darin Dimitrov


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 { } 
like image 25
mattytommo Avatar answered Oct 04 '22 00:10

mattytommo