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
Dispose `_resetCacheToken` in MemoryCacheBase on teardown (#817)
Copilotbe22638f7bbd
diff --git a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.csindex a328ed801..cf4e54c61 100644--- a/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs+++ b/src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs@@ -10,7 +10,7 @@ namespace Grand.Infrastructure.Caching;/// <summary>/// Represents a manager for memory caching/// </summary>-public class MemoryCacheBase : ICacheBase+public class MemoryCacheBase : ICacheBase, IDisposable{#region Ctor@@ -29,7 +29,7 @@ public class MemoryCacheBase : ICacheBaseprivate readonly IMediator _mediator;private readonly CacheConfig _cacheConfig;- private static CancellationTokenSource _resetCacheToken = new();+ private CancellationTokenSource _resetCacheToken = new();protected readonly ConcurrentDictionary<string, SemaphoreSlim> CacheEntries = new();@@ -144,6 +144,20 @@ public class MemoryCacheBase : ICacheBasereturn Task.CompletedTask;}+ /// <summary>+ /// Disposes the current reset token once the cache manager itself is no longer needed+ /// (e.g. on application shutdown). Unlike <see cref="Clear" />, there is no concurrent writer+ /// activity at this point, so disposing here does not risk an ObjectDisposedException.+ /// Safe to call more than once.+ /// </summary>+ public void Dispose()+ {+ if (_resetCacheToken.IsCancellationRequested) return;++ _resetCacheToken.Cancel();+ _resetCacheToken.Dispose();+ }+#endregion#region Utilitiesdiff --git a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.csindex 78943c1ee..dd8c7742b 100644--- a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs+++ b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs@@ -202,4 +202,35 @@ public class MemoryCacheBaseTestsAssert.IsNull(captured, $"Clear raced a concurrent write: {captured}");}++ [TestMethod]+ public async Task Dispose_OneInstance_DoesNotBreakAnotherInstance()+ {+ using var serviceProvider = new ServiceCollection()+ .AddMemoryCache()+ .BuildServiceProvider();+ var firstCache = serviceProvider.GetRequiredService<IMemoryCache>();+ var secondCache = serviceProvider.GetRequiredService<IMemoryCache>();+ var first = new MemoryCacheBase(firstCache, new Mock<IMediator>().Object, _config);+ var second = new MemoryCacheBase(secondCache, new Mock<IMediator>().Object, _config);++ first.Dispose();++ var result = await second.GetAsync("key", () => Task.FromResult("value"));++ Assert.AreEqual("value", result);+ }++ [TestMethod]+ public void Dispose_CalledTwice_DoesNotThrow()+ {+ using var serviceProvider = new ServiceCollection()+ .AddMemoryCache()+ .BuildServiceProvider();+ var cacheManager = new MemoryCacheBase(serviceProvider.GetRequiredService<IMemoryCache>(),+ new Mock<IMediator>().Object, _config);++ cacheManager.Dispose();+ cacheManager.Dispose();+ }}\ No newline at end of file