18 sept 2013

How to remove all items from cache memory



Sometimes you may want to remove all items from cached memory and fill it by new data.

Session State has a method for emptying Session memory for specified user (Session.Abandon()).

but in Cache object there is no the same method with simple technique you can do it.

IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
    HttpContext.Current.Cache.Remove(enumerator.Key);
}
 
Note that, this technique is not efficient for pages with OutputCache type.  For clearing this type cached memory, you should use below code line.

HttpRuntime.Close();

By Morteza Sahragard

No hay comentarios.: