由于 CacheManager 的默认实现不提供 GetItemsOfType<> (以及许多其他),我想构建自己的:
[ConfigurationElementType(typeof(CustomCacheManagerData))]
public class MyCacheManager : ICacheManager
{
//The same constructor as in CacheAppBlock - CacheManager, but it's public here:
public MyCacheManager(Cache realCache, BackgroundScheduler scheduler, ExpirationPollTimer pollTimer)
{
this.realCache = realCache;
this.scheduler = scheduler;
this.pollTimer = pollTimer;
}
//the other code is basically copy/paste from CacheManager in EntLib, with some of my methods like:
public T[] GetItemsOfType<T>()
{
return realCache.CurrentCacheState.Values.OfType<T>().ToArray();
}
//I also have some other custom code on the underlying Hashtable in realCache
}
配置部分(类型部分指向我的类,不使用加密):
<cachingConfiguration defaultCacheManager="SomeCacheManager">
<cacheManagers>
<add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
type="MyNamespace.MyCacheManager, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="SomeCacheManager" />
</cacheManagers>
<backingStores>
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Null Storage" />
</backingStores>
</cachingConfiguration>
我现在面临的问题是如何创建 MyCacheManager?这:
mCityCacheManager = (MyCacheManager)CacheFactory.GetCacheManager("SomeCacheManager");
抛出异常说 MyCacheManager 中没有构造函数(但是与 EntLib 的 CacheManager 相同,只是它们在我的班级中是公开的......)