Skip to content

Commit d1dd48c

Browse files
authored
Merge branch 'main' into users/alexpeck/opt2
2 parents 680e637 + cdc4bce commit d1dd48c

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

BitFaster.Caching.ThroughputAnalysis/CacheFactory.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
using System;
23
using System.Collections.Generic;
34
using System.Data;
45
using BitFaster.Caching.Lfu;
@@ -105,6 +106,35 @@ public ConcurrentLfuFactory(int capacity)
105106
}
106107
}
107108

109+
public class ConcurrentTLfuFactory : ICacheFactory
110+
{
111+
private readonly int capacity;
112+
113+
public ConcurrentTLfuFactory(int capacity)
114+
{
115+
this.capacity = capacity;
116+
}
117+
118+
public string Name => "ConcurrentTLfu";
119+
120+
public DataRow DataRow { get; set; }
121+
122+
public (IScheduler, ICache<long, int>) Create(int threadCount)
123+
{
124+
var scheduler = new BackgroundThreadScheduler();
125+
126+
var cache = new ConcurrentLfuBuilder<long, int>()
127+
.WithCapacity(capacity)
128+
.WithScheduler(scheduler)
129+
.WithConcurrencyLevel(threadCount)
130+
.WithKeyComparer(EqualityComparer<long>.Default)
131+
.WithExpireAfterWrite(TimeSpan.FromHours(1))
132+
.Build();
133+
134+
return (scheduler, cache);
135+
}
136+
}
137+
108138
public class ClassicLruFactory : ICacheFactory
109139
{
110140
private readonly int capacity;

BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuCoreTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using BitFaster.Caching.Lfu;
6+
using BitFaster.Caching.UnitTests.Retry;
67
using FluentAssertions;
78
using Xunit;
89

@@ -85,7 +86,7 @@ public void WhenItemIsUpdatedItIsUpdated()
8586
value.Should().Be(2);
8687
}
8788

88-
[Fact]
89+
[RetryFact]
8990
public void WhenItemDoesNotExistUpdatedAddsItem()
9091
{
9192
lfu.AddOrUpdate(1, 2);

BitFaster.Caching.UnitTests/Lfu/ConcurrentTLfuTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void WhenKeyTypeMismatchTryGetTimeToExpireReturnsFalse()
112112

113113
// policy can expire after write
114114

115-
[Fact]
115+
[RetryFact]
116116
public void WhenItemIsNotExpiredItIsNotRemoved()
117117
{
118118
lfu.GetOrAdd(1, valueFactory.Create);

BitFaster.Caching.UnitTests/Lru/ConcurrentLruAfterAccessTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TimeToLiveIsCtorArg()
4646
this.lru.Policy.ExpireAfterAccess.Value.TimeToLive.Should().Be(timeToLive);
4747
}
4848

49-
[Fact]
49+
[RetryFact]
5050
public void WhenItemIsNotExpiredItIsNotRemoved()
5151
{
5252
lru.GetOrAdd(1, valueFactory.Create);

BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void TimeToLiveIsCtorArg()
4848
this.lru.Policy.ExpireAfterWrite.Value.TimeToLive.Should().Be(timeToLive);
4949
}
5050

51-
[Fact]
51+
[RetryFact]
5252
public void WhenItemIsNotExpiredItIsNotRemoved()
5353
{
5454
lru.GetOrAdd(1, valueFactory.Create);

BitFaster.Caching/Lfu/NodePolicy.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ public void AdvanceTime()
9797
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9898
public void OnRead(TimeOrderNode<K, V> node)
9999
{
100-
var currentExpiry = node.TimeToExpire - current;
101-
node.TimeToExpire = current + expiryCalculator.GetExpireAfterRead(node.Key, node.Value, currentExpiry);
102-
wheel.Reschedule(node);
100+
var oldTte = node.TimeToExpire;
101+
node.TimeToExpire = current + expiryCalculator.GetExpireAfterRead(node.Key, node.Value, node.TimeToExpire - current);
102+
if (oldTte.raw != node.TimeToExpire.raw)
103+
{
104+
wheel.Reschedule(node);
105+
}
103106
}
104107

105108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -113,9 +116,12 @@ public void OnWrite(TimeOrderNode<K, V> node)
113116
}
114117
else
115118
{
116-
var currentExpiry = node.TimeToExpire - current;
117-
node.TimeToExpire = current + expiryCalculator.GetExpireAfterUpdate(node.Key, node.Value, currentExpiry);
118-
wheel.Reschedule(node);
119+
var oldTte = node.TimeToExpire;
120+
node.TimeToExpire = current + expiryCalculator.GetExpireAfterUpdate(node.Key, node.Value, node.TimeToExpire - current);
121+
if (oldTte.raw != node.TimeToExpire.raw)
122+
{
123+
wheel.Reschedule(node);
124+
}
119125
}
120126
}
121127

0 commit comments

Comments
 (0)