diff --git a/build/dependencies.props b/build/dependencies.props index 412440cb..b60e9c3c 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -10,6 +10,8 @@ 3.0.0-alpha1-10584 3.0.0-alpha1-10584 3.0.0-alpha1-10584 + 3.0.0-alpha1-10584 + 3.0.0-alpha1-10584 3.0.0-alpha1-10584 3.0.0-alpha1-10584 2.0.9 diff --git a/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs b/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs index f1b39e55..645d6314 100644 --- a/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs +++ b/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Internal; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.Caching.Memory @@ -74,6 +75,8 @@ public int Count // internal for testing internal long Size { get => Interlocked.Read(ref _cacheSize); } + internal ILogger Logger { private get; set; } + private ICollection> EntriesCollection => _entries; /// @@ -341,6 +344,10 @@ private bool UpdateCacheSizeExceedsCapacity(CacheEntry entry) private void TriggerOvercapacityCompaction() { + if (Logger != null) + { + Logger.LogDebug("Overcapacity compaction triggered"); + } // Spawn background thread for compaction ThreadPool.QueueUserWorkItem(s => OvercapacityCompaction((MemoryCache)s), this); } @@ -348,11 +355,22 @@ private void TriggerOvercapacityCompaction() private static void OvercapacityCompaction(MemoryCache cache) { var currentSize = Interlocked.Read(ref cache._cacheSize); + + if (cache.Logger != null) + { + cache.Logger.LogDebug($"Overcapacity compaction executing. Current size {currentSize}"); + } + var lowWatermark = cache._options.SizeLimit * (1 - cache._options.CompactionPercentage); if (currentSize > lowWatermark) { cache.Compact(currentSize - (long)lowWatermark, entry => entry.Size.Value); } + + if (cache.Logger != null) + { + cache.Logger.LogDebug($"Overcapacity compaction executed. New size {Interlocked.Read(ref cache._cacheSize)}"); + } } /// Remove at least the given percentage (0.10 for 10%) of the total entries (or estimated memory?), according to the following policy: diff --git a/src/Microsoft.Extensions.Caching.Memory/Microsoft.Extensions.Caching.Memory.csproj b/src/Microsoft.Extensions.Caching.Memory/Microsoft.Extensions.Caching.Memory.csproj index d3e80111..3f02f780 100644 --- a/src/Microsoft.Extensions.Caching.Memory/Microsoft.Extensions.Caching.Memory.csproj +++ b/src/Microsoft.Extensions.Caching.Memory/Microsoft.Extensions.Caching.Memory.csproj @@ -14,6 +14,7 @@ + diff --git a/test/Microsoft.Extensions.Caching.Memory.Tests/CapacityTests.cs b/test/Microsoft.Extensions.Caching.Memory.Tests/CapacityTests.cs index bccf795b..5118ea52 100644 --- a/test/Microsoft.Extensions.Caching.Memory.Tests/CapacityTests.cs +++ b/test/Microsoft.Extensions.Caching.Memory.Tests/CapacityTests.cs @@ -6,11 +6,12 @@ using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory.Infrastructure; using Microsoft.Extensions.Internal; +using Microsoft.Extensions.Logging.Testing; using Xunit; namespace Microsoft.Extensions.Caching.Memory { - public class CapacityTests + public class CapacityTests : LoggedTestBase { [Fact] public void MemoryDistributedCacheOptionsDefaultsTo200MBSizeLimit() @@ -117,6 +118,8 @@ public async Task DoNotAddIfSizeOverflows() SizeLimit = long.MaxValue }); + cache.Logger = Logger; + var entryOptions = new MemoryCacheEntryOptions { Size = long.MaxValue }; var sem = new SemaphoreSlim(0, 1); entryOptions.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration diff --git a/test/Microsoft.Extensions.Caching.Memory.Tests/Microsoft.Extensions.Caching.Memory.Tests.csproj b/test/Microsoft.Extensions.Caching.Memory.Tests/Microsoft.Extensions.Caching.Memory.Tests.csproj index a31e1940..2cd60fb9 100644 --- a/test/Microsoft.Extensions.Caching.Memory.Tests/Microsoft.Extensions.Caching.Memory.Tests.csproj +++ b/test/Microsoft.Extensions.Caching.Memory.Tests/Microsoft.Extensions.Caching.Memory.Tests.csproj @@ -10,6 +10,7 @@ +