How to clear cache in specified controller in asp mvc?

Possible Duplicate:
How to programmatically clear outputcache for controller action method

How 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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 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]);
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x