From e0e1fe4f89e38582ec0d6997bbdb6fe28cee9700 Mon Sep 17 00:00:00 2001 From: Alastair Crabtree Date: Mon, 21 Sep 2020 21:23:47 +0100 Subject: [PATCH] task: size of the key locks array should be at least 32, and larger if you have a lot of CPUs --- LazyCache/CachingService.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index 84d5bf8..389ddcd 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -13,7 +13,7 @@ public class CachingService : IAppCache { private readonly Lazy cacheProvider; - private readonly int[] keyLocks = new int[32]; + private readonly int[] keyLocks; public CachingService() : this(DefaultCacheProvider) { @@ -22,12 +22,17 @@ public CachingService() : this(DefaultCacheProvider) public CachingService(Lazy cacheProvider) { this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider)); + var lockCount = Math.Max(Environment.ProcessorCount * 8, 32); + keyLocks = new int[lockCount]; } public CachingService(Func cacheProviderFactory) { if (cacheProviderFactory == null) throw new ArgumentNullException(nameof(cacheProviderFactory)); cacheProvider = new Lazy(cacheProviderFactory); + var lockCount = Math.Max(Environment.ProcessorCount * 8, 32); + keyLocks = new int[lockCount]; + } public CachingService(ICacheProvider cache) : this(() => cache)