开发者

Using System.Runtime.Caching, But When I Go To Retrieve it, it's null?

I've got a C# WPF application and I'm trying to implement Caching using the new System.Runtime.Caching. When I add something to the Cache, I then check it and confirm it is there, which it is. However, when I restart the application, it is gone from the Cache.

So, in the below example, the output will always be "Found It". What I expect it to be is that the first time it is run it outputs "Found It", but all subsequent times (for the next 10 days) it would output "Was already present", but it doesn't. I've run it through the debugger and confirmed that every time you restart the application, cache["MYDATA"] will equal null.

What am I doing wrong? Thanks!

Dictionary<Type, Dictionary<string, object>> _CachedObjects = null;
ObjectCache cache = MemoryCache.Default;

_CachedObjects = cache["MYDATA"] as Dictionary<Type, Dictionary<string, object>>;
if (_CachedObjects == null)
{
     _CachedObjects = new Dictionary<Type, Dictionary<string, object>>();

    //code that fills 开发者_Python百科_CachedObjects

     CacheItemPolicy policy = new CacheItemPolicy();
     policy.AbsoluteExpiration = DateTimeOffset.Now.AddDays(10.0);

     cache.Set("MYDATA", _CachedObjects, policy);

     if(cache["MYDATA"] != null)
          Console.WriteLine("Found It");
}
else
     Console.WriteLine("Was already present");


The MemoryCache object is maintained in volatile memory so you will need to extend ObjectCache object to store in the file system or some other non-volatile storage mechanism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜