MemoryCache from microsoft isn't thread safe. If we're hitting it a lot at the same time, there is no guarantee that if one thread updates cache that the other thread will see the changes. It probably won't hurt us but something to consider.
https://github.com/alastairtree/LazyCache is a solution that adds support for lazy and atomic operations built upon microsoft's memory cache
dotnet/runtime#36499 https://stackoverflow.com/questions/20149796/memorycache-thread-safety-is-locking-necessary
MemoryCache Approach (Not thread safe/deterministic/atomic)
13 8 1 6 2 11 5 7 3 12 9 4 10 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
LazyCache Approach
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Method | Mean | Error | StdDev | Allocated |
---|---|---|---|---|
GetOrCreate_MemoryCache | 69.00 ns | 1.016 ns | 0.849 ns | - |
GetOrCreate_LazyCache | 156.98 ns | 0.635 ns | 0.563 ns | 96 B |
MemoryCache is quick and has no memory allocations.