Duende.Extensions.Caching.Memory 10.0.0

Prefix Reserved
dotnet add package Duende.Extensions.Caching.Memory --version 10.0.0
                    
NuGet\Install-Package Duende.Extensions.Caching.Memory -Version 10.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Duende.Extensions.Caching.Memory" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Duende.Extensions.Caching.Memory" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Duende.Extensions.Caching.Memory" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Duende.Extensions.Caching.Memory --version 10.0.0
                    
#r "nuget: Duende.Extensions.Caching.Memory, 10.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Duende.Extensions.Caching.Memory@10.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Duende.Extensions.Caching.Memory&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Duende.Extensions.Caching.Memory&version=10.0.0
                    
Install as a Cake Tool

Duende.Extensions.Caching.Memory

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.

Overview

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 IMemoryCache interface

Usage

Direct Instantiation

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));

Dependency Injection

// Basic registration (uses TimeProvider.System)
services.AddTimeProviderMemoryCache();

// With configuration
services.AddTimeProviderMemoryCache(options =>
{
    options.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
    options.SizeLimit = 1024;
});

Testing Example

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();

License

Apache 2.0 - see LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.0 4,239 2/17/2026