Clear your web applications Cache


We all know that Cache class provides an implementation for cache for a web application in ASP.net. Cache provides fast access to frequently used data. You can add your not so frequently changed data to Cache for a speedy access. Cache has an application scope, what ever is in cache is used by all users of your web application.

Being said that, I find sometimes a need to clear my web applications cache without affecting anything. In order to do that, this is the trick I have come up with (nothing genius here 🙂

I created a secure page (admin login required) with a “Clear All Cache” Button,
and here is what I do in that button click!

private void ClearCache_Click(object sender, System.EventArgs e)
{
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
string cacheKey="";
while (CacheEnum.MoveNext())
{
cacheKey = Convert.ToString(CacheEnum.Key);
Cache.Remove(cacheKey);
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s