What's the difference between MemoryCache.Add and MemoryCache.Set?

狂风中的少年 提交于 2020-01-30 19:28:06

问题


I read the MSDN documentation but didn't really understand it.

I believe that the behavior of Set is "replace existing, or add" (atomically).

Is that correct?


回答1:


Add does nothing (returns false) if there is already a value for that key. Set does an insert or update, as necessary.

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.

For example of Add:

Return Value

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.



来源:https://stackoverflow.com/questions/8868486/whats-the-difference-between-memorycache-add-and-memorycache-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!