我想整合緩存機制在我當前的項目,並希望問我最好的做法和問題。
我的web.config的定義如下:
<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.config中expirationPollFrequencyInSeconds
屬性,當我們需要的時候再定義一個時間跨度使用第二個Add方法在緩存中添加項目?我錯過了什麼嗎? 謝謝