Enumerating Items in a Cache (EntLib 3.1)

Topics: Caching Application Block
Sep 28, 2007 at 5:13 PM
Is it possible to enumerate the items (or at least the keys) of all items being held by a specific instance of CacheManager???
Oct 11, 2007 at 11:20 PM
I have not found a way to enumerate the items or keys. What I have done is create a wrapper class around the CacheManager, and have added a list of strings that holds the keys. When I add an item to the Cache, I add the key to my list of strings. When I perform a get, if I don't find the item in the cache, I remove it from my list of strings.
Dec 30, 2007 at 12:49 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!