diff --git a/src/Kiota.Builder/Caching/DocumentCachingProvider.cs b/src/Kiota.Builder/Caching/DocumentCachingProvider.cs index 7bb280c983..7a0a93fafc 100644 --- a/src/Kiota.Builder/Caching/DocumentCachingProvider.cs +++ b/src/Kiota.Builder/Caching/DocumentCachingProvider.cs @@ -1,13 +1,12 @@ using System; -using System.Collections.Concurrent; using System.IO; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; +using AsyncKeyedLock; using Microsoft.Extensions.Logging; -using NeoSmart.AsyncLock; namespace Kiota.Builder.Caching; @@ -39,8 +38,7 @@ private async Task GetDocumentInternalAsync(Uri documentUri, string inte { var hashedUrl = BitConverter.ToString((HashAlgorithm.Value ?? throw new InvalidOperationException("unable to get hash algorithm")).ComputeHash(Encoding.UTF8.GetBytes(documentUri.ToString()))).Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase); var target = Path.Combine(Path.GetTempPath(), Constants.TempDirectoryName, "cache", intermediateFolderName, hashedUrl, fileName); - var currentLock = _locks.GetOrAdd(target, _ => new AsyncLock()); - using (await currentLock.LockAsync(token).ConfigureAwait(false)) + using (await _locks.LockAsync(target, token).ConfigureAwait(false)) {// if multiple clients are being updated for the same description, we'll have concurrent download of the file without the lock if (!File.Exists(target) || couldNotDelete) return await DownloadDocumentFromSourceAsync(documentUri, target, accept, token).ConfigureAwait(false); @@ -67,7 +65,11 @@ private async Task GetDocumentInternalAsync(Uri documentUri, string inte return await GetDocumentInternalAsync(documentUri, intermediateFolderName, fileName, couldNotDelete, accept, token).ConfigureAwait(false); } } - private static readonly ConcurrentDictionary _locks = new(StringComparer.OrdinalIgnoreCase); + private static readonly AsyncKeyedLocker _locks = new(o => + { + o.PoolSize = 20; + o.PoolInitialFill = 1; + }); private async Task DownloadDocumentFromSourceAsync(Uri documentUri, string target, string? accept, CancellationToken token) { Logger.LogDebug("cache file {CacheFile} not found, downloading from {Url}", target, documentUri); diff --git a/src/Kiota.Builder/Kiota.Builder.csproj b/src/Kiota.Builder/Kiota.Builder.csproj index 2fef97de62..75cca50566 100644 --- a/src/Kiota.Builder/Kiota.Builder.csproj +++ b/src/Kiota.Builder/Kiota.Builder.csproj @@ -35,6 +35,7 @@ $(NoWarn);CS8785;NU5048;NU5104;CA1724;CA1055;CA1848;CA1308;CA1822 + @@ -47,7 +48,6 @@ -