ZeroAlloc.Cache
1.3.5
See the version list below for details.
dotnet add package ZeroAlloc.Cache --version 1.3.5
NuGet\Install-Package ZeroAlloc.Cache -Version 1.3.5
<PackageReference Include="ZeroAlloc.Cache" Version="1.3.5" />
<PackageVersion Include="ZeroAlloc.Cache" Version="1.3.5" />
<PackageReference Include="ZeroAlloc.Cache" />
paket add ZeroAlloc.Cache --version 1.3.5
#r "nuget: ZeroAlloc.Cache, 1.3.5"
#:package ZeroAlloc.Cache@1.3.5
#addin nuget:?package=ZeroAlloc.Cache&version=1.3.5
#tool nuget:?package=ZeroAlloc.Cache&version=1.3.5
ZeroAlloc.Cache
Put [Cache] on an interface and a source generator writes the caching proxy for you: no lookup boilerplate, no reflection, and a cache hit that allocates nothing.
Why ZeroAlloc.Cache
- 0 B per cache hit. A
ValueTask<T>hit on the defaultMemoryCacheon .NET 9 and later allocates nothing, and a test fails the build if it ever does. The few cases that still allocate are listed. - About 4× faster than FusionCache on an in-process cache hit, measured in the same run.
- Works under NativeAOT and trimming. The proxy is plain generated code, published and run as a NativeAOT app in CI.
- Mistakes surface at compile time. Unstable key parameters, a missing
HybridCachepackage or a missing AOT serializer are compiler diagnostics, not production surprises.
Install
dotnet add package ZeroAlloc.Cache
Quick start
using Microsoft.Extensions.DependencyInjection;
using ZeroAlloc.Cache;
var services = new ServiceCollection();
services.AddProductCatalogCache<ProductCatalog>(); // generated: registers the caching proxy
var catalog = services.BuildServiceProvider().GetRequiredService<IProductCatalog>();
Console.WriteLine(await catalog.GetNameAsync(42, CancellationToken.None)); // miss: calls ProductCatalog
Console.WriteLine(await catalog.GetNameAsync(42, CancellationToken.None)); // hit: no call, no allocation
[Cache(TtlMs = 60_000)]
public interface IProductCatalog
{
ValueTask<string> GetNameAsync(int id, CancellationToken ct);
}
public sealed class ProductCatalog : IProductCatalog
{
public ValueTask<string> GetNameAsync(int id, CancellationToken ct)
{
Console.WriteLine($"Loading product {id}...");
return ValueTask.FromResult($"Product {id}");
}
}
Loading product 42... prints once. Callers keep injecting IProductCatalog; caching is invisible to them.
What you get
- Transparent caching for any
Task<T>orValueTask<T>method, configured once on the interface and overridden per method. - Generated cache keys from the method's parameters, with
CancellationTokenleft out. The key text is predictable, so you can evict an entry by it. - Absolute or sliding expiry, and
MaxEntriesto bound memory on high-cardinality methods. IMemoryCacheby default, withHybridCacheas an L1 + L2 distributed tier per method:UseHybridCache = true.- One-line DI registration through a generated
Add{Service}Cache<TImpl>()extension. - Built-in metrics and tracing on the BCL
MeterandActivitySource, ready for OpenTelemetry: Telemetry. - Compile-time diagnostics ZC0001–ZC0011: the full list.
Performance
In-process cache hit. .NET 10.0.13, BenchmarkDotNet v0.15.8, same run. Allocations are exact; speed is a same-run ratio against hand-written IMemoryCache code.
| Library | Ratio | Allocated |
|---|---|---|
Raw IMemoryCache.GetOrCreateAsync |
1.00 | 104 B |
| ZA.Cache proxy | 1.66 | 0 B |
| FusionCache | 6.88 | 88 B |
ZA.Cache is about 4× faster than FusionCache and the only row that allocates nothing. Against hand-written IMemoryCache code it costs about 1.7× the time, for proxy dispatch, key formatting and telemetry, in exchange for no lookup code at any call site. Methodology and the full numbers: docs/performance.md.
Learn more
- Documentation: cache.zeroalloc.net
- Getting started, including
HybridCacheunder NativeAOT, and the attribute reference - Related packages: ZeroAlloc.Resilience for retry and circuit-breaker proxies, ZeroAlloc.Telemetry for tracing and metrics proxies
- Contributing: issues and pull requests are welcome on GitHub
- License: MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.11.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
-
net11.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.11.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
-
net8.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
-
net9.0
- Microsoft.Extensions.Caching.Hybrid (>= 10.11.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ZeroAlloc.Cache:
| Package | Downloads |
|---|---|
|
ZeroAlloc.Mediator.Cache
Pipeline behavior that caches IRequest<T> responses when the request type carries [CacheResponse]. A cache hit formats its key on the stack instead of building a string; a request that implements ISpanFormattable is answered from the cache without allocating. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.1 | 66 | 11/22/2026 |
| 2.0.0 | 89 | 11/22/2026 |
| 1.3.5 | 812 | 11/17/2026 |
| 1.3.4 | 625 | 11/16/2026 |
| 1.3.3 | 442 | 11/14/2026 |
| 1.3.2 | 197 | 11/14/2026 |
| 1.3.1 | 287 | 11/12/2026 |
| 1.3.0 | 109 | 11/11/2026 |
| 1.2.18 | 385 | 11/10/2026 |
| 1.2.17 | 152 | 11/8/2026 |
| 1.2.16 | 116 | 11/7/2026 |
| 1.2.14 | 221 | 10/30/2026 |
| 1.2.13 | 113 | 10/30/2026 |
| 1.2.12 | 468 | 10/26/2026 |
| 1.2.11 | 114 | 10/25/2026 |
| 1.2.10 | 133 | 10/24/2026 |
| 1.2.9 | 140 | 10/22/2026 |
| 1.2.8 | 151 | 10/20/2026 |
| 1.2.7 | 125 | 10/19/2026 |
| 1.2.6 | 152 | 10/18/2026 |