Skip to content

Commit 151be2b

Browse files
authored
Fix no check expires when get cache count in memory cache (#498)
* fix: no check expires when get cache count in memory cache * styles: format code #497 * feat: add test
1 parent 8f2c764 commit 151be2b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/EasyCaching.InMemory/Internal/InMemoryCaching.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void Clear(string prefix = "")
4949
public int GetCount(string prefix = "")
5050
{
5151
return string.IsNullOrWhiteSpace(prefix)
52-
? _memory.Count
53-
: _memory.Count(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
52+
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow).Count()
53+
: _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow).Count();
5454
}
5555

5656
internal void RemoveExpiredKey(string key)
@@ -283,7 +283,7 @@ public int RemoveByPattern(string searchKey, SearchKeyPattern searchPattern)
283283
public IEnumerable<string> GetAllKeys(string prefix)
284284
{
285285
return _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow)
286-
.Select(x=> x.Key).ToList();
286+
.Select(x => x.Key).ToList();
287287
}
288288

289289
private static bool FilterByPattern(string key, string searchKey, SearchKeyPattern searchKeyPattern)
@@ -314,10 +314,10 @@ public IDictionary<string, CacheValue<T>> GetAll<T>(IEnumerable<string> keys)
314314

315315
public IDictionary<string, CacheValue<T>> GetAll<T>(string prefix = "")
316316
{
317-
var values = string.IsNullOrEmpty(prefix)
318-
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow)
317+
var values = string.IsNullOrEmpty(prefix)
318+
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow)
319319
: _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow);
320-
320+
321321
return values.ToDictionary(k => k.Key, v => new CacheValue<T>(v.GetValue<T>(_options.EnableReadDeepClone), true));
322322
}
323323

test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs

+15
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ public void Evicted_Event_Should_Trigger_When_GetExpiredItems()
155155
_provider.Get<string>(key1);
156156
System.Threading.Thread.Sleep(500);
157157
}
158+
159+
[Fact]
160+
public async void Issues497_GetCountAsync_Check_Expires_Test()
161+
{
162+
for (int i = 0; i < 9; i++)
163+
{
164+
await _provider.SetAsync(Guid.NewGuid().ToString(), $"value-{i}", TimeSpan.FromSeconds(5));
165+
}
166+
167+
Assert.Equal(9, await _provider.GetCountAsync());
168+
169+
await Task.Delay(5000);
170+
171+
Assert.Equal(0, await _provider.GetCountAsync());
172+
}
158173
}
159174

160175
public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest

0 commit comments

Comments
 (0)