Yos*_*ssi 2 asp.net caching system.web.caching
我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));
}
Run Code Online (Sandbox Code Playgroud)
在这里,我用过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
可能是什么问题呢 ?
内部缓存过期计时器仅每20秒触发一次.
我反思过来 System.Web.Caching.CacheExpires
但后来发现它已经在SO了