Reputation: 36674
I use EnterpriseLibrary cacheManager
<cacheManagers>
<add name="NonExperimentalAppsCache" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="10000" numberToRemoveWhenScavenging="100" backingStoreName="Null Storage" />
</cacheManagers>
I want the experation time to be 1 min (absolute, not refreshing on evey cache touch) how can I do this? because now it saves data for much longer.
I use Repository over the cache
public static List<string> GetAllNonExperimentalAppsNames()
{
List<string> nonExperimentalAppsNames = NonExperimentalAppsCacheManager.Get();
if (nonExperimentalAppsNames == null)
{
//was not found in the cache
nonExperimentalAppsNames = GetAllNonExperimentalAppsNamesFromDb();
if (nonExperimentalAppsNames != null)
{
NonExperimentalAppsCacheManager.Set(nonExperimentalAppsNames);
}
else
{
mApplicationLogger.Info(string.Format("GetAllNonExperimentalAppsNames:: nonExperimentalAppsNames list is null"));
}
}
return nonExperimentalAppsNames;
}
..
internal static class NonExperimentalAppsCacheManager
{
private const string NONEXPERIMENTALAPPS = "NonExperimentalApps";
private static readonly ICacheManager nonExperimentalAppsCache = CacheFactory.GetCacheManager("NonExperimentalAppsCache");
internal static List<String> Get()
{
return nonExperimentalAppsCache[NONEXPERIMENTALAPPS] as List<String>;
}
internal static void Set(List<String> settings)
{
nonExperimentalAppsCache.Add(NONEXPERIMENTALAPPS, settings);
}
}
Upvotes: 1
Views: 3357
Reputation: 22655
When adding items to the cache specify an absolute expiration:
internal static void Set(List<String> settings)
{
nonExperimentalAppsCache.Add(NONEXPERIMENTALAPPS, settings,
CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromMinutes(1)));
}
Expiration results in the item being removed from the cache. It would be up to you to refresh it (which you can do using an ICacheItemRefreshAction
). If you specify an expiration of 1 minute when adding the item to the cache but the expiration pool frequency is 10 minutes the item will not be removed from cache until after 10 minutes unless you attempt to access the item. If you call Get()
when the item is "expired" but still in the cache (because the background process has not run), the item will be removed from the cache and a null will returned.
I recommend reading Design of the Caching Application Block for more discussion of the internal design.
Upvotes: 2