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
15 changes: 9 additions & 6 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,10 @@ public void WhenValueExpiresItIsDisposed()
lruOfDisposable.GetOrAdd(i, disposableValueFactory.Create);
}

disposableValueFactory.Items[0].IsDisposed.Should().BeFalse();
disposableValueFactory.Items[1].IsDisposed.Should().BeFalse();

disposableValueFactory.Items[2].IsDisposed.Should().BeTrue();
disposableValueFactory.Items[0].IsDisposed.Should().BeTrue();

disposableValueFactory.Items[1].IsDisposed.Should().BeFalse();
disposableValueFactory.Items[2].IsDisposed.Should().BeFalse();
disposableValueFactory.Items[3].IsDisposed.Should().BeFalse();
disposableValueFactory.Items[4].IsDisposed.Should().BeFalse();
disposableValueFactory.Items[5].IsDisposed.Should().BeFalse();
Expand Down Expand Up @@ -598,8 +597,8 @@ public void WhenValueEvictedItemRemovedEventIsFired()

removedItems.Count.Should().Be(2);

removedItems[0].Key.Should().Be(3);
removedItems[0].Value.Should().Be(4);
removedItems[0].Key.Should().Be(1);
removedItems[0].Value.Should().Be(2);
removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted);

removedItems[1].Key.Should().Be(4);
Expand Down Expand Up @@ -1016,6 +1015,8 @@ public void WhenTrimCountIsMoreThanCapacityThrows()
[InlineData(9, new int[] { })]
public void WhenColdItemsExistTrimRemovesExpectedItemCount(int trimCount, int[] expected)
{
Warmup();

// initial state:
// Hot = 9, 8, 7
// Warm = 3, 2, 1
Expand Down Expand Up @@ -1109,6 +1110,8 @@ public void WhenHotItemsExistTrimRemovesExpectedItemCount(int itemCount, int[] e
[InlineData(9, new int[] { })]
public void WhenColdItemsAreTouchedTrimRemovesExpectedItemCount(int trimCount, int[] expected)
{
Warmup();

// initial state:
// Hot = 9, 8, 7
// Warm = 3, 2, 1
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public void WhenValueEvictedItemRemovedEventIsFired()

removedItems.Count.Should().Be(2);

removedItems[0].Key.Should().Be(3);
removedItems[0].Value.Should().Be(4);
removedItems[0].Key.Should().Be(1);
removedItems[0].Value.Should().Be(2);
removedItems[0].Reason.Should().Be(ItemRemovedReason.Evicted);

removedItems[1].Key.Should().Be(4);
Expand Down
18 changes: 6 additions & 12 deletions BitFaster.Caching/Lru/ConcurrentLruCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,20 +587,14 @@ private void CycleDuringWarmup(int hotCount)

if (this.hotQueue.TryDequeue(out var item))
{
// always move to warm until it is full
if (Volatile.Read(ref this.counter.warm) < this.capacity.Warm)
{
// If there is a race, we will potentially add multiple items to warm. Guard by cycling the queue.
int warmCount = this.Move(item, ItemDestination.Warm, ItemRemovedReason.Evicted);
CycleWarm(warmCount);
}
else
int count = this.Move(item, ItemDestination.Warm, ItemRemovedReason.Evicted);

// if warm is now full, overflow to cold and mark as warm
if (count > this.capacity.Warm)
{
// Else mark isWarm and move items to cold.
// If there is a race, we will potentially add multiple items to cold. Guard by cycling the queue.
Volatile.Write(ref this.isWarm, true);
int coldCount = this.Move(item, ItemDestination.Cold, ItemRemovedReason.Evicted);
CycleCold(coldCount);
count = LastWarmToCold();
ConstrainCold(count, ItemRemovedReason.Evicted);
}
}
else
Expand Down