How to remove all items from cache memory

Some times 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();

Have a good time.

Published Monday, August 11, 2008 6:31 PM by mlife

Comments

# re: How to remove all items from cache memory

Thursday, November 13, 2008 8:23 AM by Eddie

Nice Work Morteza. Just wanted to know what you will do in case of distributed sessions, since ASP.NET cache is stand alone in process cache.

# re: How to remove all items from cache memory

Monday, December 01, 2008 3:40 AM by Iain

Thanks,

Been looking for this:

HttpRuntime.Close();

and why is it not on the cache page on msdn !

# re: How to remove all items from cache memory

Tuesday, August 18, 2009 3:48 AM by Anil

Good Post mlife!!!

Leave a Comment

(required) 
(required) 
(optional)
(required)