Vali-Mediator.Caching
2.0.0
dotnet add package Vali-Mediator.Caching --version 2.0.0
NuGet\Install-Package Vali-Mediator.Caching -Version 2.0.0
<PackageReference Include="Vali-Mediator.Caching" Version="2.0.0" />
<PackageVersion Include="Vali-Mediator.Caching" Version="2.0.0" />
<PackageReference Include="Vali-Mediator.Caching" />
paket add Vali-Mediator.Caching --version 2.0.0
#r "nuget: Vali-Mediator.Caching, 2.0.0"
#:package Vali-Mediator.Caching@2.0.0
#addin nuget:?package=Vali-Mediator.Caching&version=2.0.0
#tool nuget:?package=Vali-Mediator.Caching&version=2.0.0
Vali-Mediator.Caching
Caching integration for the Vali-Mediator ecosystem.
Provides a pluggable ICacheStore abstraction, an in-memory implementation with absolute/sliding
expiry and group-based invalidation, two pipeline behaviors that wire everything together, and
automatic coalescing of concurrent cache misses so a stampede of identical requests only runs the
handler once.
Installation
dotnet add package Vali-Mediator.Caching
Quick Start
// Program.cs / Startup.cs
builder.Services.AddInMemoryCacheStore(); // or AddCacheStore<MyRedisStore>()
builder.Services.AddValiMediator(config =>
{
config.RegisterServicesFromAssemblyContaining<Program>();
config.AddCachingBehavior();
});
Making a Query Cacheable
Implement ICacheable on your IRequest<TResponse>:
public sealed class GetProductQuery : IRequest<ProductDto>, ICacheable
{
public int ProductId { get; init; }
public string CacheKey => $"product:{ProductId}";
public TimeSpan? AbsoluteExpiration => TimeSpan.FromMinutes(10);
public TimeSpan? SlidingExpiration => null;
public string? CacheGroup => "products";
public bool BypassCache => false;
public CacheOrder Order => CacheOrder.ReadThenWrite;
}
Invalidating Cache From a Command
Implement IInvalidatesCache on the command that modifies the resource:
public sealed class UpdateProductCommand : IRequest<Unit>, IInvalidatesCache
{
public int ProductId { get; init; }
public IReadOnlyList<string> InvalidatedKeys => new List<string> { $"product:{ProductId}" };
public IReadOnlyList<string> InvalidatedGroups => new List<string>();
}
Or invalidate an entire group at once:
public sealed class DeleteAllProductsCommand : IRequest<Unit>, IInvalidatesCache
{
public IReadOnlyList<string> InvalidatedKeys => new List<string>();
public IReadOnlyList<string> InvalidatedGroups => new List<string> { "products" };
}
CacheOrder
| Value | Behavior |
|---|---|
ReadThenWrite (default) |
Read cache first; on miss execute handler and write result |
WriteOnly |
Always execute handler; skip read but always write |
ReadOnly |
Read cache if available; never write after handler |
Custom Cache Store
Implement ICacheStore (and optionally IGroupAwareCacheStore for group support):
public sealed class RedisCacheStore : ICacheStore
{
// ... implement TryGetAsync, SetAsync, RemoveAsync, RemoveByGroupAsync
}
// Registration
builder.Services.AddCacheStore<RedisCacheStore>();
In-Memory Store Options
Every limit is validated (must be greater than zero) and bounds memory even when keys/groups come from client-controlled input — an oversized key or a group past the limit is dropped instead of accepted:
builder.Services.AddInMemoryCacheStore(options =>
{
options.MaxEntries = 5000; // least-recently-used entry evicted when full (default 10 000)
options.MaxKeyLength = 512; // longer keys/group names are never stored (default 512)
options.MaxGroups = 10_000; // distinct groups indexed at once (default 10 000)
options.MaxKeysPerGroup = 10_000; // keys indexed under one group (default 10 000)
options.CleanupInterval = TimeSpan.FromMinutes(10);
});
Coalescing Concurrent Misses
When several requests ask for the same cache key at the same time and it's a miss, only the first one
runs the handler — the rest await that same in-flight call instead of each hitting the backing store
(and, transitively, the database). This is automatic whenever AddCachingBehavior() is registered; the
only knob is how long a follower waits before giving up and running the handler itself:
builder.Services.AddCachingOptions(options =>
{
options.CoalescingWaitTimeout = TimeSpan.FromSeconds(30); // bounds the damage of a hung handler
});
Donations
If Vali-Mediator is useful to you, consider supporting its development:
- Latin America — MercadoPago
- International — PayPal
License
Contributions
Issues and pull requests are welcome on GitHub.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 3.0.0)
-
net7.0
- Microsoft.Bcl.TimeProvider (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 3.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 3.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.0.0 — Requires Vali-Mediator >= 3.0.0. LRU store with strict limits (MaxEntries/MaxKeyLength/MaxGroups/MaxKeysPerGroup), request coalescing, failed results never cached. See CHANGELOG.md.
v1.1.0 — Package structure: now uses Vali-Mediator as NuGet dependency instead of local ProjectReference.
v1.0.1 — Documentation: enabled XML documentation file for full IntelliSense support.
v1.0.0 — Initial release:
- ICacheable: per-request cache key, absolute/sliding expiry, group, bypass flag, and CacheOrder.
- IInvalidatesCache: explicit key and group invalidation contract for commands.
- CacheOrder enum: ReadThenWrite (default), WriteOnly, ReadOnly.
- ICacheStore abstraction: TryGetAsync, SetAsync, RemoveAsync, RemoveByGroupAsync.
- InMemoryCacheStore: ConcurrentDictionary-backed, lazy expiry checking, group tracking.
- InMemoryCacheOptions: MaxEntries and CleanupInterval configuration.
- CachingBehavior: open-generic IPipelineBehavior that honors CacheOrder and BypassCache.
- CacheInvalidationBehavior: evicts keys and groups after a successful command.
- AddCachingBehavior() extension on ValiMediatorConfiguration.
- AddInMemoryCacheStore() / AddCacheStore<TStore>() extensions on IServiceCollection.