Memory Cache or Concurrent Dictionary?
Solution 1:
If you don't need some kind of expiration logic, I would suggest using concurrent collections. You can easily implement a single entry caching mechanism combining ConcurrentDictionary
and Lazy
classes. Here is another link about Lazy and ConcurrentDictionary combination.
If you need your items to expire, then you better use the built-in MemoryCache and implement double-checked locking pattern to guarantee single retrieval of cache items. A ready to go implementation of double checked locking can be found in Locking pattern for proper use of .NET MemoryCache