Skip to content

Commit

Permalink
AbsoluteExpirationRelativeToNow can internally just use AbsoluteExpir…
Browse files Browse the repository at this point in the history
…ation and current time
  • Loading branch information
adamsitnik committed Dec 1, 2020
1 parent 4b39c18 commit f84b6cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ internal class CacheEntry : ICacheEntry
private IList<IDisposable> _expirationTokenRegistrations;
private IList<PostEvictionCallbackRegistration> _postEvictionCallbacks;
private IList<IChangeToken> _expirationTokens;
private TimeSpan? _absoluteExpirationRelativeToNow;
private TimeSpan? _slidingExpiration;
private long? _size;
private IDisposable _scope;
Expand All @@ -45,8 +44,10 @@ internal CacheEntry(object key, MemoryCache memoryCache)
/// </summary>
public TimeSpan? AbsoluteExpirationRelativeToNow
{
get => _absoluteExpirationRelativeToNow;
set => _absoluteExpirationRelativeToNow = value > TimeSpan.Zero ? value : throw new ArgumentOutOfRangeException(nameof(AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive.");
get => AbsoluteExpiration.HasValue ? AbsoluteExpiration.Value - _cache.GetUtcNow() : null;
set => AbsoluteExpiration = value > TimeSpan.Zero
? (_cache.GetUtcNow() + value)
: throw new ArgumentOutOfRangeException(nameof(AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ public MemoryCache(IOptions<MemoryCacheOptions> optionsAccessor, ILoggerFactory
_logger = loggerFactory.CreateLogger<MemoryCache>();

_entries = new ConcurrentDictionary<object, CacheEntry>();

if (_options.Clock == null)
{
_options.Clock = new SystemClock();
}

_lastExpirationScan = _options.Clock.UtcNow;
_lastExpirationScan = _options.Clock?.UtcNow ?? DateTimeOffset.UtcNow;
}

/// <summary>
Expand Down Expand Up @@ -96,6 +90,8 @@ public ICacheEntry CreateEntry(object key)
return new CacheEntry(key, this);
}

internal DateTimeOffset GetUtcNow() => _options.Clock?.UtcNow ?? DateTimeOffset.UtcNow;

internal void SetEntry(CacheEntry entry)
{
if (_disposed)
Expand All @@ -109,17 +105,7 @@ internal void SetEntry(CacheEntry entry)
throw new InvalidOperationException($"Cache entry must specify a value for {nameof(entry.Size)} when {nameof(_options.SizeLimit)} is set.");
}

DateTimeOffset utcNow = _options.Clock.UtcNow;

DateTimeOffset? absoluteExpiration = null;
if (entry.AbsoluteExpirationRelativeToNow.HasValue)
{
absoluteExpiration = utcNow + entry.AbsoluteExpirationRelativeToNow;
}
else if (entry.AbsoluteExpiration.HasValue)
{
absoluteExpiration = entry.AbsoluteExpiration;
}
DateTimeOffset? absoluteExpiration = entry.AbsoluteExpiration;

// Applying the option's absolute expiration only if it's not already smaller.
// This can be the case if a dependent cache entry has a smaller value, and
Expand All @@ -132,6 +118,7 @@ internal void SetEntry(CacheEntry entry)
}
}

DateTimeOffset utcNow = GetUtcNow();
// Initialize the last access timestamp at the time the entry is added
entry.LastAccessed = utcNow;

Expand Down Expand Up @@ -227,7 +214,7 @@ public bool TryGetValue(object key, out object result)
ValidateCacheKey(key);
CheckDisposed();

DateTimeOffset utcNow = _options.Clock.UtcNow;
DateTimeOffset utcNow = GetUtcNow();

if (_entries.TryGetValue(key, out CacheEntry entry))
{
Expand Down Expand Up @@ -279,7 +266,7 @@ public void Remove(object key)
entry.InvokeEvictionCallbacks();
}

StartScanForExpiredItemsIfNeeded(_options.Clock.UtcNow);
StartScanForExpiredItemsIfNeeded(GetUtcNow());
}

private void RemoveEntry(CacheEntry entry)
Expand All @@ -298,7 +285,7 @@ internal void EntryExpired(CacheEntry entry)
{
// TODO: For efficiency consider processing these expirations in batches.
RemoveEntry(entry);
StartScanForExpiredItemsIfNeeded(_options.Clock.UtcNow);
StartScanForExpiredItemsIfNeeded(GetUtcNow());
}

// Called by multiple actions to see how long it's been since we last checked for expired items.
Expand All @@ -321,7 +308,7 @@ void ScheduleTask(DateTimeOffset utcNow)

private static void ScanForExpiredItems(MemoryCache cache)
{
DateTimeOffset now = cache._lastExpirationScan = cache._options.Clock.UtcNow;
DateTimeOffset now = cache._lastExpirationScan = cache.GetUtcNow();
foreach (CacheEntry entry in cache._entries.Values)
{
if (entry.CheckExpired(now))
Expand Down Expand Up @@ -404,7 +391,7 @@ private void Compact(long removalSizeTarget, Func<CacheEntry, long> computeEntry
long removedSize = 0;

// Sort items by expired & priority status
DateTimeOffset now = _options.Clock.UtcNow;
DateTimeOffset now = GetUtcNow();
foreach (CacheEntry entry in _entries.Values)
{
if (entry.CheckExpired(now))
Expand Down

0 comments on commit f84b6cb

Please sign in to comment.