How to clear cache in specified controller in asp mvc?
41,230
Solution 1
try this:
put this on your Model:
public class NoCache : 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);
}
}
and on your specific controller: e.g:
[NoCache]
[Authorize]
public ActionResult Home()
{
////////...
}
source: original code
Solution 2
Have you tried
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult DontCacheMeIfYouCan()
{
}
If this doesn't do it for you then a custom attribute like Mark Yu suggests.
Solution 3
Try this :
public void ClearApplicationCache()
{
List<string> keys = new List<string>();
// retrieve application Cache enumerator
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
// copy all keys that currently exist in Cache
while (enumerator.MoveNext())
{
keys.Add(enumerator.Key.ToString());
}
// delete every key from cache
for (int i = 0; i < keys.Count; i++)
{
Cache.Remove(keys[i]);
}
}
Author by
testCoder
Updated on July 09, 2022Comments
-
testCoder almost 2 years
Possible Duplicate:
How to programmatically clear outputcache for controller action methodHow to clear cache in specified controller?
I try to use several approaches:
Response.RemoveOutputCacheItem(); Response.Cache.SetExpires(DateTime.Now);
There is no any effect, it not work. :( May be exists any way to get all keys in controller cache and remove they explicitly? And in which overridden method i should perform clear cache? and how to do that?
Does any ideas?