【问题标题】:Microsoft Enterprise Library - Caching Mechanism expiryMicrosoft Enterprise Library - 缓存机制到期
【发布时间】:2013-05-25 14:21:41
【问题描述】:

早安

我正在尝试将缓存机制集成到我当前的项目中,并想询问我的最佳实践和问题。

我的 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) 方法定义了一个时间跨度,用于指定缓存何时到期

我的问题是,当我们需要在使用第二种 Add 方法在缓存中添加项目时再次定义时间跨度时,我们为什么要在 web.config 中定义 expirationPollFrequencyInSeconds 属性?我错过了什么吗? 谢谢

【问题讨论】:

    标签: c# caching enterprise-library-5


    【解决方案1】:

    如果我错了,请纠正我。

    我们在Add() 方法中指定的ICacheItemExpiration(在我们的例子中)实际上定义了将应用于要添加到缓存中的项目的过期策略。在我们的例子中,我们使用了SlidingTime 过期策略。假设我们将SlidingTime 设置为 10 秒。这意味着如果 10 秒内没有连续访问该项目,则该项目将被标记为过期。当我们需要在有多个请求进入该项目时保持该项目处于活动状态时,这很有用。附带说明一下,我们还有其他过期策略,例如 AbsoluteTimeFileDependencyNeverExpire...

    我们需要了解,当一个项目已过期时,它仍然存在于哈希中,直到后台调度程序删除该项目(取决于后台调度程序将检查过期的过期策略)。在为expirationPollFrequencyInSeconds 设置值时,我们定义了后台调度程序执行和分析哈希以删除已过期项目的时间间隔(根据该项目上定义的过期策略)。

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多