ICache Manager To List

Topics: Caching Application Block
Apr 9, 2012 at 10:54 AM

Hi Everybody,

How can i get cache list from ICacheManager iterface?

I can only get a cache value by string key but i want get all cache list without string key.

Please help me. 

Apr 9, 2012 at 5:27 PM

The short answer is that you can't because the ICacheManager interface only allows you to access items by their key and doesn't provide a means to access the internal representation of the cache.

One solution would be to create a facade around ICacheManager where you can manage keeping track of the keys and expose the ability to construct a list (it's not clear if you want a list of keys or a list of values).

Now, there is a way to get at that information but it is dependent on the internal implementation of Enterprise Library as well as the internal representation of the cache so it is a tightly coupled, brittle solution that could break with further releases of Enterprise Library.  With those caveats, you can bypass the CacheManager and access the internal Cache object using the EnterpriseLibraryContainer:

    ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>("My Cache Manager");
    cacheManager.Add("Hello", "World");

    // Relies on implementation detail where Cache is registered with same name as CacheManager
    Cache cache = EnterpriseLibraryContainer.Current.GetInstance<Cache>("My Cache Manager");
    Hashtable cacheHash = cache.CurrentCacheState;

    List<string> listOfKeys = cacheHash.Keys.Cast<string>().ToList();
    List<object> listOfValues = cacheHash.Values.Cast<CacheItem>().Select(c => c.Value).ToList();

This appears to work but it is not the standard or recommended approach for the reasons given above.

Randy Levy
Enterprise Library support engineer

Apr 10, 2012 at 6:04 AM

randlevy ,thanks so much it worked.


Best Regards