MemoryCache.Add和MemoryCache.Set有什么区别? [英] What's the difference between MemoryCache.Add and MemoryCache.Set?
问题描述
我阅读了 MSDN文档,但实际上并没有了解它.
I read the MSDN documentation but didn't really understand it.
我相信Set
的行为是替换现有的或添加的"(原子上).
I believe that the behavior of Set
is "replace existing, or add" (atomically).
对吗?
推荐答案
Add
如果该键已经有值,则不执行任何操作(返回false
). Set
根据需要进行插入或更新.
Add
does nothing (returns false
) if there is already a value for that key. Set
does an insert or update, as necessary.
Remove
+ Add
在另一个查询该键的线程没有任何线索的情况下会在中间留下一个空白(Set
不会;交换通常是原子的);因此,尽管Set
与Remove
+ Add
具有相同的最终结果,但机制上的差异很重要,因为它可能会影响其他调用者.
Remove
+ Add
would leave a gap in the middle when another thread querying that key would get no clue (Set
does not; the swap is typically atomic); as such, while Set
has the same end result as Remove
+ Add
, the mechanism difference is important since it could impact other callers.
例如添加:
返回值
类型:System.Boolean如果插入成功,则为true;如果高速缓存中已有与键相同的项,则为false.
Type: System.Boolean true if insertion succeeded, or false if there is an already an entry in the cache that has the same key as key.
这篇关于MemoryCache.Add和MemoryCache.Set有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!