diff --git a/src/FastCache.Cached/CacheManager.cs b/src/FastCache.Cached/CacheManager.cs index 4bd2e9b..844cb5b 100644 --- a/src/FastCache.Cached/CacheManager.cs +++ b/src/FastCache.Cached/CacheManager.cs @@ -320,17 +320,15 @@ internal static void ReportEvictions(uint count) { var now = TimeUtils.Now; var store = CacheStaticHolder.Store; - uint totalRemoved = 0; + var countBefore = store.Count; void CheckAndRemove(KeyValuePair> kvp) { var (key, timestamp) = (kvp.Key, kvp.Value._timestamp); - ref var count = ref totalRemoved; if (now > timestamp) { store.TryRemove(key, out _); - count++; } } @@ -339,7 +337,10 @@ void CheckAndRemove(KeyValuePair> kvp) .AsUnordered() .ForAll(CheckAndRemove); - return totalRemoved; + // Perform dirty evictions count and discard the result if eviction + // happened to overlap with significant amount of insertions. + // The logic that consumes this value does not require precise reports. + return (uint)Math.Max(countBefore - store.Count, 0); } private static async ValueTask ConsiderFullGC()