What is the difference between MemoryCache.Add and MemoryCache.Set?

I read the MSDN documentation but didn’t really understand.

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

Is it correct?

+72
memorycache
Jan 15 '12 at 8:40
source share
1 answer

Add does nothing (returns false ) if there is already a value for this key. Set if necessary, inserts or updates.

Remove + Add will leave a gap in the middle when another thread requesting this key does not receive any hint ( Set does not; swap is usually atomic); as such, while Set has the same end result as Remove + Add , the difference in the mechanism is important, as this can affect other callers.

For example Add :

Return value

Type: System.Boolean true if the insert was successful, or false if the cache already has an entry with the same key as the key.

+117
Jan 15 2018-12-12T00:
source share



All Articles