我在HttpContext.Cache中存儲一個整數值,並且從現在起絕對過期時間爲5分鐘。然而,在等待6分鐘(或更長時間)之後,整數值仍然在高速緩存中(即,即使絕對超時已經過去也不會被除去)。這裏是我使用的代碼:ASP.net緩存絕對過期不起作用
public void UpdateCountFor(string remoteIp)
{
// only returns true the first time its run
// after that the value is still in the Cache
// even after the absolute expiration has passed
// so after that this keeps returning false
if (HttpContext.Current.Cache[remoteIp] == null)
{
// nothing for this ip in the cache so add the ip as a key with a value of 1
var expireDate = DateTime.Now.AddMinutes(5);
// I also tried:
// var expireDate = DateTime.UtcNow.AddMinutes(5);
// and that did not work either.
HttpContext.Current.Cache.Insert(remoteIp, 1, null, expireDate, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
else
{
// increment the existing value
HttpContext.Current.Cache[remoteIp] = ((int)HttpContext.Current.Cache[remoteIp]) + 1;
}
}
我第一次運行UpdateCountFor(「127.0.0.1」)它插入1與鍵「127.0.0.1」的高速緩存和5分鐘從現在絕對過期如預期。隨後的每一次運行都會增加緩存中的值。但是,等待10分鐘後,它會繼續增加緩存中的值。該值永不過期,永遠不會從緩存中刪除。這是爲什麼?
這是我的理解,絕對到期時間意味着該項目將被刪除大約在那個時候。難道我做錯了什麼?我誤解了什麼嗎?
我期待值5分鐘後從緩存中刪除,但它留在那裏,直到我重建項目。
這是所有在我的本地機器上運行的.NET 4.0。
您的web.config中是否有任何奇怪的緩存配置設置? – 2011-05-06 16:49:51
不,我有默認的web.config – smoak 2011-05-06 17:37:56