Skip to content

Instantly share code, notes, and snippets.

@jmeline
Last active July 27, 2022 19:50
Show Gist options
  • Save jmeline/ab738f5a7abf0a44e8bc938e31cee975 to your computer and use it in GitHub Desktop.
Save jmeline/ab738f5a7abf0a44e8bc938e31cee975 to your computer and use it in GitHub Desktop.
Testing out memory cache vs lazycache
[MemoryDiagnoser(false)]
public class Benchmarks
{
private readonly IAppCache _appCache = new CachingService();
private readonly IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
[Benchmark]
public int GetOrCreate_MemoryCache() =>
_memoryCache.GetOrCreate("set", _ => 100);
[Benchmark]
public int GetOrCreate_LazyCache() =>
_appCache.GetOrAdd("set", _ => 100);
}
using LazyCache;
using Microsoft.Extensions.Caching.Memory;
Console.WriteLine("\nMemoryCache Approach");
MemoryCacheApproach();
Console.WriteLine("\nLazyCache Approach");
LazyCacheApproach();
void MemoryCacheApproach()
{
var cache = new MemoryCache(new MemoryCacheOptions());
var counter = 0;
Parallel.ForEach(Enumerable.Range(1, 50), _ =>
{
var cachedItem = cache.GetOrCreate("key", _ => Interlocked.Increment(ref counter));
Console.Write($"{cachedItem} ");
});
}
void LazyCacheApproach()
{
var cache = new CachingService();
var counter = 0;
Parallel.ForEach(Enumerable.Range(1, 50), _ =>
{
var cachedItem = cache.GetOrAdd("key", _ => Interlocked.Increment(ref counter));
Console.Write($"{cachedItem} ");
});
}
Console.WriteLine();

Notes

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

Notes

dotnet/runtime#36499 https://stackoverflow.com/questions/20149796/memorycache-thread-safety-is-locking-necessary

results

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

Benchmarks

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment