Skip to content

Commit 553b452

Browse files
author
Alex Peck
committed
trim cold
1 parent 17e6898 commit 553b452

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,25 @@ public void WhenWarmThenClearedIsWarmIsReset()
953953
lru.Count.Should().Be(capacity.Hot + capacity.Warm + capacity.Cold);
954954
}
955955

956+
[Fact]
957+
public void WhenWarmThenTrimIsWarmIsReset()
958+
{
959+
for (int i = 0; i < 20; i++)
960+
{
961+
lru.GetOrAdd(i, k => k.ToString());
962+
}
963+
964+
lru.Trim(6);
965+
lru.Count.Should().Be(3);
966+
967+
for (int i = 0; i < 20; i++)
968+
{
969+
lru.GetOrAdd(i, k => k.ToString());
970+
}
971+
972+
lru.Count.Should().Be(capacity.Hot + capacity.Warm + capacity.Cold);
973+
}
974+
956975
[Fact]
957976
public void WhenItemsAreDisposableClearDisposesItemsOnRemove()
958977
{

BitFaster.Caching/Lru/ConcurrentLruCore.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ public void AddOrUpdate(K key, V value)
423423
public void Clear()
424424
{
425425
this.TrimLiveItems(itemsRemoved: 0, this.Count, ItemRemovedReason.Cleared);
426-
Volatile.Write(ref this.isWarm, false);
427426
}
428427

429428
/// <summary>
@@ -532,6 +531,11 @@ private void TrimLiveItems(int itemsRemoved, int itemCount, ItemRemovedReason re
532531
trimWarmAttempts++;
533532
}
534533
}
534+
535+
if (Volatile.Read(ref this.counter.warm) < this.capacity.Warm)
536+
{
537+
Volatile.Write(ref this.isWarm, false);
538+
}
535539
}
536540

537541
private void TrimWarmOrHot(ItemRemovedReason reason)

0 commit comments

Comments
 (0)