Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View the data cached in System.Web.HttpRuntime.Cache

Is there any tool available for viewing cached data in HttpRunTime cache..?
We have an Asp.Net application which is caching data into HttpRuntime Cache. Default value given was 60 seconds, but later changed to 5 minutes. But feels that the cached data is refreshing before 5 minutes. Don't know what is happening underneath.

Is there any tool available or how we can see the data cached in HttpRunTime Cache.... with expiration time...?
Following code is using for add items to cache.

    public static void Add(string pName, object pValue)
    {
    int cacheExpiry= int.TryParse(System.Configuration.ConfigurationManager.AppSettings["CacheExpirationInSec"], out cacheExpiry)?cacheExpiry:60;
    System.Web.HttpRuntime.Cache.Add(pName, pValue, null, DateTime.Now.AddSeconds(cacheExpiry), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
    }


Thanks.

like image 851
Sunil Avatar asked Jun 06 '13 07:06

Sunil


2 Answers

The Cache class supports an IDictionaryEnumerator to enumerate over all keys and values in the cache.

IDictionaryEnumerator enumerator = System.Web.HttpRuntime.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
    string key = (string)enumerator.Key;
    object value = enumerator.Value;
    ...
}

But I don't believe there is any official way to access metadata such as expiration time.

like image 107
Joe Avatar answered Oct 20 '22 22:10

Joe


Cache class supports an IDictionaryEnumerator to enumerate over all keys and values in the cache. The below code is an example for how to delete every key from cache:

List<string> keys = new List<string>();

// retrieve application Cache enumerator
IDictionaryEnumerator enumerator = System.Web.HttpRuntime.Cache.GetEnumerator();

// copy all keys that currently exist in Cache
while (enumerator.MoveNext())
{
    keys.Add(enumerator.Key.ToString());
}

// delete every key from cache
for (int i = 0; i < keys.Count; i++)
{
    HttpRuntime.Cache.Remove(keys[i]);
}
like image 44
Praveen04 Avatar answered Oct 20 '22 20:10

Praveen04