Retrieving the CacheManager name in a custom cache manager

Topics: Building and extending application blocks, Caching Application Block
Jun 8, 2009 at 8:21 AM
Edited Jun 8, 2009 at 8:23 AM


I've written a custom cache manager that wraps the standard cacheManager of EntLib 4.1. It seemed a simple task, but I was faced with a few problems. As mentioned in the documentation I've derived a class from ICacheManager and annotated the class with the ConfigurationElementType attribute.  I've added a custom cache manager named 'cacheManager1' in the config file using the entlib config tool.  So far so good. I've also written a little testapp to consume the custom cache manager. Running this testapp failed. It seems that the custom cache manager must have a constructor with 1 parameter of type NameValueCollection. Adding this constructor did the magic. The cache works fine. Although there's one issue that remains, for instrumentation reasons I need the name of the custom cache manager ('cacheManager1'). It is not available in the NameValueCollection that is passed to the constructor. Does anybody knows how the obtain this value?




Here's my ctr:

public ChcCacheManager(NameValueCollection col)

  // parse the parameters

  string mgrName = "cache manager1";

  int expirationPollFrequencyInSeconds = this.GetConfigIntParamSafe(col, "expirationPollFrequencyInSeconds", mgrName);

  int maximumElementsInCacheBeforeScavenging = this.GetConfigIntParamSafe(col, "maximumElementsInCacheBeforeScavenging", mgrName);

  int numberToRemoveWhenScavenging = this.GetConfigIntParamSafe(col, "numberToRemoveWhenScavenging", mgrName);

  this.expirationMode = (ExpirationMode)this.GetConfigEnumParamSafe(col, "expirationMode", mgrName, typeof(ExpirationMode));

  if (this.expirationMode != ExpirationMode.NeverExpire)

 this.expirationTimespan = this.GetConfigTimespanParamSafe(col, "expirationTimespan", mgrName);



 this.expirationTimespan = TimeSpan.FromDays(3650);

  // create the entlib cacheMgr

  NullBackingStore bs = new NullBackingStore();

  InstrumentationConfigurationSection ics = InstrumentationConfigurationSection.GetSection(cs);

  if (ics == null)

 ics = new InstrumentationConfigurationSection(false, false, false);

  CachingInstrumentationProvider cip = new CachingInstrumentationProvider(mgrName, ics.PerformanceCountersEnabled, ics.EventLoggingEnabled, ics.WmiEnabled, new AppDomainNameFormatter());

  Cache cache = new Cache(bs, cip);

  ExpirationTask et = new ExpirationTask(cache, cip);

  ScavengerTask st = new ScavengerTask(numberToRemoveWhenScavenging, maximumElementsInCacheBeforeScavenging, cache, cip);

  BackgroundScheduler bgs = new BackgroundScheduler(et, st, cip);

  ExpirationPollTimer ept = new ExpirationPollTimer(expirationPollFrequencyInSeconds);

  cacheMgr = new CacheManager(cache, bgs, ept);

Jun 8, 2009 at 11:29 AM


One thing you can do is to add the name of the cacheManager as a KeyValuePair to the "Attributes" property of the Cache Manager. For example, I'll add a KeyValuePair with my Key as "Name" and the [Name of the Cache Manager] as the value. Then later it will be available to the NameValueCollection parameter of your constructor.

string cacheManagerName = string.Empty;

if (list.AllKeys.Contains("Name"))


    cacheManagerName = list["Name"].ToString();


Valiant Dudan
Global Technology & Solutions
Avande, Inc.

Jun 12, 2009 at 7:50 AM


Indeed that is a solution to the problem, but it violates the DRY principle. In the config file I've a parameter 'name' which is used by EntLib internally and a parameter 'Name' which I use in my custom cache manager. I think I've to live with it.