Skip to content

Commit 87d0ad2

Browse files
committed
Reclaim entries that expire during mismatching comparisons
1 parent 351d2cb commit 87d0ad2

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎src/Foundatio/Caching/InMemoryCacheClient.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,19 @@ private TResult UpdateLease<TResult>(string key, Func<CacheEntry, (CacheEntry? E
394394
var result = UpdateEntry(key, current =>
395395
{
396396
reclaimed = false;
397-
if (current is null || update(current) is not { } decision)
397+
if (current is null)
398398
return (current, missResult);
399399

400+
var decision = update(current);
401+
400402
// A lease that expired while it was being compared no longer belongs to the caller: reclaim it
401403
if (current.IsExpired)
402404
{
403405
reclaimed = true;
404406
return (null, missResult);
405407
}
406408

407-
return decision;
409+
return decision ?? (current, missResult);
408410
});
409411

410412
if (reclaimed)

‎tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,8 @@ public async Task RemoveIfEqualAsync_WhenEntryExpiresDuringComparison_ReturnsFal
10631063
[InlineData(true, true, false)]
10641064
[InlineData(false, false, true)]
10651065
[InlineData(false, true, true)]
1066+
[InlineData(true, false, true)]
1067+
[InlineData(true, true, true)]
10661068
public async Task RemoveIfEqualAsync_WhenEntryExpires_ReclaimsMemoryAndNotifies(bool expiresDuringComparison, bool matches, bool recentMaintenance)
10671069
{
10681070
// Arrange

0 commit comments

Comments
 (0)