View the data cached in System.Web.HttpRuntime.Cache
Asked Answered
S

2

14

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.

Seaway answered 6/6, 2013 at 7:28 Comment(0)
O
20

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.

Odiliaodille answered 6/6, 2013 at 7:38 Comment(0)
D
8

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]);
}
Dressel answered 30/9, 2013 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.