Hi All,
Though leverage browser caching reduces the load times of pages by storing commonly used files from your website on your visitors browser but sometimes, we need to force application to clear Cache (or System.Web.HttpRuntime.Cache), many ways are there to accomplish this requirement, e.g. meta tags can be added on the page to prevent storing items in the Cache. You can use the combination of meta tags, also known as HTML Cache Control Tags, as mentioned following:
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
Another way to clear cache is implementing code snippets in code behind. As you can see in example given below, "Cache.GetEnumerator()" method is invoked, the purpose of this function is to retrieves a dictionary enumerator used to iterate through the key settings and their values contained in the cache. Its Return Value
Type is System.Collections.IDictionaryEnumerator which is an enumerator to iterate through the Cache object.
The following example creates an IDictionaryEnumerator object, CacheEnum, using the GetEnumerator method. The enumerator moves through the cache and adds the keys into IDictionaryEnumerator. Cache.Remove function removes the specified item from the application's Cache object and here we are vanishing all the cache items using FOR loop.
public void ClearCacheItems()
{
List keys = new List();
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while (enumerator.MoveNext())
keys.Add(enumerator.Key.ToString());
for (int i = 0; i < keys.Count; i++)
Cache.Remove(keys[i]);
}
Happy Coding..!
0 Comment(s)