A TimeProvider-based implementation of IMemoryCache that enables testable time-dependent caching. This exists because
of this outstanding issue. This library will be obsolete once that issue is resolved
in some future version of .NET SDK.
This library provides a wrapper around Microsoft.Extensions.Caching.Memory.MemoryCache that integrates with .NET's TimeProvider abstraction. This allows you to:
- Write unit tests that manipulate time using
FakeTimeProvider - Verify cache expiration behavior without real delays
- Maintain full compatibility with the standard
IMemoryCacheinterface
var timeProvider = TimeProvider.System; // or FakeTimeProvider in tests
var options = Options.Create(new TimeProviderMemoryCacheOptions
{
ExpirationScanFrequency = TimeSpan.FromMinutes(1),
SizeLimit = 1024
});
var cache = new TimeProviderMemoryCache(timeProvider, options);
// Use like any IMemoryCache
cache.Set("key", "value", TimeSpan.FromMinutes(5));// Basic registration (uses TimeProvider.System)
services.AddTimeProviderMemoryCache();
// With configuration
services.AddTimeProviderMemoryCache(options =>
{
options.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
options.SizeLimit = 1024;
});var fakeTime = new FakeTimeProvider();
var cache = new TimeProviderMemoryCache(fakeTime, options);
cache.Set("key", "value", TimeSpan.FromMinutes(5));
// Advance time past expiration
fakeTime.Advance(TimeSpan.FromMinutes(6));
// Entry should be expired
cache.TryGetValue("key", out var result).ShouldBeFalse();Apache 2.0 - see LICENSE for details.