我在我的网站使用的程序集中使用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
可能是什么问题?
答案 0 :(得分:5)
内部缓存过期计时器仅每20秒触发一次。
我反映到System.Web.Caching.CacheExpires
但后来发现它已经在SO
了答案 1 :(得分:0)
您可以使用在PokeIn库中实现的PCache类。非常好的是,免费版本对此课程没有限制。它有许多功能,远远优于System.Web.Caching.Cache类..
此外,链接上还有一个示例项目。您可以使用PCache将TimerInterval更改为6秒。