From c87b059cbdfaced4db2b17f73bc8ff361a4df7a5 Mon Sep 17 00:00:00 2001 From: Memoyu Date: Thu, 12 Oct 2023 10:55:17 +0800 Subject: [PATCH 1/3] fix: no check expires when get cache count in memory cache --- src/EasyCaching.InMemory/Internal/InMemoryCaching.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs index 408f7fa8..3cfe1d27 100644 --- a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs +++ b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs @@ -49,8 +49,8 @@ public void Clear(string prefix = "") public int GetCount(string prefix = "") { return string.IsNullOrWhiteSpace(prefix) - ? _memory.Count - : _memory.Count(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)); + ? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow).Count() + : _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow).Count(); } internal void RemoveExpiredKey(string key) From 9ece7ff563785f9c3394225b5ab330a44b12c194 Mon Sep 17 00:00:00 2001 From: Memoyu Date: Thu, 12 Oct 2023 10:58:55 +0800 Subject: [PATCH 2/3] styles: format code #497 --- src/EasyCaching.InMemory/Internal/InMemoryCaching.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs index 3cfe1d27..913ebb10 100644 --- a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs +++ b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs @@ -283,7 +283,7 @@ public int RemoveByPattern(string searchKey, SearchKeyPattern searchPattern) public IEnumerable GetAllKeys(string prefix) { return _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow) - .Select(x=> x.Key).ToList(); + .Select(x => x.Key).ToList(); } private static bool FilterByPattern(string key, string searchKey, SearchKeyPattern searchKeyPattern) @@ -314,10 +314,10 @@ public IDictionary> GetAll(IEnumerable keys) public IDictionary> GetAll(string prefix = "") { - var values = string.IsNullOrEmpty(prefix) - ? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow) + var values = string.IsNullOrEmpty(prefix) + ? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow) : _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow); - + return values.ToDictionary(k => k.Key, v => new CacheValue(v.GetValue(_options.EnableReadDeepClone), true)); } From e99a3533697de88e305bc2cdac826ffd2b0424d0 Mon Sep 17 00:00:00 2001 From: Memoyu Date: Thu, 12 Oct 2023 22:30:03 +0800 Subject: [PATCH 3/3] feat: add test --- .../CachingTests/MemoryCachingProviderTest.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs index 899b2589..0795a6ea 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs @@ -155,6 +155,21 @@ public void Evicted_Event_Should_Trigger_When_GetExpiredItems() _provider.Get(key1); System.Threading.Thread.Sleep(500); } + + [Fact] + public async void Issues497_GetCountAsync_Check_Expires_Test() + { + for (int i = 0; i < 9; i++) + { + await _provider.SetAsync(Guid.NewGuid().ToString(), $"value-{i}", TimeSpan.FromSeconds(5)); + } + + Assert.Equal(9, await _provider.GetCountAsync()); + + await Task.Delay(5000); + + Assert.Equal(0, await _provider.GetCountAsync()); + } } public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest