简体   繁体   English

缓存如何检测复杂的对象更改并有效地更新缓存数据

[英]How caching detect complex object changes and update cache data efficiently

Default ASP.NET use in memory cache so it looks like it use Dictionary<> internally to use key-value pairs. 内存缓存中默认使用ASP.NET,因此它看起来像在内部使用Dictionary <>来使用键值对。 But external caching solutions like Azure cache or others serialize data which makes scenarious different that in memory solution. 但是外部缓存解决方案(例如Azure缓存或其他缓存解决方案)会序列化数据,这使方案的使用方式与内存解决方案有所不同。 At below example, i hold List object with key and i update it. 在下面的示例中,我持有带有键的List对象并对其进行更新。

var users = new List<User>();
HttpContext.Cache.Insert("key", users);
users.Add(new User());//cached list will update itself with this insertion ?
var cachedUsers = (List<User>) HttpContext.Cache["key"];// now i got users list object reference or copied users list without inserted user ?

As i commented on lines, if this scenario use default in memory cache solution of ASP.NET so it is clear that cache holds users list reference but answers are changing if i use external Cache solution like Azure cache solution. 正如我在线上评论的那样,如果这种情况在ASP.NET的内存缓存解决方案中使用默认设置,则很显然缓存可以保存用户列表引用,但是如果我使用外部缓存解决方案(如Azure缓存解决方案),答案就会改变。 What are answers of below questions if external solution is used which serialize objects instead of caching object references ? 如果使用外部解决方案来序列化对象而不是缓存对象引用,以下问题的答案是什么?

Cached list will update itself with that insertion ? 缓存列表是否会随着该插入而更新?

if i get back data from cache,it would be users list object reference or copied users list without inserted user ? 如果我从缓存中取回数据,它将是用户列表对象引用还是复制的用户列表而不插入用户?

In a cache that stores data in-memory, your user list would be updated. 在将数据存储在内存中的缓存中,您的用户列表将被更新。 Obviously in a cache store that serializes the data, your user list would contain the list without the newly added user. 显然,在序列化数据的缓存存储区中,您的用户列表将包含没有新添加用户的列表。

In any way relying on your cache values getting updated after inserting it in your cache is bad practice. 在任何情况下,依靠缓存值在将其插入缓存后进行更新都是不正确的做法。 What if you want to move to a different type of cache later? 如果您以后想移至其他类型的缓存该怎么办? When the data that you're caching changes, you invalidate your cache, and re-populate it. 当您要缓存的数据发生更改时,您会使缓存无效,然后重新填充它。 Don't rely on the implementation. 不要依赖于实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM