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
Add SetAsync methods to ICacheBase and MemoryCacheBase
supportddbca3e56533
diff --git a/src/Core/Grand.Infrastructure/Caching/ICacheBase.cs b/src/Core/Grand.Infrastructure/Caching/ICacheBase.csindex 18c2f0ee5..af893b2a1 100644--- a/src/Core/Grand.Infrastructure/Caching/ICacheBase.cs+++ b/src/Core/Grand.Infrastructure/Caching/ICacheBase.cs@@ -7,6 +7,8 @@ namespace Grand.Infrastructure.Caching{Task<T> GetAsync<T>(string key, Func<Task<T>> acquire);Task<T> GetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime);+ Task<T> SetAsync<T>(string key, Func<Task<T>> acquire);+ Task<T> SetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime);T Get<T>(string key, Func<T> acquire);T Get<T>(string key, Func<T> acquire, int cacheTime);Task RemoveAsync(string key, bool publisher = true);diff --git a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.csindex 5763ff84b..86cdd2b2e 100644--- a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs+++ b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs@@ -17,7 +17,7 @@ namespace Grand.Infrastructure.Cachingprivate readonly IMemoryCache _cache;private readonly IMediator _mediator;private readonly CacheConfig _cacheConfig;-+private bool _disposed;private static CancellationTokenSource _resetCacheToken = new();@@ -63,6 +63,27 @@ namespace Grand.Infrastructure.Cachingreturn cacheEntry;}+ public virtual Task<T> SetAsync<T>(string key, Func<Task<T>> acquire)+ {+ return SetAsync(key, acquire, _cacheConfig.DefaultCacheTimeMinutes);+ }++ public virtual async Task<T> SetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime)+ {+ SemaphoreSlim semaphore = CacheEntries.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));+ await semaphore.WaitAsync();+ try+ {+ var cacheEntry = await acquire();+ _cache.Set(key, cacheEntry, GetMemoryCacheEntryOptions(cacheTime));+ return cacheEntry;+ }+ finally+ {+ semaphore.Release();+ }+ }+public virtual T Get<T>(string key, Func<T> acquire){return Get(key, acquire, _cacheConfig.DefaultCacheTimeMinutes);@@ -86,7 +107,7 @@ namespace Grand.Infrastructure.Cachingsemaphore.Release();}return cacheEntry;- }+ }public virtual Task RemoveAsync(string key, bool publisher = true){@@ -166,7 +187,7 @@ namespace Grand.Infrastructure.Caching{if (reason != EvictionReason.Replaced)CacheEntries.TryRemove(key.ToString(), out var _);-+}#endregiondiff --git a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.csindex 886186af7..8492fbdd0 100644--- a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs+++ b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs@@ -1,4 +1,5 @@-using Grand.Infrastructure.Caching;+using Grand.Business.Core.Commands.Checkout.Orders;+using Grand.Infrastructure.Caching;using Grand.Infrastructure.Configuration;using MediatR;using Microsoft.Extensions.Caching.Memory;@@ -97,5 +98,47 @@ namespace Grand.Infrastructure.Tests.Cachingvar result = _memoryCache.Get("key1");Assert.IsNull(result);}++ [TestMethod()]+ public async Task SetAsync_Key_NotExist_ShouldSetCacheEntry()+ {+ // Arrange+ var key = "testKey";+ var cacheTime = 60;+ var cacheEntry = "testValue";++ var acquireMock = new Mock<Func<Task<string>>>();+ acquireMock.Setup(a => a.Invoke()).ReturnsAsync(cacheEntry);++ // Act+ var result = await _service.SetAsync(key, acquireMock.Object, cacheTime);++ // Assert+ acquireMock.Verify(a => a.Invoke(), Times.Once);+ var cacheResult = _memoryCache.Get(key);+ Assert.IsNotNull(cacheResult);+ Assert.AreEqual(cacheEntry, cacheResult);+ }+ [TestMethod()]+ public async Task SetAsync_Key_Exist_ShouldSetCacheEntry()+ {+ // Arrange+ var key = "testKey";+ var cacheTime = 60;+ var cacheEntry = "testValue";++ var acquireMock = new Mock<Func<Task<string>>>();+ acquireMock.Setup(a => a.Invoke()).ReturnsAsync(cacheEntry);+ _service.Get<string>(key, () => { return "fakeValue"; }, 1);++ // Act+ var result = await _service.SetAsync(key, acquireMock.Object, cacheTime);++ // Assert+ acquireMock.Verify(a => a.Invoke(), Times.Once);+ var cacheResult = _memoryCache.Get(key);+ Assert.IsNotNull(cacheResult);+ Assert.AreEqual(cacheEntry, cacheResult);+ }}}\ No newline at end of file