Skip to content

Commit

Permalink
RavenDB-17373
Browse files Browse the repository at this point in the history
- Pulling MemoryCache fixes from  dotnet/runtime#57631 and dotnet/runtime#61187
  • Loading branch information
ayende committed Nov 11, 2021
1 parent 76753b7 commit 36c56d8
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/Raven.Server/Utils/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Lucene.Net.Util.Cache;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
Expand All @@ -24,7 +25,7 @@ public class MemoryCache : IMemoryCache
internal readonly ILogger _logger;

private readonly MemoryCacheOptions _options;
private readonly ConcurrentDictionary<object, CacheEntry> _entries;
private ConcurrentDictionary<object, CacheEntry> _entries;

private long _cacheSize;
private bool _disposed;
Expand Down Expand Up @@ -85,7 +86,18 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory

private ICollection<KeyValuePair<object, CacheEntry>> EntriesCollection => _entries;

public void Clear() => _entries.Clear();
public void Clear()
{
var oldEntries = _entries;
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();
}
}

public IEnumerable<KeyValuePair<object, object>> EntriesForDebug => _entries.Select(kvp => new KeyValuePair<object, object>(kvp.Key, kvp.Value.Value));

Expand Down Expand Up @@ -386,9 +398,10 @@ public void Compact(double percentage)
private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntrySize)
{
var entriesToRemove = new List<CacheEntry>();
var lowPriEntries = new List<CacheEntry>();
var normalPriEntries = new List<CacheEntry>();
var highPriEntries = new List<CacheEntry>();
// cache LastAccessed outside of the CacheEntry so it is stable during compaction
var lowPriEntries = new List<(CacheEntry entry, DateTimeOffset lastAccessed)>();
var normalPriEntries = new List<(CacheEntry entry, DateTimeOffset lastAccessed)>();
var highPriEntries = new List<(CacheEntry entry, DateTimeOffset lastAccessed)>();
long removedSize = 0;

// Sort items by expired & priority status
Expand All @@ -406,13 +419,13 @@ private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntry
switch (entry.Priority)
{
case CacheItemPriority.Low:
lowPriEntries.Add(entry);
lowPriEntries.Add((entry, entry.LastAccessed));
break;
case CacheItemPriority.Normal:
normalPriEntries.Add(entry);
normalPriEntries.Add((entry, entry.LastAccessed));
break;
case CacheItemPriority.High:
highPriEntries.Add(entry);
highPriEntries.Add((entry, entry.LastAccessed));
break;
case CacheItemPriority.NeverRemove:
break;
Expand All @@ -437,7 +450,7 @@ private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntry
// ?. Items with the soonest sliding expiration.
// ?. Larger objects - estimated by object graph size, inaccurate.
static void ExpirePriorityBucket(ref long removedSize, long removalSizeTarget, Func<CacheEntry, long> computeEntrySize, List<CacheEntry> entriesToRemove,
List<CacheEntry> priorityEntries)
List<(CacheEntry Entry, DateTimeOffset LastAccessed)> priorityEntries)
{
// Do we meet our quota by just removing expired entries?
if (removalSizeTarget <= removedSize)
Expand All @@ -450,8 +463,8 @@ static void ExpirePriorityBucket(ref long removedSize, long removalSizeTarget, F
// TODO: Refine policy

// LRU
priorityEntries.Sort((e1, e2) => e1.LastAccessed.CompareTo(e2.LastAccessed));
foreach (CacheEntry entry in priorityEntries)
priorityEntries.Sort( (e1, e2) => e1.LastAccessed.CompareTo(e2.LastAccessed));
foreach ((CacheEntry entry,_) in priorityEntries)
{
entry.SetExpired(EvictionReason.Capacity);
entriesToRemove.Add(entry);
Expand Down

0 comments on commit 36c56d8

Please sign in to comment.