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
250 changes: 125 additions & 125 deletions BitFaster.Caching/Lru/AfterReadStopwatchPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,127 +1,127 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace BitFaster.Caching.Lru
{
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Implement an expire after access policy.
/// </summary>
/// <remarks>
/// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us.
/// </remarks>
public readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
{
private readonly long timeToLive;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace BitFaster.Caching.Lru
{
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Implement an expire after access policy.
/// </summary>
/// <remarks>
/// This class measures time using Stopwatch.GetTimestamp() with a resolution of ~1us.
/// </remarks>
internal readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
{
private readonly long timeToLive;
private readonly Time time;

///<inheritdoc/>
public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive);

/// <summary>
/// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live.
/// </summary>
/// <param name="timeToLive">The time to live.</param>
public AfterAccessLongTicksPolicy(TimeSpan timeToLive)
{
this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive);
this.time = new Time();
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public LongTickCountLruItem<K, V> CreateItem(K key, V value)
{
return new LongTickCountLruItem<K, V>(key, value, Stopwatch.GetTimestamp());
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LongTickCountLruItem<K, V> item)
{
item.TickCount = this.time.Last;
item.WasAccessed = true;
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(LongTickCountLruItem<K, V> item)
{
item.TickCount = Stopwatch.GetTimestamp();
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldDiscard(LongTickCountLruItem<K, V> item)
{
this.time.Last = Stopwatch.GetTimestamp();
if (this.time.Last - item.TickCount > this.timeToLive)
{
return true;
}

return false;
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanDiscard()
{
return true;
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteHot(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}

if (item.WasAccessed)
{
return ItemDestination.Warm;
}

return ItemDestination.Cold;
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteWarm(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}

if (item.WasAccessed)
{
return ItemDestination.Warm;
}

return ItemDestination.Cold;
}

///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteCold(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}

if (item.WasAccessed)
{
return ItemDestination.Warm;
}

return ItemDestination.Remove;
}

}
#endif
}
///<inheritdoc/>
public TimeSpan TimeToLive => StopwatchTickConverter.FromTicks(timeToLive);
/// <summary>
/// Initializes a new instance of the TLruLongTicksPolicy class with the specified time to live.
/// </summary>
/// <param name="timeToLive">The time to live.</param>
public AfterAccessLongTicksPolicy(TimeSpan timeToLive)
{
this.timeToLive = StopwatchTickConverter.ToTicks(timeToLive);
this.time = new Time();
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public LongTickCountLruItem<K, V> CreateItem(K key, V value)
{
return new LongTickCountLruItem<K, V>(key, value, Stopwatch.GetTimestamp());
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LongTickCountLruItem<K, V> item)
{
item.TickCount = this.time.Last;
item.WasAccessed = true;
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(LongTickCountLruItem<K, V> item)
{
item.TickCount = Stopwatch.GetTimestamp();
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldDiscard(LongTickCountLruItem<K, V> item)
{
this.time.Last = Stopwatch.GetTimestamp();
if (this.time.Last - item.TickCount > this.timeToLive)
{
return true;
}
return false;
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanDiscard()
{
return true;
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteHot(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}
if (item.WasAccessed)
{
return ItemDestination.Warm;
}
return ItemDestination.Cold;
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteWarm(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}
if (item.WasAccessed)
{
return ItemDestination.Warm;
}
return ItemDestination.Cold;
}
///<inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemDestination RouteCold(LongTickCountLruItem<K, V> item)
{
if (this.ShouldDiscard(item))
{
return ItemDestination.Remove;
}
if (item.WasAccessed)
{
return ItemDestination.Warm;
}
return ItemDestination.Remove;
}
}
#endif
}
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/AfterReadTickCount64Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </remarks>
public readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
internal readonly struct AfterAccessLongTicksPolicy<K, V> : IItemPolicy<K, V, LongTickCountLruItem<K, V>>
{
private readonly long timeToLive;
private readonly Time time;
Expand Down