简体   繁体   English

在C#中确认缓存过期日期/时间

[英]Confirming the cache expiration date/time in C#

I have the following code set up to create a cache. 我设置了以下代码来创建缓存。 In debugging, I add cache to the watch list, and step through the code. 在调试中,我将cache添加到监视列表,并逐步执行代码。 But when I check the properties of cache , in PollingInterval (only place I can see anything related to time) it shows Days as 0, and minutes as 2. I've tried this with both AbsoluteExpiration and SlidingExpiration. 但是,当我检查cache的属性时,在PollingInterval(只能看到与时间有关的任何地方)中,它将Days显示为0,将minutes显示为2。我已经使用AbsoluteExpiration和SlidingExpiration进行了尝试。

ObjectCache cache = MemoryCache.Default;
CacheItemPolicy policy = new CacheItemPolicy();
//policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(24.0);
//policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30.0);
policy.SlidingExpiration = TimeSpan.FromDays(2.0);

cache.Set("Clients", clients, policy);

I've even tried using DateTimeOffset.Now.AddHours(24.0) as the third parameter for cache.Set(...) instead of CacheItemPolicy. 我什至尝试使用DateTimeOffset.Now.AddHours(24.0)作为cache.Set(...)的第三个参数,而不是CacheItemPolicy。

Thanks. 谢谢。

As @stuartd pointed out in the comment above, I shouldn't doubt that the expiration isn't being applied properly. 正如@stuartd在上面的评论中指出的那样,我不应该怀疑到期未正确应用。 I ended up checking policy.AbsoluteExpiration for this, and confirmed with a short enough time span that the cache expiration was indeed applying correctly. 我为此结束了对policy.AbsoluteExpiration的检查,并在足够短的时间范围内确认了缓存过期确实已正确应用。 Thanks again @stuartd. 再次感谢@stuartd。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM