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
2 changes: 1 addition & 1 deletion .github/workflows/gate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Test
name: Build and Test (windows-latest)

on:
push:
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/mac-gate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build and Test (macos-latest)

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
6 changes: 3 additions & 3 deletions BitFaster.Caching.UnitTests/Lru/TlruLongTicksPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void TouchUpdatesItemWasAccessed()
public void WhenItemIsExpiredShouldDiscardIsTrue()
{
var item = this.policy.CreateItem(1, 2);
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks;
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));

this.policy.ShouldDiscard(item).Should().BeTrue();
}
Expand All @@ -58,7 +58,7 @@ public void WhenItemIsExpiredShouldDiscardIsTrue()
public void WhenItemIsNotExpiredShouldDiscardIsFalse()
{
var item = this.policy.CreateItem(1, 2);
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(9).Ticks;
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(9));

this.policy.ShouldDiscard(item).Should().BeFalse();
}
Expand Down Expand Up @@ -107,7 +107,7 @@ private LongTickCountLruItem<int, int> CreateItem(bool wasAccessed, bool isExpir

if (isExpired)
{
item.TickCount = Stopwatch.GetTimestamp() - TimeSpan.FromSeconds(11).Ticks;
item.TickCount = Stopwatch.GetTimestamp() - TLruLongTicksPolicy<int, int>.ToTicks(TimeSpan.FromSeconds(11));
}

return item;
Expand Down
9 changes: 8 additions & 1 deletion BitFaster.Caching/Lru/TlruLongTicksPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ namespace BitFaster.Caching.Lru
/// </remarks>
public readonly struct TLruLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
{
// On some platforms (e.g. MacOS), stopwatch and timespan have different resolution
private static readonly double stopwatchAdjustmentFactor = Stopwatch.Frequency / (double)TimeSpan.TicksPerSecond;
private readonly long timeToLive;

public TLruLongTicksPolicy(TimeSpan timeToLive)
{
this.timeToLive = timeToLive.Ticks;
this.timeToLive = ToTicks(timeToLive);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -94,5 +96,10 @@ public ItemDestination RouteCold(LongTickCountLruItem<K, V> item)

return ItemDestination.Remove;
}

public static long ToTicks(TimeSpan timespan)
{
return (long)(timespan.Ticks * stopwatchAdjustmentFactor);
}
}
}