Skip to content

Commit

Permalink
TwoLayerCache concurrency bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
petriashev committed Jun 20, 2024
1 parent c2b73b7 commit b19d30e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/MicroElements.Collections.Sources/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [1.11.0] - 2024-06-17
- TwoLayerCache concurrency bug

## [1.10.0] - 2023-10-22
- PollingCache become async only, unified methods, more customizations
- TwoLayerCache fixed maxItemCount for cold cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFramework>netstandard2.1</TargetFramework>

<PackageId>MicroElements.Collections.Sources</PackageId>
<PackageVersion>1.10.0</PackageVersion>
<PackageVersion>1.11.0</PackageVersion>
<Description>MicroElements source only package:
Collection extensions: NotNull, Iterate, Execute, WhereNotNull, Materialize, IncludeByWildcardPatterns, ExcludeByWildcardPatterns.
Special collections: Cache, TwoLayerCache, PollingCache.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,21 @@ private void OnColdCacheValueAdded(TKey key)
{
if (_checkColdCacheSize)
{
_keysQueue.Value.Enqueue(key);
CheckColdCacheSize();
}

Interlocked.Increment(ref _metrics.ItemsAdded);
}

private void CheckColdCacheSize()
{
if (_checkColdCacheSize && _coldCache.Count > _maxItemCount)
{
lock (_sync)
lock (_keysQueue)
{
var theOldestKey = _keysQueue.Value.Dequeue();
_coldCache.TryRemove(theOldestKey, out var removed);
if (_coldCache.Count > _maxItemCount)
{
// Get the oldest key and remove it from the cold cache
var theOldestKey = _keysQueue.Value.Dequeue();
_coldCache.TryRemove(theOldestKey, out var removed);
}

// Store added key
_keysQueue.Value.Enqueue(key);
}
}

Interlocked.Increment(ref _metrics.ItemsAdded);
}
}

Expand Down

0 comments on commit b19d30e

Please sign in to comment.