简体   繁体   中英

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. 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.

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.

Thanks.

As @stuartd pointed out in the comment above, I shouldn't doubt that the expiration isn't being applied properly. I ended up checking policy.AbsoluteExpiration for this, and confirmed with a short enough time span that the cache expiration was indeed applying correctly. Thanks again @stuartd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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