Skip to content

Commit

Permalink
Extend MemoryCacheOptions with TrackLinkedCacheEntries (#57648)
Browse files Browse the repository at this point in the history
* add the property to reference assembly

* update tests

* implementation

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* Apply suggestions from code review

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

* update comment

Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
  • Loading branch information
3 people authored Sep 14, 2021
1 parent e500c80 commit f2ce707
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 85 deletions.
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;
_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>
public bool TrackLinkedCacheEntries { get; set; }

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

0 comments on commit f2ce707

Please sign in to comment.