How can I detach the object reference on MemoryCache

前端 未结 5 571
轮回少年
轮回少年 2021-01-04 03:26

I\'m currently trying out the new MemoryCache in .Net 4 to cache a few bits of data in one of our apps. The trouble I have is the objects are updated and the ca

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 04:03

    You can do it easier if you deserialize and serialize again and get your cache object "By Value".

    You can do it like this with Newtonsoft lib (just get it from NuGet)

    var cacheObj = HttpRuntime.Cache.Get(CACHEKEY);
    var json = JsonConvert.SerializeObject(cacheObj);
    var byValueObj = JsonConvert.DeserializeObject>(json);
    return byValueObj;
    

提交回复
热议问题