Mark Smith

ASP.NET, SQL Server, HTML, CSS and other random thoughts!

Clear your cache

I've just created a site management section for one of my sites, and one of the pages allows you to clear certain elements from the cache. I wanted an option to remove all cached objects but it doesn't appear to have a clear method. A way around this is just to create a dictionary enumerator to loop through the cache and clear objects one at a time e.g.

        Dim CacheEnum As IDictionaryEnumerator = Cache.GetEnumerator
        While (CacheEnum.MoveNext)
            Cache.Remove(CacheEnum.Key.ToString)
        End While

 


 

Posted: Nov 16 2007, 10:32 AM by ca8msm | with 4 comment(s)
Filed under: ,

Comments

Clear your cache - Mark Smith said:

Pingback from  Clear your cache - Mark Smith

# November 16, 2007 7:09 AM

Wim Hollebrandse said:

Alternatively, you could have a wrapper around the Cache API that inserts an application level dependency cache item which all your cachable items depend upon.

When you want to expire the cache, you then simply Remove the root object from the cache, and it will cascade down.

You could use several layers of scope in there if required; so you could introduce session level cache which involves the Session.SessionID as a cache key and for which you have a similar dependency object that lets you clear out the cached items for a user session.

# November 16, 2007 8:13 AM

AndrewSeven said:

I posted a simple CacheHelper for grouping cache items with a shared dependency.

weblogs.asp.net/.../CacheHelper.aspx

# November 16, 2007 8:55 AM

HaiZa said:

Your code helps me...thanks!

# March 13, 2008 3:32 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)