C# – MemoryCache.Set()线程安全吗?

MSDN documentation for MemoryCache.Set不幸地没有明确指出它是否是线程安全的。


是的,MemoryCache类is thread safe

System.Runtime.Caching.MemoryCache is threadsafe. Multiple concurrent
threads can read and write a MemoryCache instance. Internally
thread-safety is automatically handled to ensure the cache is updated
in a consistent manner.

What this might be referring to is that data stored within the cache
may itself not be threadsafe. For example if a List is placed in
the cache, and two separate threads both get a reference to the cached
List, the two threads will end up stepping on each other if they
both attempt to update the list simultaneously.


转载注明原文:C# – MemoryCache.Set()线程安全吗? - 代码日志