MethodCache 1.0.0-rc.17

This is a prerelease version of MethodCache.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package MethodCache --version 1.0.0-rc.17
                    
NuGet\Install-Package MethodCache -Version 1.0.0-rc.17
                    
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="MethodCache" Version="1.0.0-rc.17" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MethodCache" Version="1.0.0-rc.17" />
                    
Directory.Packages.props
<PackageReference Include="MethodCache" />
                    
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 MethodCache --version 1.0.0-rc.17
                    
#r "nuget: MethodCache, 1.0.0-rc.17"
                    
#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 MethodCache@1.0.0-rc.17
                    
#: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=MethodCache&version=1.0.0-rc.17&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MethodCache&version=1.0.0-rc.17&prerelease
                    
Install as a Cake Tool

MethodCache

Cache any .NET method with one attribute. MethodCache's source generator writes the caching code for you at compile time: no reflection, no hand-written keys, no TryGetValue boilerplate, and it works with Native AOT.

using MethodCache;

public interface IUserService
{
    [Cache(Duration = "10m", Tags = ["user:{id}"])]
    Task<User> GetUserAsync(int id);

    [CacheInvalidate(Tags = ["user:{id}"])]
    Task RenameAsync(int id, string name);
}
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddMethodCache();

Register your services as usual and call AddMethodCache() after them. GetUserAsync(42) then runs once and comes from memory until it expires or RenameAsync(42, ...) invalidates user 42, and only user 42.

  • One attribute, zero boilerplate. Keys are built from the method and its arguments; your service stays plain code.
  • Fast. A cache hit allocates nothing and on x64 is about 10 % faster than hand-written IMemoryCache code.
  • Compile-time. A source generator writes the decorators, analyzers catch mistakes in the editor, and it works with Native AOT.
  • Tags that understand your arguments. "user:{id}" becomes user:42, so an update invalidates exactly what it changed.
  • Tune it without redeploying. Durations and tags come from attributes, defaults, appsettings.json (reloaded live) or runtime overrides.
  • Third-party clients, HTTP responses, distributed caches. Cache an SDK client's own interface, HttpClient responses (MethodCache.HttpCaching), and share results through Redis, SQL Server or PostgreSQL (MethodCache.Redis, MethodCache.SqlServer, MethodCache.PostgreSql). MethodCache.EntityFrameworkCore invalidates after Entity Framework Core commits. Install add-ons at the same version.
  • Built for production. Stampede protection, fail-safe, a size-bounded memory cache, admin endpoints, a health check and OpenTelemetry tracing and metrics.

Common questions

  • Why does it need an interface? The generator writes a class that implements your interface and checks the cache before it calls your implementation. Without an interface, use ICacheManager.GetOrCreateAsync with a key you choose.
  • When does [CacheInvalidate] run? After the method, or its task, succeeds. Never when it throws.
  • The data changes somewhere else. Invalidate after the write with cache.InvalidateTagAsync(keys.Tag<IPriceSource>(s => s.GetRateAsync(provider, model), "price:{provider}:{model}")). keys.Tag formats the tag exactly as the cached method does. With Entity Framework Core, MethodCache.EntityFrameworkCore invalidates the tags of the entities a commit changed.
  • Can I build the tag string myself? Only with MethodCacheKeys.Part around each value: a Guid is written without hyphens, an enum as its number, and : is escaped. Analyzer MC0024 warns about hand-built tags.
  • How do I cache null, or skip some results? CacheNullValues sets it for every method; CacheWhen decides per method which results to store.
  • A test writes to the database directly. Call ICacheAdministrator.ClearAsync() after the write, or register NoOpCacheManager to turn caching off.
  • How do I test the caching itself? MethodCache.Testing adds a fake clock, a recorder that counts the runs of each call and lists the invalidated tags, and TagCoverage, which checks that every tag has an invalidation.
  • Are synchronous methods cached? In memory only, also with a distributed cache.
  • How do I see what's cached? ICacheAdministrator, the admin endpoints in MethodCache.AspNetCore, and the MethodCache meter.

More in How do I…?.

Documentation

This package carries its documentation in its docs folder (~/.nuget/packages/methodcache/<version>/docs/), so a coding agent can read the docs for the version you installed. Start with docs/ai/methodcache.md and docs/how-to.md. Online: documentation, getting started, build diagnostics and llms-full.txt.

To have an agent add caching, give it the prompt in Using a coding agent.

Source, benchmarks and a runnable demo: https://github.com/eoniclabs/MethodCache

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

Showing the top 5 NuGet packages that depend on MethodCache:

Package Downloads
MethodCache.OpenTelemetry

OpenTelemetry instrumentation for MethodCache: traces and metrics for cache operations

MethodCache.SqlServer

SQL Server as MethodCache's distributed cache, with a polling backplane that keeps every instance's in-memory cache in sync

MethodCache.AspNetCore

ASP.NET Core admin endpoints and health check for MethodCache: inspect statistics and entries, invalidate tags and clear the cache at runtime

MethodCache.Serialization.MessagePack

MessagePack serialization for MethodCache: compact binary values in Redis and SQL Server instead of the default System.Text.Json

MethodCache.Redis

Redis as MethodCache's distributed cache, with a pub/sub backplane that keeps every instance's in-memory cache in sync

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-rc.20 111 11/12/2026
1.0.0-rc.19 110 11/10/2026
1.0.0-rc.18 106 11/10/2026
1.0.0-rc.17 102 11/10/2026
1.0.0-rc.16 119 11/8/2026
1.0.0-rc.15 108 11/8/2026
1.0.0-rc.14 115 11/8/2026
1.0.0-rc.13 117 11/7/2026
1.0.0-rc.12 115 11/7/2026
1.0.0-rc.11 940 11/7/2026
1.0.0-rc.10 115 11/6/2026
1.0.0-rc.9 117 11/5/2026
1.0.0-rc.8 116 11/5/2026
1.0.0-rc.7 207 11/5/2026
1.0.0-rc.6 108 11/5/2026
1.0.0-rc.5 111 11/4/2026
1.0.0-rc.4 107 11/4/2026
1.0.0-rc.3 97 11/4/2026
1.0.0-rc.2 169 10/20/2026
1.0.0-rc.1 97 10/19/2026