Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<!-- https://stackoverflow.com/a/59916801/131345 -->
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/AfterAccessPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public LongTickCountLruItem<K, V> CreateItem(K key, V value)
public void Touch(LongTickCountLruItem<K, V> item)
{
item.TickCount = this.time.Last;
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/DiscretePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Touch(LongTickCountLruItem<K, V> item)
var currentExpiry = new Duration(item.TickCount - this.time.Last);
var newExpiry = expiry.GetExpireAfterRead(item.Key, item.Value, currentExpiry);
item.TickCount = this.time.Last + newExpiry.raw;
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
23 changes: 17 additions & 6 deletions BitFaster.Caching/Lru/LruItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class LruItem<K, V>
{
private V data;

private volatile bool wasAccessed;
private volatile bool wasRemoved;
private bool wasAccessed;
private bool wasRemoved;

// only used when V is a non-atomic value type to prevent torn reads
private int sequence;
Expand Down Expand Up @@ -72,17 +72,28 @@ public V Value
/// </summary>
public bool WasAccessed
{
get => this.wasAccessed;
set => this.wasAccessed = value;
get => Volatile.Read(ref this.wasAccessed);
set => Volatile.Write(ref this.wasAccessed, value);
}

/// <summary>
/// Gets or sets a value indicating whether the item was removed.
/// </summary>
public bool WasRemoved
{
get => this.wasRemoved;
set => this.wasRemoved = value;
get => Volatile.Read(ref this.wasRemoved);
set => Volatile.Write(ref this.wasRemoved, value);
}

/// <summary>
/// Marks the item as accessed, if it was not already accessed.
/// </summary>
public void MarkAccessed()
{
if (!Volatile.Read(ref this.wasAccessed))
{
Volatile.Write(ref this.wasAccessed, true);
}
}

internal V SeqLockRead()
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/LruPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TLruLongTicksPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LongTickCountLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LongTickCountLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TlruDateTimePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TimeStampedLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(TimeStampedLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TlruTicksPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TickCountLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(TickCountLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
Loading