Skip to content

Commit 116476f

Browse files
authored
TLruTickCount64Policy uses 64 bit time (#337)
* 64 * rem dead code * throw helper ---------
1 parent 96c5345 commit 116476f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ public class TLruTickCount64PolicyTests
1414
{
1515
private readonly TLruTickCount64Policy<int, int> policy = new TLruTickCount64Policy<int, int>(TimeSpan.FromSeconds(10));
1616

17+
[Fact]
18+
public void WhenTtlIsTimeSpanMaxThrow()
19+
{
20+
Action constructor = () => { new TLruTickCount64Policy<int, int>(TimeSpan.MaxValue); };
21+
22+
constructor.Should().Throw<ArgumentOutOfRangeException>();
23+
}
24+
25+
[Fact]
26+
public void WhenTtlIsZeroThrow()
27+
{
28+
Action constructor = () => { new TLruTickCount64Policy<int, int>(TimeSpan.MaxValue); };
29+
30+
constructor.Should().Throw<ArgumentOutOfRangeException>();
31+
}
32+
33+
[Fact]
34+
public void WhenTtlIsMaxSetAsMax()
35+
{
36+
var maxRepresentable = TimeSpan.FromTicks(9223372036854769664);
37+
var policy = new TLruTickCount64Policy<int, int>(maxRepresentable);
38+
policy.TimeToLive.Should().Be(maxRepresentable);
39+
}
40+
1741
[Fact]
1842
public void TimeToLiveShouldBeTenSecs()
1943
{

BitFaster.Caching/Lru/TlruTickCount64Policy.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace BitFaster.Caching.Lru
1515
/// </remarks>
1616
public readonly struct TLruTickCount64Policy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
1717
{
18-
private readonly int timeToLive;
18+
private readonly long timeToLive;
1919

2020
///<inheritdoc/>
2121
public TimeSpan TimeToLive => TimeSpan.FromMilliseconds(timeToLive);
@@ -26,7 +26,13 @@ namespace BitFaster.Caching.Lru
2626
/// <param name="timeToLive">The time to live.</param>
2727
public TLruTickCount64Policy(TimeSpan timeToLive)
2828
{
29-
this.timeToLive = (int)timeToLive.TotalMilliseconds;
29+
TimeSpan maxRepresentable = TimeSpan.FromTicks(9223372036854769664);
30+
if (timeToLive < TimeSpan.Zero || timeToLive > maxRepresentable)
31+
{
32+
Ex.ThrowArgOutOfRange(nameof(timeToLive), $"Value must greater than zero and less than {maxRepresentable}");
33+
}
34+
35+
this.timeToLive = (long)timeToLive.TotalMilliseconds;
3036
}
3137

3238
///<inheritdoc/>
@@ -47,14 +53,14 @@ public void Touch(LongTickCountLruItem<K, V> item)
4753
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4854
public void Update(LongTickCountLruItem<K, V> item)
4955
{
50-
item.TickCount = Environment.TickCount;
56+
item.TickCount = Environment.TickCount64;
5157
}
5258

5359
///<inheritdoc/>
5460
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5561
public bool ShouldDiscard(LongTickCountLruItem<K, V> item)
5662
{
57-
if (Environment.TickCount - item.TickCount > this.timeToLive)
63+
if (Environment.TickCount64 - item.TickCount > this.timeToLive)
5864
{
5965
return true;
6066
}

0 commit comments

Comments
 (0)