【问题标题】:System.Web.Caching.Cache doesn't seem to refresh itself on time ?System.Web.Caching.Cache 似乎没有按时刷新自己?
【发布时间】:2011-06-16 22:17:42
【问题描述】:

我在我的网站使用的程序集中使用System.Web.Caching.Cache。 我已将一些密钥过期(绝对过期)设置为 10 秒(仅用于调试)。 我还设置了删除键时的回调。

问题是我看到缓存在大约 20 秒而不是 10 秒后刷新。

我为此使用HttpRuntime.Cache

关于为什么会发生这种情况的任何建议?

我想展示一个代码示例,它可以提供更多信息:

public  void OnUpdate(string key
                      , CacheItemUpdateReason reason
                      , out object expensiveObject
                      , out CacheDependency dependency
                      , out DateTime absoluteExpiration
                      , out TimeSpan slidingExpiration)
{
    using (StreamWriter sw = new StreamWriter(@"C:\temp\foo.txt",true))
    {
        sw.WriteLine("Updated Cache at " + DateTime.UtcNow); 
    }
    expensiveObject = 11;
    dependency = null;
    absoluteExpiration = DateTime.UtcNow.AddSeconds(3);
    slidingExpiration = Cache.NoSlidingExpiration;
}
protected void Page_Load(object sender, EventArgs e)
{
    log.WriteInfo("Updated Cache", MethodBase.GetCurrentMethod());
    Page.Cache.Insert("foo", (object)11, null, DateTime.UtcNow.AddSeconds(10), Cache.NoSlidingExpiration, new CacheItemUpdateCallback(OnUpdate));
}

在这里,我使用了Page.Cache。更新应该是每 3 秒。实际上它每 40 秒执行一次,如下面的打印输出所示:

Updated Cache at 1/28/2011 1:38:20 AM Updated Cache at 1/28/2011 1:38:40 AM Updated Cache at 1/28/2011 1:39:00 AM Updated Cache at 1/28/2011 1:39:20 AM Updated Cache at 1/28/2011 1:39:40 AM Updated Cache at 1/28/2011 1:40:00 AM Updated Cache at 1/28/2011 1:40:20 AM Updated Cache at 1/28/2011 1:40:40 AM Updated Cache at 1/28/2011 1:41:00 AM Updated Cache at 1/28/2011 1:41:20 AM Updated Cache at 1/28/2011 1:41:40 AM Updated Cache at 1/28/2011 1:42:00 AM Updated Cache at 1/28/2011 1:42:20 AM Updated Cache at 1/28/2011 1:42:40 AM

可能是什么问题?

【问题讨论】:

  • 你能发布你的 Cache.Add() 语句
  • @djeeg:我刚刚添加了这个示例代码的问题。你可以看到。

标签: asp.net caching system.web.caching


【解决方案1】:

内部缓存过期计时器仅每 20 秒触发一次。

我反映到System.Web.Caching.CacheExpires

但后来发现它已经在 SO

Changing frequency of ASP.NET cache item expiration?

【讨论】:

  • 上帝保佑你!我朋友!你已经解决了整晚不睡觉的问题,试图尝试那件事。这正是我一直在寻找的答案。奇怪的是他们没有在文档上写这个。例如,当我们真的需要在 50 秒后更新项目时,我们能做什么?基本上,您要说的是(我已经阅读了您推荐的内容)是我们必须在 20 秒的倍数内使用缓存?比如:20、40、60等等?
【解决方案2】:

您可以使用在PokeIn 库中实现的PCache 类。非常好的是,免费版对这个课程没有限制。它有很多功能,而且比 System.Web.Caching.Cache 类要好得多..

此外,链接上还有一个示例项目。您可以使用 PCache 将 TimerInterval 更改为 6 秒。

【讨论】:

    猜你喜欢
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    相关资源
    最近更新 更多