From e65af7c5e10d39ee33c80216698aa372cf7631f1 Mon Sep 17 00:00:00 2001 From: billhong Date: Wed, 10 Jan 2024 15:15:00 +0800 Subject: [PATCH] temp --- .../DefaultInMemoryCachingProvider.Async.cs | 32 ++++++++++--------- .../DefaultInMemoryCachingProvider.cs | 31 ++++++++++-------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs index 5a0b8c9a..2ea45023 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs @@ -27,28 +27,30 @@ public override async Task> BaseGetAsync(string cacheKey, Func< ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - var result = _cache.Get(cacheKey); - if (result.HasValue) + bool getLock; + do { - if (_options.EnableLogging) - _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); + var result = _cache.Get(cacheKey); + if (result.HasValue) + { + if (_options.EnableLogging) + _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); - CacheStats.OnHit(); + CacheStats.OnHit(); - return result; - } + return result; + } - CacheStats.OnMiss(); + CacheStats.OnMiss(); - if (_options.EnableLogging) - _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); + if (_options.EnableLogging) + _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); - if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs))) - { - //wait for some ms - await Task.Delay(_options.SleepMs, cancellationToken); - return await GetAsync(cacheKey, dataRetriever, expiration); + getLock = _cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)); + if (!getLock) + await Task.Delay(_options.SleepMs, cancellationToken); } + while (!getLock); try { diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs index 4a18578b..227a872e 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs @@ -117,27 +117,30 @@ public override CacheValue BaseGet(string cacheKey, Func dataRetriever, ////mutex key //Lock(cacheKey); - var result = _cache.Get(cacheKey); - if (result.HasValue) + bool getLock; + do { - if (_options.EnableLogging) - _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); + var result = _cache.Get(cacheKey); + if (result.HasValue) + { + if (_options.EnableLogging) + _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); - CacheStats.OnHit(); + CacheStats.OnHit(); - return result; - } + return result; + } - CacheStats.OnMiss(); + CacheStats.OnMiss(); - if (_options.EnableLogging) - _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); + if (_options.EnableLogging) + _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); - if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs))) - { - System.Threading.Thread.Sleep(_options.SleepMs); - return Get(cacheKey, dataRetriever, expiration); + getLock = _cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs)); + if (!getLock) + System.Threading.Thread.Sleep(_options.SleepMs); } + while (!getLock); try {