使用System.Runtime.Caching,但是当我去找回它,它是空?当我、它是、System、Runtime

2023-09-03 22:50:48 作者:美好的那么不真实

我有一个C#WPF应用程序,我尝试使用新的System.Runtime.Caching实现缓存。当我添加了一些缓存,我再检查,并确认它的存在,它是。然而,当我重新启动应用程序,它经历了从高速缓存。

那么,在下面的例子中,输出总会被发现。我希望它是是第一次运行时输出找到了,但是后来的时间(未来10天),它会输出当时已经present,但事实并非如此。我已经通过调试器中运行,并确认每次重新启动应用程序时,高速缓存[MYDATA]将等于空。

我是什么做错了吗?谢谢!

 词典<类型,字典<字符串,对象>> _CachedObjects = NULL;
ObjectCache缓存= MemoryCache.Default;

_CachedObjects =缓存[MYDATA]作为字典<类型,字典<字符串,对象>&GT ;;
如果(_CachedObjects == NULL)
{
     _CachedObjects =新字典<类型,字典<字符串,对象>>();

    // code,填补_CachedObjects

     CacheItemPolicy政策=新CacheItemPolicy();
     policy.AbsoluteExpiration = DateTimeOffset.Now.AddDays(10.0);

     cache.Set(MYDATA,_CachedObjects,政策);

     如果(高速缓存[MYDATA]!= NULL)
          Console.WriteLine(发现);
}
其他
     Console.WriteLine(当时已经present);
 

解决方案

的MemoryCache 对象保持在非易失性存储器,所以你需要扩展 ObjectCache 来存储在文件系统或一些其它非易失性存储机构对象

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.

之 内部类 lambda表达式 API Math System Runtime BigDecima

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

 
精彩推荐