Skip to content

Commit e0e1fe4

Browse files
committed
task: size of the key locks array should be at least 32, and larger if you have a lot of CPUs
1 parent 0140063 commit e0e1fe4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

LazyCache/CachingService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CachingService : IAppCache
1313
{
1414
private readonly Lazy<ICacheProvider> cacheProvider;
1515

16-
private readonly int[] keyLocks = new int[32];
16+
private readonly int[] keyLocks;
1717

1818
public CachingService() : this(DefaultCacheProvider)
1919
{
@@ -22,12 +22,17 @@ public CachingService() : this(DefaultCacheProvider)
2222
public CachingService(Lazy<ICacheProvider> cacheProvider)
2323
{
2424
this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider));
25+
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
26+
keyLocks = new int[lockCount];
2527
}
2628

2729
public CachingService(Func<ICacheProvider> cacheProviderFactory)
2830
{
2931
if (cacheProviderFactory == null) throw new ArgumentNullException(nameof(cacheProviderFactory));
3032
cacheProvider = new Lazy<ICacheProvider>(cacheProviderFactory);
33+
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
34+
keyLocks = new int[lockCount];
35+
3136
}
3237

3338
public CachingService(ICacheProvider cache) : this(() => cache)

0 commit comments

Comments
 (0)