System.Runtime.Caching.MemoryCache strange behavio

2019-07-21 08:31发布

Have this question up on the MSDN forums too (http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/0a666d5e-9bda-47bd-8dd3-98d32eb5fe60/?prof=required), thought I'd get an opinion of folks here :

Here's the output of my immediate window :

(EmployeeCache as MemoryCache).Add("test", new Object(),DateTimeOffset.Now.AddMinutes(10));
true
(EmployeeCache as  MemoryCache).GetCount()
0
(EmployeeCache as  MemoryCache)
{<namespace>.CustomCache}
[<namespace>.CustomCache]: {[<namespace>.CustomCache}
base {System.Runtime.Caching.ObjectCache}: {<namespace>.CustomCache}
CacheMemoryLimit: 1887436800
DefaultCacheCapabilities: InMemoryProvider | CacheEntryChangeMonitors | AbsoluteExpirations |   SlidingExpirations | CacheEntryUpdateCallback | CacheEntryRemovedCallback
Name: "keyname"
PhysicalMemoryLimit: 99
PollingInterval: {00:02:00}

Under what conditions will adding to a MemoryCache return true, but an object will not be cached? (The call to GetCount and the one following it are made immediatley after adding to the cache)

标签: .net caching
1条回答
姐就是有狂的资本
2楼-- · 2019-07-21 08:49

The easiest way to get into this situation is to Add on a MemoryCache object for which Dispose has been called. You can check this by using the Visual Studio debugger to see the private _disposed property of the MemoryCache object. Hover over a variable that references the cache, use a quick watch, or use the immediate window.

查看更多
登录 后发表回答