Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,18 +676,37 @@ public void WhenItemDoesNotExistTryUpdateIsFalse()

[Fact]
public void WhenClearedCacheIsEmpty()
{
{
cache.GetOrAdd(1, k => k);
cache.GetOrAdd(2, k => k);
cache.DoMaintenance();

cache.GetOrAdd(2, k => k);

cache.Clear();
cache.DoMaintenance();

cache.Count.Should().Be(0);
cache.TryGet(1, out var _).Should().BeFalse();
}

[Fact]
public void WhenBackgroundMaintenanceRepeatedReadThenClearResultsInEmpty()
{
cache = new ConcurrentLfu<int, int>(1, 40, new BackgroundThreadScheduler(), EqualityComparer<int>.Default);

var overflow = 0;
for (var a = 0; a < 200; a++)
{
for (var i = 0; i < 40; i++)
{
cache.GetOrAdd(i, k => k);
}

cache.Clear();
overflow += cache.Count;
}

// there should be no iteration of the loop where count != 0
overflow.Should().Be(0);
}

[Fact]
public void TrimRemovesNItems()
{
Expand Down
7 changes: 6 additions & 1 deletion BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,12 @@ private static void TakeCandidatesInLruOrder(LfuNodeList<K, V> lru, List<LfuNode

while (candidates.Count < itemCount && curr != null)
{
candidates.Add(curr);
// LRUs can contain items that are already removed, skip those
if (!curr.WasRemoved)
{
candidates.Add(curr);
}

curr = curr.Next;
}
}
Expand Down