Note that there are some explanatory texts on larger screens.

plurals
  1. POMemoryCache AbsoluteExpiration acting strange
    primarykey
    data
    text
    <p>I'm trying to use a <code>MemoryCache</code> in .net 4.5 to keep track of and automatically update various items, but it seems like no matter what I set as an <code>AbsoluteExpiration</code> it will always only expire in 15 seconds or more.</p> <p>I want the cache items to expire every 5 seconds, but it always expires in at least 15 seconds, and if I move the expiration time out, it will end up being something like 15 seconds + my refresh interval, but never less than 15 seconds.</p> <p>Is there some internal timer resolution that I'm not seeing? I looked through a bit of the reflected <code>System.Runtime.Caching.MemoryCache</code> code and nothing stood out to me, and I haven't been able to find anybody else who has this issue out on the internet.</p> <p>I have a very basic example below that illustrates the problem.</p> <p>What I want is for <code>CacheEntryUpdate</code> to be hit every 5 seconds or so and update with new data, but, as I've said, it only ever gets hit in 15+ seconds.</p> <pre><code>static MemoryCache MemCache; static int RefreshInterval = 5000; protected void Page_Load(object sender, EventArgs e) { if (MemCache == null) MemCache = new MemoryCache("MemCache"); if (!MemCache.Contains("cacheItem")) { var cacheObj = new object(); var policy = new CacheItemPolicy { UpdateCallback = new CacheEntryUpdateCallback(CacheEntryUpdate), AbsoluteExpiration = DateTimeOffset.UtcNow.AddMilliseconds(RefreshInterval) }; var cacheItem = new CacheItem("cacheItem", cacheObj); MemCache.Set("cacheItem", cacheItem, policy); } } private void CacheEntryUpdate(CacheEntryUpdateArguments args) { var cacheItem = MemCache.GetCacheItem(args.Key); var cacheObj = cacheItem.Value; cacheItem.Value = cacheObj; args.UpdatedCacheItem = cacheItem; var policy = new CacheItemPolicy { UpdateCallback = new CacheEntryUpdateCallback(CacheEntryUpdate), AbsoluteExpiration = DateTimeOffset.UtcNow.AddMilliseconds(RefreshInterval) }; args.UpdatedCacheItemPolicy = policy; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload