问题
为什么System.Web.Caching.Cache onUpdateCallback不尊重absolutionExpiration时间?
上下文
我正在尝试创建一个缓存策略,在给定的间隔之后自动刷新自己。但是,我希望缓存在后台线程上刷新,而不是逐出,然后等待新的用户请求强制刷新缓存。一旦达到绝对过期,我选择使用onUpdateCallback重新填充缓存。
代码
我创建了一个示例Controller来说明这一点:
public class ExampleController : Controller
{
private const string CacheKey = "Test";
//Simple function to get current time
private readonly Func<string> GetTime =
() => DateTime.Now.ToString("HH:mm:ss fff");
//Cache for 5 seconds
private readonly TimeSpan _cacheDuration = new TimeSpan(0, 0, 5);
private static bool _cacheWarmed = false;
private void WarmCache()
{
if (_cacheWarmed)
return;
_cacheWarmed = true;
//Cache current time
HttpContext.Cache
.Insert(
key: CacheKey,
value: GetTime(),
dependencies: null,
absoluteExpiration: DateTime.Now.Add(_cacheDuration),
slidingExpiration: Cache.NoSlidingExpiration,
//On cache eviction - cache current time (ie eviction time)
onUpdateCallback:
(string s,
CacheItemUpdateReason reason,
out object cachedItem,
out CacheDependency dependency,
out DateTime expiration,
out TimeSpan slidingExpiration) =>
{
expiration = DateTime.Now.Add(_cacheDuration);
cachedItem = GetTime();
dependency = null;
slidingExpiration = Cache.NoSlidingExpiration;
});
}
public ActionResult GetCachedTime()
{
WarmCache();
return new ContentResult
{
Content = (string)HttpContext.Cache.Get(CacheKey)
};
}
}还有一个简单的Html,带有一些javascript,可以一次又一次地调用该方法并输出结果:
<script type="text/javascript">
$(document).ready(function() {
var myFunc = function()
{
var startTime = new Date().getTime();
$.ajax({
url: "/example/GetCachedTime"
}).done(function(data) {
var currentTime = new Date();
var msg =
currentTime.toLocaleTimeString() +
": [" +
data +
"] - Executed in " +
(currentTime.getTime() - startTime) +
" ms <br/>";
$("#container").append(msg);
console.log(msg);
});
};
var intervalHandle = setInterval(myFunc, 2000);
myFunc();
});
</script>结果
为了简洁而截断:
3:45:52 PM: [15:45:40 387] - Executed in 30 ms
3:45:55 PM: [15:45:40 387] - Executed in 4 ms
3:45:57 PM: [15:45:40 387] - Executed in 4 ms
3:45:59 PM: [15:45:40 387] - Executed in 4 ms
3:46:01 PM: [15:46:00 399] - Executed in 4 ms
3:46:03 PM: [15:46:00 399] - Executed in 4 ms
(duplicates removed - :04 - :18)
3:46:19 PM: [15:46:00 399] - Executed in 3 ms
3:46:21 PM: [15:46:20 406] - Executed in 4 ms
3:46:23 PM: [15:46:20 406] - Executed in 3 ms
(duplicates removed - :24 - :38)
3:46:39 PM: [15:46:20 406] - Executed in 3 ms
3:46:41 PM: [15:46:40 418] - Executed in 3 ms
3:46:43 PM: [15:46:40 418] - Executed in 3 ms
(duplicates removed - :44 - :58)
3:46:59 PM: [15:46:40 418] - Executed in 3 ms
3:47:01 PM: [15:47:00 419] - Executed in 3 ms
3:47:03 PM: [15:47:00 419] - Executed in 3 ms 尽管我为20 5**!**设置了超时,但方法被缓存为秒。
我尝试过更改缓存持续时间:
_cacheDuration = new TimeSpan(0, 0, 1) -不变_cacheDuration = new TimeSpan(0, 0, 10) -不变_cacheDuration = new TimeSpan(0, 0, 20) -不变_cacheDuration = new TimeSpan(0, 0, 25) -缓存40秒问题复述
这是怎么回事?为什么缓存持续时间不受尊重?还是我没有正确设置缓存?
发布于 2014-12-08 23:52:00
ASP.NET缓存中有一个内部计时器,每20秒检查一次过期时间。5秒太短,没有调整缓存轮询周期,一些用户已经这样做了。
https://stackoverflow.com/questions/25448761
复制相似问题