Skip to content

Commit

Permalink
More reliable concurrency on read optimized LRU cache. (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
emreyigit authored Jun 9, 2023
1 parent 8256b1b commit 6ec01e9
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Hazelcast.Net/Core/ReadOptimizedLruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ReadOptimizedLruCache<TKey, TValue> : IDisposable
internal ConcurrentDictionary<TKey, TimeBasedEntry<TValue>> Cache { get; } = new();

// internal only for tests
private readonly SemaphoreSlim _evicting = new(1, 1);
private int _evicting;
private readonly int _capacity;
private readonly int _threshold;
private int _disposed;
Expand Down Expand Up @@ -117,9 +117,7 @@ public bool TryRemove(TKey key, [MaybeNullWhen(false)] out TValue value)
private void DoEviction()
{
// Don't block if a thread already doing eviction.
if (_evicting.CurrentCount == 0) return;

_evicting.Wait();
if (!_evicting.InterlockedZeroToOne()) return;

try
{
Expand Down Expand Up @@ -150,7 +148,7 @@ private void DoEviction()
}
finally
{
_evicting.Release();
Interlocked.Decrement(ref _evicting);
}
}

Expand All @@ -159,7 +157,6 @@ public void Dispose()
if (_disposed.InterlockedZeroToOne())
{
Cache.Clear();
_evicting.Dispose();
}
}
}

0 comments on commit 6ec01e9

Please # to comment.