Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Further improvements with memorycache
support342e1c2b388a
diff --git a/src/Core/Grand.Infrastructure/Caching/CacheEntryExtensions.cs b/src/Core/Grand.Infrastructure/Caching/CacheEntryExtensions.csdeleted file mode 100644index 3e8440593..000000000--- a/src/Core/Grand.Infrastructure/Caching/CacheEntryExtensions.cs+++ /dev/null@@ -1,18 +0,0 @@-using Microsoft.Extensions.Caching.Memory;-using System.Collections.Concurrent;--namespace Grand.Infrastructure.Caching-{- public static class CacheEntryExtensions- {- public static ICacheEntry SetKey(this ICacheEntry entry, ConcurrentDictionary<string, ICacheEntry> _cacheEntries)- {- _cacheEntries.AddOrUpdate(entry.Key.ToString(), entry, (o, cacheEntry) =>- {- cacheEntry.Value = entry;- return cacheEntry;- });- return entry;- }- }-}diff --git a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.csindex 9e2f43f89..6f4bdfb76 100644--- a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs+++ b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs@@ -20,7 +20,7 @@ namespace Grand.Infrastructure.Cachingprivate bool _disposed;private static CancellationTokenSource _resetCacheToken = new();- protected readonly ConcurrentDictionary<string, ICacheEntry> _cacheEntries = new();+ protected readonly ConcurrentDictionary<string, SemaphoreSlim> _cacheEntries = new();#endregion@@ -41,14 +41,27 @@ namespace Grand.Infrastructure.Cachingreturn GetAsync(key, acquire, CommonHelper.CacheTimeMinutes);}- public virtual Task<T> GetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime)+ public virtual async Task<T> GetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime){- return _cache.GetOrCreateAsync(key, entry =>+ T cacheEntry;+ if (!_cache.TryGetValue(key, out cacheEntry)){- entry.SetOptions(GetMemoryCacheEntryOptions(cacheTime));- entry.SetKey(_cacheEntries);- return acquire();- });+ SemaphoreSlim slock = _cacheEntries.GetOrAdd(key, k => new SemaphoreSlim(1, 1));+ await slock.WaitAsync();+ try+ {+ if (!_cache.TryGetValue(key, out cacheEntry))+ {+ cacheEntry = await acquire();+ _cache.Set(key, cacheEntry, GetMemoryCacheEntryOptions(cacheTime));+ }+ }+ finally+ {+ slock.Release();+ }+ }+ return cacheEntry;}public virtual T Get<T>(string key, Func<T> acquire)@@ -58,12 +71,25 @@ namespace Grand.Infrastructure.Cachingpublic virtual T Get<T>(string key, Func<T> acquire, int cacheTime){- return _cache.GetOrCreate(key, entry =>+ T cacheEntry;+ if (!_cache.TryGetValue(key, out cacheEntry)){- entry.SetOptions(GetMemoryCacheEntryOptions(cacheTime));- entry.SetKey(_cacheEntries);- return acquire();- });+ SemaphoreSlim slock = _cacheEntries.GetOrAdd(key, k => new SemaphoreSlim(1, 1));+ slock.Wait();+ try+ {+ if (!_cache.TryGetValue(key, out cacheEntry))+ {+ cacheEntry = acquire();+ _cache.Set(key, cacheEntry, GetMemoryCacheEntryOptions(cacheTime));+ }+ }+ finally+ {+ slock.Release();+ }+ }+ return cacheEntry;}public virtual Task RemoveAsync(string key, bool publisher = true)