diff --git a/BitFaster.Caching/Lru/AfterReadStopwatchPolicy.cs b/BitFaster.Caching/Lru/AfterReadStopwatchPolicy.cs index fcb0a793..b508d907 100644 --- a/BitFaster.Caching/Lru/AfterReadStopwatchPolicy.cs +++ b/BitFaster.Caching/Lru/AfterReadStopwatchPolicy.cs @@ -1,127 +1,127 @@ -using System; -using System.Diagnostics; -using System.Runtime.CompilerServices; - -namespace BitFaster.Caching.Lru -{ -#if !NETCOREAPP3_0_OR_GREATER - /// - /// Implement an expire after access policy. - /// - /// - /// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us. - /// - public readonly struct AfterAccessLongTicksPolicy : IItemPolicy> - { - private readonly long timeToLive; +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; + +namespace BitFaster.Caching.Lru +{ +#if !NETCOREAPP3_0_OR_GREATER + /// + /// Implement an expire after access policy. + /// + /// + /// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us. + /// + internal readonly struct AfterAccessLongTicksPolicy : IItemPolicy> + { + private readonly long timeToLive; private readonly Time time; - /// - public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive); - - /// - /// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live. - /// - /// The time to live. - public AfterAccessLongTicksPolicy(TimeSpan timeToLive) - { - this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive); - this.time = new Time(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public LongTickCountLruItem CreateItem(K key, V value) - { - return new LongTickCountLruItem(key, value, Stopwatch.GetTimestamp()); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Touch(LongTickCountLruItem item) - { - item.TickCount = this.time.Last; - item.WasAccessed = true; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Update(LongTickCountLruItem item) - { - item.TickCount = Stopwatch.GetTimestamp(); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool ShouldDiscard(LongTickCountLruItem item) - { - this.time.Last = Stopwatch.GetTimestamp(); - if (this.time.Last - item.TickCount > this.timeToLive) - { - return true; - } - - return false; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool CanDiscard() - { - return true; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ItemDestination RouteHot(LongTickCountLruItem item) - { - if (this.ShouldDiscard(item)) - { - return ItemDestination.Remove; - } - - if (item.WasAccessed) - { - return ItemDestination.Warm; - } - - return ItemDestination.Cold; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ItemDestination RouteWarm(LongTickCountLruItem item) - { - if (this.ShouldDiscard(item)) - { - return ItemDestination.Remove; - } - - if (item.WasAccessed) - { - return ItemDestination.Warm; - } - - return ItemDestination.Cold; - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ItemDestination RouteCold(LongTickCountLruItem item) - { - if (this.ShouldDiscard(item)) - { - return ItemDestination.Remove; - } - - if (item.WasAccessed) - { - return ItemDestination.Warm; - } - - return ItemDestination.Remove; - } - - } -#endif -} + /// + public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive); + + /// + /// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live. + /// + /// The time to live. + public AfterAccessLongTicksPolicy(TimeSpan timeToLive) + { + this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive); + this.time = new Time(); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public LongTickCountLruItem CreateItem(K key, V value) + { + return new LongTickCountLruItem(key, value, Stopwatch.GetTimestamp()); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Touch(LongTickCountLruItem item) + { + item.TickCount = this.time.Last; + item.WasAccessed = true; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Update(LongTickCountLruItem item) + { + item.TickCount = Stopwatch.GetTimestamp(); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool ShouldDiscard(LongTickCountLruItem item) + { + this.time.Last = Stopwatch.GetTimestamp(); + if (this.time.Last - item.TickCount > this.timeToLive) + { + return true; + } + + return false; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool CanDiscard() + { + return true; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ItemDestination RouteHot(LongTickCountLruItem item) + { + if (this.ShouldDiscard(item)) + { + return ItemDestination.Remove; + } + + if (item.WasAccessed) + { + return ItemDestination.Warm; + } + + return ItemDestination.Cold; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ItemDestination RouteWarm(LongTickCountLruItem item) + { + if (this.ShouldDiscard(item)) + { + return ItemDestination.Remove; + } + + if (item.WasAccessed) + { + return ItemDestination.Warm; + } + + return ItemDestination.Cold; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ItemDestination RouteCold(LongTickCountLruItem item) + { + if (this.ShouldDiscard(item)) + { + return ItemDestination.Remove; + } + + if (item.WasAccessed) + { + return ItemDestination.Warm; + } + + return ItemDestination.Remove; + } + + } +#endif +} diff --git a/BitFaster.Caching/Lru/AfterReadTickCount64Policy.cs b/BitFaster.Caching/Lru/AfterReadTickCount64Policy.cs index 92360dc1..d12c5ea3 100644 --- a/BitFaster.Caching/Lru/AfterReadTickCount64Policy.cs +++ b/BitFaster.Caching/Lru/AfterReadTickCount64Policy.cs @@ -13,7 +13,7 @@ namespace BitFaster.Caching.Lru /// than both Stopwatch.GetTimestamp and DateTime.UtcNow. However, resolution is lower (typically /// between 10-16ms), vs 1us for Stopwatch.GetTimestamp. /// - public readonly struct AfterAccessLongTicksPolicy : IItemPolicy> + internal readonly struct AfterAccessLongTicksPolicy : IItemPolicy> { private readonly long timeToLive; private readonly Time time;