我在应用程序上使用缓存应用程序块。配置文件如下所示:
<cachingConfiguration defaultCacheManager="Cache Manager">
<cacheManagers>
<add name="ParamCache" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore"/>
</cacheManagers>
<backingStores>
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="NullBackingStore"/>
</backingStores>
</cachingConfiguration>
我认为expirationPollFrequencyInSecconds属性将控制缓存中存储的值的过期,因此,如果我尝试获取缓存存储60秒或更长时间的值,它将从DB而不是缓存中获取。然而,在这种配置中,我看到该值仍在从缓存中获取大约5分钟,然后才从DB中获取更新后的值。
我错过了什么?
发现问题。expirationPollFrequencyInSconds参数不影响缓存中的项目过期,只影响清除过期项目的频率。
实际上,过期时间是在项目添加到缓存时设置的,在我的情况下,它被设置为5分钟。。。
您实际上还没有在缓存上设置超时策略。expirationPollFrequencyInSeconds
属性执行它所说的,它每60秒轮询一次缓存。
我相信,目前缓存中的默认行为是在缓存中存储多达1000个元素,然后通过删除使用最少的10个缓存项来开始清理内存。它将每60秒检查一次每个项目,就像你设置的那样。
您需要在代码中的某个位置执行此操作,或者可以在配置文件中进行配置,这就是我的建议。
AbsoluteTime absTime = new AbsoluteTime(TimeSpan.FromMinutes(1));
cacheManager.Add("CacheObject", cachedInformation, CacheItemPriority.Normal, null, absTime);
这里有一篇很好的CodeProject文章,描述了如何正确使用它,包括看起来像是代码的精确副本的内容。