Caching Dependencies

Topics: Caching Application Block , General discussion
Nov 5, 2007 at 1:06 PM
Hi There,

I am attempting to figure out how to do inter-cache dependencies
(cache item 1 depends on cache item 2, etc) with the Enterprise
Library Caching block and am stumped. Does any one have any
suggestions?

I would also like to enumerate through my cached items so that I can
display them, (so that I can debug my application's caching
behaviors). The caching API doesn't seem to have anything I could use
to enumerate through the cached collection. There is a count but the
GetData and the [] method both take the caching keys. Does anyone have
any suggestions?

Thanks

-Cam
Dec 29, 2007 at 11:19 PM
I solved the first problem by creating my own cache expiration that inherits ICacheExpiration. See code below.

public class KeyExpiration : ICacheItemExpiration
{
protected Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager _CacheManager = CacheFactory.GetCacheManager();
protected String _Key;

public KeyExpiration(string Key)
{
_Key = Key;
CacheManager.Add(Key, System.DateTime.Now, Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority.Normal, null, null);
}

public bool HasExpired() { return CacheManager.GetData(Key) == null; }

public void Initialize(Microsoft.Practices.EnterpriseLibrary.Caching.CacheItem owningCacheItem) {}

public void Notify() {}
}


The second problem is easily soved with a little reflection. It is relatively easy to get at the private variables of the Cache Manager.

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