微软企业库-缓存机制到期
本文关键字:机制 缓存 企业库 微软 | 更新日期: 2023-09-27 18:16:15
我正在尝试将缓存机制集成到我当前的项目中,并想询问有关最佳实践和问题。
我的网页。配置定义如下:
<configSections>
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<cachingConfiguration defaultCacheManager="SomeName">
<cacheManagers>
<add expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="SomeName" />
</cacheManagers>
<backingStores>
<add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Null Storage" />
</backingStores>
</cachingConfiguration>
当我向缓存添加内容时,我使用以下代码:
ICacheManager manager = CacheFactory.GetCacheManager("SomeName");
if (manager.GetData(key) != null && IsCacheEnable)
{
specialChars = (Hashtable)manager.GetData(key);
}
else
{
manager.Add(key, specialChars, CacheItemPriority.Normal, null, new SlidingTime(new TimeSpan(0, 0, CacheExpirySecond)));
}
从文档中,我可以看到通过方法Add(string key, object value)
放入缓存的项不会过期。但是,我可以看到方法Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
定义了一个时间范围,该时间范围指定了缓存何时过期
我的问题是,为什么我们要在web中定义expirationPollFrequencyInSeconds
属性?当使用第二个Add方法在缓存中添加项目时,我们需要再次定义一个时间跨度?我错过什么了吗?由于
如果我说错了请指正。
我们在Add()
方法中指定的ICacheItemExpiration
(在本例中)实际上定义了将应用于添加到缓存中的项的过期策略。在本例中,我们使用SlidingTime
过期策略。假设我们将SlidingTime
设置为10秒。这意味着,如果条目在10秒内没有被连续访问,它将被标记为过期。当有多个请求进入该项目时,我们需要使该项目保持活动状态,这很有用。顺便说一下,我们还有其他的过期策略,如AbsoluteTime
, FileDependency
, NeverExpire
…
我们需要理解,当一个项过期时,它仍然存在于哈希中,直到后台调度程序删除该项(取决于后台调度程序将检查过期的过期策略)。当设置expirationPollFrequencyInSeconds
的值时,我们定义了后台调度器执行和分析哈希的时间间隔,以删除已经过期的项(根据在该项上定义的过期策略)。