MethodCache 1.0.0-rc.5
See the version list below for details.
dotnet add package MethodCache --version 1.0.0-rc.5
NuGet\Install-Package MethodCache -Version 1.0.0-rc.5
<PackageReference Include="MethodCache" Version="1.0.0-rc.5" />
<PackageVersion Include="MethodCache" Version="1.0.0-rc.5" />
<PackageReference Include="MethodCache" />
paket add MethodCache --version 1.0.0-rc.5
#r "nuget: MethodCache, 1.0.0-rc.5"
#:package MethodCache@1.0.0-rc.5
#addin nuget:?package=MethodCache&version=1.0.0-rc.5&prerelease
#tool nuget:?package=MethodCache&version=1.0.0-rc.5&prerelease
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 = "00:10:00", 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
IMemoryCachecode. - 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}"becomesuser: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,
HttpClientresponses (MethodCache.HttpCaching), and share results through Redis, SQL Server or PostgreSQL (MethodCache.Redis,MethodCache.SqlServer,MethodCache.PostgreSql).MethodCache.EntityFrameworkCoreinvalidates 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.GetOrCreateAsyncwith 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.Tagformats the tag exactly as the cached method does. With Entity Framework Core,MethodCache.EntityFrameworkCoreinvalidates the tags of the entities a commit changed. - Can I build the tag string myself? Only with
MethodCacheKeys.Partaround each value: aGuidis 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?
CacheNullValuessets it for every method;CacheWhendecides per method which results to store. - A test writes to the database directly. Call
ICacheAdministrator.ClearAsync()after the write, or registerNoOpCacheManagerto turn caching off. - Are synchronous methods cached? In memory only, also with a distributed cache.
- How do I see what's cached?
ICacheAdministrator, the admin endpoints inMethodCache.AspNetCore, and theMethodCachemeter.
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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.9)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.9)
- Microsoft.Extensions.Diagnostics (>= 9.0.9)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.9)
- Microsoft.Extensions.Options (>= 9.0.9)
- System.IO.Hashing (>= 9.0.9)
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 |