Get list of items in a cache manager

Topics: Caching Application Block
Apr 30, 2007 at 8:03 PM
I would like to be able to get a list of the items in the cache manager for debugging purposes on an admin page on my site, but I don't see an API for this. Is there a way to iterate through the items in the cache?
May 2, 2007 at 3:19 AM
Unfortunately, there is no way to iterate through items in the cache.

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#
May 2, 2007 at 5:21 AM
Is there a way to promote this to the wish list? I was able to resolve my issue by logging my inserts and reads from the cache and pouring through reams of logging info, but I think it might have been easier if I could have written out the cache to an admin page and inspected the contents. Certainly not a high priority, but would be a nice to have for debugging purposes.
May 2, 2007 at 6:03 PM
Edited May 2, 2007 at 6:06 PM
Add an object to your Cache that will hold the list of items. It's a work-around but it's probably better than reading through log data.

Something like this... (this is probably ugly code, but you get the idea)

When Cache is created/initialized, add an empty List<string> to cache:
cm.Add("CacheItems", new List<string>());

Then use the following methods to add/remove/read data in Cache:
void AddToCache<T>(CacheManager cm, string name, T value)
{
     List<string> items = cm["CacheItems"] as List<string>;
     cm.Add(name, value);
     items.Add(name);
     cm["CacheItems"] = items;
}
 
void DelFromCache<T>(CacheManager cm, string name)
{
     List<string> items = cm["CacheItems"] as List<string>;
     cm.Remove(name);
     items.Remove(name);
     cm["CacheItems"] = items;
}
 
T ReadFromCache<T>(CacheManager cm, string name)
{
     return (T) cm[name];
}
 
List<string> CacheItems()
{
     return cm["CacheItems"] as List<string>;
}
May 23, 2007 at 8:18 PM
Edited May 23, 2007 at 8:33 PM
I'm wondering why any method like "Items" (or "Keys" if you prefer) is not implemented in the CacheManager. The life would be a bit easier.

Except the code from Greif above, is there any other way to iterate through CacheManager's items?
May 23, 2007 at 9:40 PM
Yes, I'm surprised the CacheManager class does not implement IDictionary<string, object> at the very least. Then you would have access to a Keys property. Plus the addition of generic methods would make casting/boxing a non-issue.

It wouldn't be hard to modify the source yourself to add that feature. It would be cleaner than the code above.
Dec 30, 2007 at 12:54 AM
You can use reflection to get to the private cache store from the CacheManager.

private CacheManager _CacheManager = CacheFactory.GetCacheManager();
private Cache myCache = (Cache)CacheManager.GetType().GetField("realCache", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(CacheManager);

foreach (DictionaryEntry Item in myCache.CurrentCacheState)
{
Object Key = Item.Key;
CacheItem CacheItem = (CacheItem)Item.Value;
// do Something with them
}

In my opinion, this should be an exposed property, even if readonly!
Nov 7, 2010 at 6:34 PM
vb version
    Dim AppCache As CacheManager = CacheFactory.GetCacheManager(cacheManagerName)
    Dim _cache As Cache = DirectCast(GetType(CacheManager).GetField("realCache", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic).GetValue(AppCache), Cache)

    For Each item As DictionaryEntry In _cache.CurrentCacheState

    Next