Question

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:

<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. 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

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? Am i missing something? Thanks

Was it helpful?

Solution

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. In our case, we have use SlidingTime expiration policy. Suppose we set the SlidingTime to 10 seconds. What is implied is that the item will be flag as expired if it is not accessed continuously for 10 second. 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...

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top