Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend MemoryCacheOptions with TrackLinkedCacheEntries #57648

Merged
merged 6 commits into from
Sep 14, 2021
Merged
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 @@ -43,6 +43,7 @@ public MemoryCacheOptions() { }
public System.TimeSpan ExpirationScanFrequency { get { throw null; } set { } }
Microsoft.Extensions.Caching.Memory.MemoryCacheOptions Microsoft.Extensions.Options.IOptions<Microsoft.Extensions.Caching.Memory.MemoryCacheOptions>.Value { get { throw null; } }
public long? SizeLimit { get { throw null; } set { } }
public bool TrackLinkedCacheEntries { get { throw null; } set { } }
}
public partial class MemoryDistributedCacheOptions : Microsoft.Extensions.Caching.Memory.MemoryCacheOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ internal sealed partial class CacheEntry : ICacheEntry
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
private long? _size;
private CacheEntry _previous; // this field is not null only before the entry is added to the cache
private CacheEntry _previous; // this field is not null only before the entry is added to the cache and tracking is enabled
private object _value;
private CacheEntryState _state;

internal CacheEntry(object key, MemoryCache memoryCache)
{
Key = key ?? throw new ArgumentNullException(nameof(key));
_cache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
_previous = CacheEntryHelper.EnterScope(this);
_previous = memoryCache.TrackLinkedCacheEntries ? CacheEntryHelper.EnterScope(this) : null;
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
_state = new CacheEntryState(CacheItemPriority.Normal);
}

Expand Down Expand Up @@ -136,7 +136,10 @@ public void Dispose()
{
_state.IsDisposed = true;

CacheEntryHelper.ExitScope(this, _previous);
if (_cache.TrackLinkedCacheEntries)
{
CacheEntryHelper.ExitScope(this, _previous);
}

// Don't commit or propagate options if the CacheEntry Value was never set.
// We assume an exception occurred causing the caller to not set the Value successfully,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory
}

_lastExpirationScan = _options.Clock.UtcNow;
TrackLinkedCacheEntries = _options.TrackLinkedCacheEntries; // we store the setting now so it's consistent for entire MemoryCache lifetime
}

/// <summary>
Expand All @@ -79,6 +80,8 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory
// internal for testing
internal long Size { get => Interlocked.Read(ref _cacheSize); }

internal bool TrackLinkedCacheEntries { get; }

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

/// <inheritdoc />
Expand Down Expand Up @@ -232,7 +235,7 @@ public bool TryGetValue(object key, out object result)
entry.LastAccessed = utcNow;
result = entry.Value;

if (entry.CanPropagateOptions())
if (TrackLinkedCacheEntries && entry.CanPropagateOptions())
{
// When this entry is retrieved in the scope of creating another entry,
// that entry needs a copy of these expiration tokens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public double CompactionPercentage
}
}

/// <summary>
/// Gets or sets whether to track linked entries. Disabled by default.
/// </summary>
/// <remarks>Prior to .NET 7 this feature was always enabled.</remarks>
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
public bool TrackLinkedCacheEntries { get; set; }

MemoryCacheOptions IOptions<MemoryCacheOptions>.Value
{
get { return this; }
Expand Down
Loading