Skip to content

Commit dbacadc

Browse files
author
Alex Peck
committed
fix during warmup
1 parent 661d8c1 commit dbacadc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,44 @@ public void WhenItemIsRemovedRemovedEventIsFired()
678678
removedItems[0].Reason.Should().Be(ItemRemovedReason.Removed);
679679
}
680680

681+
[Fact]
682+
public void WhenItemRemovedFromHotDuringWarmupItIsEagerlyCycledOut()
683+
{
684+
lru.GetOrAdd(1, valueFactory.Create);
685+
686+
lru.TryRemove(1);
687+
688+
lru.GetOrAdd(1, valueFactory.Create);
689+
lru.GetOrAdd(2, valueFactory.Create);
690+
lru.GetOrAdd(3, valueFactory.Create);
691+
692+
lru.WarmCount.Should().Be(0);
693+
lru.ColdCount.Should().Be(0);
694+
}
695+
696+
[Fact]
697+
public void WhenItemRemovedFromHotAfterWarmupItIsEagerlyCycledOut()
698+
{
699+
for (int i = 0; i < lru.Capacity; i++)
700+
{
701+
lru.GetOrAdd(i, valueFactory.Create);
702+
}
703+
704+
lru.Metrics.Value.Evicted.Should().Be(0);
705+
706+
lru.GetOrAdd(-1, valueFactory.Create);
707+
708+
lru.TryRemove(-1);
709+
710+
// fully cycle hot, which is 3 items
711+
lru.GetOrAdd(-2, valueFactory.Create);
712+
lru.GetOrAdd(-3, valueFactory.Create);
713+
lru.GetOrAdd(-4, valueFactory.Create);
714+
715+
// without eager eviction as -1 is purged from hot, a 4th item will pushed out since hot queue is full
716+
lru.Metrics.Value.Evicted.Should().Be(3);
717+
}
718+
681719
[Fact]
682720
public void WhenKeyDoesNotExistTryRemoveReturnsFalse()
683721
{

BitFaster.Caching/Lru/ConcurrentLruCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ private void CycleDuringWarmup(int hotCount)
624624

625625
if (this.hotQueue.TryDequeue(out var item))
626626
{
627+
// special case: removed during warmup
628+
if (item.WasRemoved)
629+
{
630+
return;
631+
}
632+
627633
int count = this.Move(item, ItemDestination.Warm, ItemRemovedReason.Evicted);
628634

629635
// if warm is now full, overflow to cold and mark as warm

0 commit comments

Comments
 (0)