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)