Skip to content

Commit

Permalink
address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Aug 19, 2021
1 parent 378bfbe commit 40cbe82
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,15 @@ public void Clear()
{
CheckDisposed();

var oldEntries = Interlocked.Exchange(ref _entries, new ConcurrentDictionary<object, CacheEntry>());
// the following two operations are not atomic change as a whole, but an alternative would be to introduce a global lock for every access to _entries and _cacheSize
ConcurrentDictionary<object, CacheEntry> oldEntries = Interlocked.Exchange(ref _entries, new ConcurrentDictionary<object, CacheEntry>());
Interlocked.Exchange(ref _cacheSize, 0);

foreach (var entry in oldEntries)
{
entry.Value.SetExpired(EvictionReason.Removed);
entry.Value.InvokeEvictionCallbacks();
}
oldEntries.Clear();
}

private void RemoveEntry(CacheEntry entry)
Expand Down Expand Up @@ -335,7 +335,7 @@ private static void ScanForExpiredItems(MemoryCache cache)
{
DateTimeOffset now = cache._lastExpirationScan = cache._options.Clock.UtcNow;

var entries = cache._entries; // Clear() can update the reference in the meantime
ConcurrentDictionary<object, CacheEntry> entries = cache._entries; // Clear() can update the reference in the meantime
foreach (KeyValuePair<object, CacheEntry> item in entries)
{
CacheEntry entry = item.Value;
Expand Down Expand Up @@ -421,7 +421,7 @@ private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntry

// Sort items by expired & priority status
DateTimeOffset now = _options.Clock.UtcNow;
var entries = _entries; // Clear() can update the reference in the meantime
ConcurrentDictionary<object, CacheEntry> entries = _entries; // Clear() can update the reference in the meantime
foreach (KeyValuePair<object, CacheEntry> item in entries)
{
CacheEntry entry = item.Value;
Expand Down

0 comments on commit 40cbe82

Please sign in to comment.