简体   繁体   English

Microsoft Enterprise Library-缓存机制到期

[英]Microsoft Enterprise Library - Caching Mechanism expiry

Good Morning 早上好

I am trying to integrate the caching mechanism in my current project and wanted to ask on the best practice and questions I have. 我正在尝试将缓存机制集成到当前项目中,并想问一问我的最佳实践和问题。

My web.config is defined as follows: 我的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>

When I am adding something to the cache, I use the following code: 当我向缓存中添加内容时,使用以下代码:

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)));
}

From the documentation, I can see that items put in the cache via the method Add(string key, object value) does not expire. 从文档中,我可以看到通过方法Add(string key, object value)放入缓存的项没有过期。 However, I can see that the method Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations) defines a timespan that specifies when the cache will expire 但是,我看到方法Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)定义了一个时间跨度,该时间跨度指定了缓存何时到期

My question is, why should we define the expirationPollFrequencyInSeconds property in the web.config when we would need to define a timespan again when adding items in the cache using the second Add method? 我的问题是,当使用第二个Add方法在缓存中添加项目时需要再次定义时间跨度时,为什么还要在web.config中定义expirationPollFrequencyInSeconds属性? Am i missing something? 我想念什么吗? Thanks 谢谢

Correct me if I'm wrong. 如果我错了纠正我。

The ICacheItemExpiration (in our case) that we specify in the Add() method actually defines the expiration policy that will be applied to that item being added to the cache. 我们在Add()方法中指定的ICacheItemExpiration (在我们的示例中)实际上定义了过期策略,该策略将应用于将添加到缓存中的该项。 In our case, we have use SlidingTime expiration policy. 在本例中,我们使用了SlidingTime过期策略。 Suppose we set the SlidingTime to 10 seconds. 假设我们将SlidingTime设置为10秒。 What is implied is that the item will be flag as expired if it is not accessed continuously for 10 second. 这意味着如果连续10秒钟不访问该项目,则该项目将被标记为已过期。 This is useful when we need to keep the item alive when there are multiple request coming in for that item. 当我们需要该项目保持活动状态时,当该项目有多个请求时,这很有用。 As a side note, we have other expiration policy such as AbsoluteTime , FileDependency , NeverExpire ... 附带说明一下,我们还有其他到期策略,例如AbsoluteTimeFileDependencyNeverExpire ...

We need to understand that when an item has been expired, it is still present in the hash, until a background scheduler remove that item (depending on its expiration policy that the background scheduler will check for expiration). 我们需要了解,当某个项目过期时,它仍会存在于哈希中,直到后台调度程序删除该项目为止(取决于后台调度程序将检查其到期时间的到期策略)。 When setting a value for expirationPollFrequencyInSeconds , we are defining the time interval for the background scheduler to execute and analyse the hash to remove items that have been expired (according to the expiration policy defined on that item). 在为expirationPollFrequencyInSeconds设置值expirationPollFrequencyInSeconds ,我们正在定义后台调度程序执行和分析哈希以删除已过期项目的时间间隔(根据该项目上定义的过期策略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM