OutputCache Flush Cache through an Action in an MVC 3 Application
In many actions from the MVC application we are building up, we use OutputCache as follows:
[OutputCache(Duration = 3600, VaryByCustom = "language")]
public ActionResult SomeAction()
{
//Action..
}
So, I want to have an action where I can flush manually all these caches:
public ActionResult RefrescarCache()
{
var keys = HttpContext.Cache.Cast<DictionaryEntry>().ToList();
keys.ForEach(k => HttpContext.Cache.Remove(k.Key.ToString()));
ViewBag.operationResult= "The cache was flushed succesfully!";
return View();
}
The thing, that it seems to not work. I will aprecc开发者_StackOverflow社区iate any idea or advice you have!
We've had the same problem and the only solution which was working was with:
HttpResponse.RemoveOutputCacheItem(url)
like Giedrius already mentioned.
You should look here: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.removeoutputcacheitem.aspx
Since everyone is asking for a way to clear the all the url´s...
I can think of two ways:
1- painfull but easy, maintain an array of virtual paths to be clean.
foreach(string path in myArray){HttpResponse.RemoveOutputCacheItem(path); }
2- Uses reflection to get everything, example here: list OutputCache entry
I think this is dificult because it´s not ASP.NET caching the pages, but IIS (7+) kernel cache.
精彩评论