我在我的網站使用的程序集中使用了System.Web.Caching.Cache
。 我已經設置了一些關鍵到期(絕對過期)爲10秒(僅用於調試)。 我還設置了一個回調鍵刪除。System.Web.Caching.Cache似乎並沒有按時刷新自己?
的問題是,我看到緩存像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));
}
這裏,i用於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 2011-01-28 01:25:57
@djeeg:我剛剛添加了這個示例代碼的問題。你可以看到。 – Yossi 2011-01-28 01:46:15