扩展企业库缓存块 - 如何获取 MyCacheManager 的实例?
由于 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
}
配置部分(type部分指向我的类,不使用加密):
<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 ? The:
mCityCacheManager = (MyCacheManager)CacheFactory.GetCacheManager("SomeCacheManager");
抛出异常,表示 MyCacheManager 中没有构造函数(但有,与 EntLib 的 CacheManager 中相同,只是它们在我的类中是公共的...)
Since the default implementation of CacheManager doesn't provide GetItemsOfType<> (and many others) I thought of building my own:
[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
}
The cofiguration part (the type part points to my class, encryption isn't used):
<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>
The problem I'm facing now is how to create MyCacheManager?
The:
mCityCacheManager = (MyCacheManager)CacheFactory.GetCacheManager("SomeCacheManager");
throws exception saying there's no Constructor in MyCacheManager (but there is, same as in EntLib's CacheManager only they are public in my class...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
MyCacheManager
与 EntLib 并不完全相同! 我指的不是额外的方法。 看一下声明。原始
CacheManager
:MyCacheManager
:除了名称差异(并且您没有扩展
IDisposable
)之外,请注意元素类型属性。您正在使用(您必须)自定义的。 自定义的需要一个采用
NameValueCollection
作为参数的构造函数。可以这么说,它是一个通用配置驱动程序,因此不能期望它知道使用由 Cache 对象、调度程序和轮询计时器组成的 3 参数构造函数来创建实例,就像您所拥有的那样。 相反,它通过您必须手动解析的基本
NameValueCollection
传入这些值(或您在配置文件中设置为属性的任何内容)。另请参阅:创建Enterprise Library 4 的自定义缓存管理器
That's because
MyCacheManager
isn't exactly like the EntLib one! And I don't mean the extra methods. Take a look at the declarations.Original
CacheManager
:MyCacheManager
:Other than the name difference (and you didn't extend
IDisposable
), notice the element type attributes.You're using (you have to) the Custom one. The custom one requires a constructor that takes a
NameValueCollection
as a parameter.It is a generic configuration driver, so to speak, and as such it cannot be expected to know to create your instance with a 3 parameter constructor consisting of a Cache object, scheduler, and poll timer like you've got. Instead, it passes in these values (or anything you've got set as attributes in the configuration file) via a basic
NameValueCollection
which you'll have to parse manually.See also: Create a custom caching manager for Enterprise Library 4