Skip to content

Commit 601f5d3

Browse files
authored
Consolidate maximum time to expire (#508)
1 parent de55559 commit 601f5d3

File tree

6 files changed

+11
-23
lines changed

6 files changed

+11
-23
lines changed

BitFaster.Caching.UnitTests/Lru/AfterAccessPolicyTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ public void WhenTtlIsZeroThrow()
3333
[Fact]
3434
public void WhenTtlIsMaxSetAsMax()
3535
{
36-
#if NETFRAMEWORK
37-
var maxRepresentable = TimeSpan.FromTicks((long)(long.MaxValue / 100.0d)) - TimeSpan.FromTicks(10);
38-
#else
39-
var maxRepresentable = Time.MaxRepresentable;
40-
#endif
41-
var policy = new AfterAccessPolicy<int, int>(maxRepresentable);
42-
policy.TimeToLive.Should().BeCloseTo(maxRepresentable, TimeSpan.FromTicks(20));
36+
var policy = new AfterAccessPolicy<int, int>(Duration.MaxRepresentable);
37+
policy.TimeToLive.Should().BeCloseTo(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20));
4338
}
4439

4540
[Fact]

BitFaster.Caching.UnitTests/Lru/TLruTickCount64PolicyTests .cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ public void WhenTtlIsZeroThrow()
3232
[Fact]
3333
public void WhenTtlIsMaxSetAsMax()
3434
{
35-
var maxRepresentable = TimeSpan.FromTicks(9223372036854769664);
36-
var policy = new TLruLongTicksPolicy<int, int>(maxRepresentable);
37-
policy.TimeToLive.Should().Be(maxRepresentable);
35+
var policy = new TLruLongTicksPolicy<int, int>(Duration.MaxRepresentable);
36+
policy.TimeToLive.Should().BeCloseTo(Duration.MaxRepresentable, TimeSpan.FromMilliseconds(20));
3837
}
3938

4039
[Fact]

BitFaster.Caching/Duration.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ namespace BitFaster.Caching
1818
public readonly struct Duration
1919
{
2020
internal readonly long raw;
21-
#if NETCOREAPP3_0_OR_GREATER
22-
internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks(9223372036854769664);
23-
#else
21+
2422
// MacOS Stopwatch adjustment factor is 100, giving lowest maximum TTL on mac platform - use same upper limit on all platforms for consistency
2523
// this also avoids overflow when multipling long.MaxValue by 1.0
26-
internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks((long)(long.MaxValue * 0.01d));
27-
#endif
24+
internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks((long)(long.MaxValue / 100.0d));
25+
2826
internal Duration(long raw)
2927
{
3028
this.raw = raw;

BitFaster.Caching/Lru/AfterAccessPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace BitFaster.Caching.Lru
2020
/// <param name="timeToLive">The time to live.</param>
2121
public AfterAccessPolicy(TimeSpan timeToLive)
2222
{
23-
if (timeToLive <= TimeSpan.Zero || timeToLive > Time.MaxRepresentable)
24-
Throw.ArgOutOfRange(nameof(timeToLive), $"Value must greater than zero and less than {Time.MaxRepresentable}");
23+
if (timeToLive <= TimeSpan.Zero || timeToLive > Duration.MaxRepresentable)
24+
Throw.ArgOutOfRange(nameof(timeToLive), $"Value must greater than zero and less than {Duration.MaxRepresentable}");
2525

2626
this.timeToLive = Duration.FromTimeSpan(timeToLive);
2727
this.time = new Time();

BitFaster.Caching/Lru/TLruLongTicksPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace BitFaster.Caching.Lru
2020
/// <param name="timeToLive">The time to live.</param>
2121
public TLruLongTicksPolicy(TimeSpan timeToLive)
2222
{
23-
if (timeToLive <= TimeSpan.Zero || timeToLive > Time.MaxRepresentable)
23+
if (timeToLive <= TimeSpan.Zero || timeToLive > Duration.MaxRepresentable)
2424
Throw.ArgOutOfRange(nameof(timeToLive), $"Value must greater than zero and less than {Duration.MaxRepresentable}");
2525

2626
this.timeToLive = Duration.FromTimeSpan(timeToLive);

BitFaster.Caching/Lru/Time.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace BitFaster.Caching.Lru
1+
namespace BitFaster.Caching.Lru
42
{
53
/// <summary>
64
/// During reads, the policy evaluates ShouldDiscard and Touch. To avoid Getting the current time twice
@@ -9,8 +7,6 @@ namespace BitFaster.Caching.Lru
97
/// </summary>
108
internal class Time
119
{
12-
internal static readonly TimeSpan MaxRepresentable = TimeSpan.FromTicks(9223372036854769664);
13-
1410
/// <summary>
1511
/// Gets or sets the last time.
1612
/// </summary>

0 commit comments

Comments
 (0)