Skip to content

Commit

Permalink
task: size of the key locks array should be at least 32, and larger i…
Browse files Browse the repository at this point in the history
…f you have a lot of CPUs
  • Loading branch information
alastairtree committed Sep 21, 2020
1 parent 0140063 commit e0e1fe4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CachingService : IAppCache
{
private readonly Lazy<ICacheProvider> cacheProvider;

private readonly int[] keyLocks = new int[32];
private readonly int[] keyLocks;

public CachingService() : this(DefaultCacheProvider)
{
Expand All @@ -22,12 +22,17 @@ public CachingService() : this(DefaultCacheProvider)
public CachingService(Lazy<ICacheProvider> cacheProvider)
{
this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider));
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
keyLocks = new int[lockCount];
}

public CachingService(Func<ICacheProvider> cacheProviderFactory)
{
if (cacheProviderFactory == null) throw new ArgumentNullException(nameof(cacheProviderFactory));
cacheProvider = new Lazy<ICacheProvider>(cacheProviderFactory);
var lockCount = Math.Max(Environment.ProcessorCount * 8, 32);
keyLocks = new int[lockCount];

}

public CachingService(ICacheProvider cache) : this(() => cache)
Expand Down

1 comment on commit e0e1fe4

@jnyrup
Copy link
Contributor

@jnyrup jnyrup commented on e0e1fe4 Sep 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alastairtree If you'd like to avoid the duplication in both constructors, the second constructor could be written as

public CachingService(Func<ICacheProvider> cacheProviderFactory)
    : this (new Lazy<ICacheProvider>(cacheProviderFactory ?? throw new ArgumentNullException(nameof(cacheProviderFactory))))
{
}

Please sign in to comment.