1

我使用 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>

我希望体验时间为 1 分钟(绝对,在每次缓存触摸时不刷新)我该怎么做?因为现在它可以保存更长时间的数据。

我在缓存上使用存储库

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

1 回答 1

2

将项目添加到缓存时指定绝对过期时间:

internal static void Set(List<String> settings)
{
    nonExperimentalAppsCache.Add(NONEXPERIMENTALAPPS, settings, 
        CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromMinutes(1)));
}

过期会导致该项目从缓存中删除。刷新它由您决定(您可以使用 来完成ICacheItemRefreshAction)。如果您在将项目添加到缓存时指定 1 分钟的到期时间,但到期池频率为 10 分钟,则除非您尝试访问该项目,否则该项目将在 10 分钟后才会从缓存中删除。如果您Get()在该项目“过期”但仍在缓存中时调用(因为后台进程尚未运行),则该项目将从缓存中删除并返回空值。

我建议阅读缓存应用程序块的设计,以了解更多关于内部设计的讨论。

于 2013-01-10T08:11:58.290 回答