【问题标题】:Will the following cached item expire after 15 minutes?以下缓存项会在 15 分钟后过期吗?
【发布时间】:2012-10-11 03:49:28
【问题描述】:
_cache.Insert(cacheKey, userList, null,
 Cache.NoAbsoluteExpiration,
 new TimeSpan(0, 15, 0),
 CacheItemPriority.High, null);

我上面的代码似乎没有在 3 分钟后过期缓存,userList 对象从更新的数据库中提取数据,但缓存在 15 分钟后没有过期。

怎么了?

【问题讨论】:

    标签: c# asp.net caching


    【解决方案1】:

    您正在使用Cache.NoAbsoluteExpiration 将缓存显式设置为永不过期。您想改用Cache.NoSlidingExpiration:

    使用时,此字段将slidingExpiration 参数设置为 TimeSpan.Zero 字段,其常量值为零。缓存的 项目根据absoluteExpiration 参数过期 与Insert 或Add 方法调用关联。

    【讨论】:

    • 在调用插入时,我发现它混淆了 b/c 方法调用的签名希望您同时插入绝对时间和滑动时间。不就是一个吗?
    • 我认为如果页面的缓存值更长,添加页面缓存可能会影响控件缓存可能是相关的。
    【解决方案2】:

    您将 15 分钟传递给滑动到期参数:http://msdn.microsoft.com/en-us/library/05kd8d77.aspx

    上次访问插入对象的时间与该对象过期的时间之间的时间间隔。如果此值等于 20 分钟,则对象将在上次访问后 20 分钟过期并从缓存中删除。如果使用滑动过期,absoluteExpiration 参数必须为 NoAbsoluteExpiration。

    如果您的缓存对象的访问频率高于每 15 分钟一次,则它永远不会过期。

    _cache.Insert(
            cacheKey, 
            userList, 
            null, 
            new TimeSpan(0, 15, 0),
            Cache.NoSlidingExpiration,
            CacheItemPriority.High, 
            null);
    

    现在该项目将在 15 分钟后过期

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2013-09-28
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多