Skip to content

Commit

Permalink
Fixed failing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Mar 19, 2024
1 parent aa03c0c commit 969caf3
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Foundatio/Caching/InMemoryCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ public Task<int> RemoveByPrefixAsync(string prefix)

internal void RemoveExpiredKey(string key, bool sendNotification = true)
{
// Consideration: We could reduce the amount of calls to this by updating the key and only having maintenance run this.
// Consideration: We could also only remove expired items after a last access period determined when any property on the model is updated.
// Consideration: We could reduce the amount of calls to this by updating ExpiresAt and only having maintenance remove keys.
if (_memory.TryGetValue(key, out var existingEntry) && existingEntry.ExpiresAt < SystemClock.UtcNow)
{
if (_memory.TryRemove(key, out var removedEntry))
Expand All @@ -223,7 +222,6 @@ public Task<CacheValue<T>> GetAsync<T>(string key)

if (existingEntry.ExpiresAt < SystemClock.UtcNow)
{
RemoveExpiredKey(key);
Interlocked.Increment(ref _misses);
return Task.FromResult(CacheValue<T>.NoValue);
}
Expand Down Expand Up @@ -801,16 +799,19 @@ public Task<bool> ExistsAsync(string key)
if (String.IsNullOrEmpty(key))
return Task.FromException<bool>(new ArgumentNullException(nameof(key), "Key cannot be null or empty."));

if (!_memory.TryGetValue(key, out var cacheEntry))
if (!_memory.TryGetValue(key, out var existingEntry))
{
Interlocked.Increment(ref _misses);
return Task.FromResult(false);
}

Interlocked.Increment(ref _hits);
if (cacheEntry.ExpiresAt < SystemClock.UtcNow)
if (existingEntry.ExpiresAt < SystemClock.UtcNow)
{
Interlocked.Increment(ref _misses);
return Task.FromResult(false);
}

Interlocked.Increment(ref _hits);
return Task.FromResult(true);
}

Expand All @@ -819,19 +820,20 @@ public Task<bool> ExistsAsync(string key)
if (String.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key), "Key cannot be null or empty");

if (!_memory.TryGetValue(key, out var value) || value.ExpiresAt == DateTime.MaxValue)
if (!_memory.TryGetValue(key, out var existingEntry) || existingEntry.ExpiresAt == DateTime.MaxValue)
{
Interlocked.Increment(ref _misses);
return Task.FromResult<TimeSpan?>(null);
}

Interlocked.Increment(ref _hits);
if (value.ExpiresAt >= SystemClock.UtcNow)
return Task.FromResult<TimeSpan?>(value.ExpiresAt.Subtract(SystemClock.UtcNow));

RemoveExpiredKey(key);
if (existingEntry.ExpiresAt < SystemClock.UtcNow || existingEntry.ExpiresAt == DateTime.MaxValue)
{
Interlocked.Increment(ref _misses);
return Task.FromResult<TimeSpan?>(null);
}

return Task.FromResult<TimeSpan?>(null);
Interlocked.Increment(ref _hits);
return Task.FromResult<TimeSpan?>(existingEntry.ExpiresAt.Subtract(SystemClock.UtcNow));
}

public async Task SetExpirationAsync(string key, TimeSpan expiresIn)
Expand All @@ -847,9 +849,9 @@ public async Task SetExpirationAsync(string key, TimeSpan expiresIn)
}

Interlocked.Increment(ref _writes);
if (_memory.TryGetValue(key, out var value))
if (_memory.TryGetValue(key, out var existingEntry))
{
value.ExpiresAt = expiresAt;
existingEntry.ExpiresAt = expiresAt;
await StartMaintenanceAsync().AnyContext();
}
}
Expand Down

0 comments on commit 969caf3

Please sign in to comment.