Vali-Mediator.Caching
1.1.0
dotnet add package Vali-Mediator.Caching --version 1.1.0
NuGet\Install-Package Vali-Mediator.Caching -Version 1.1.0
<PackageReference Include="Vali-Mediator.Caching" Version="1.1.0" />
<PackageVersion Include="Vali-Mediator.Caching" Version="1.1.0" />
<PackageReference Include="Vali-Mediator.Caching" />
paket add Vali-Mediator.Caching --version 1.1.0
#r "nuget: Vali-Mediator.Caching, 1.1.0"
#:package Vali-Mediator.Caching@1.1.0
#addin nuget:?package=Vali-Mediator.Caching&version=1.1.0
#tool nuget:?package=Vali-Mediator.Caching&version=1.1.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, and two pipeline behaviors that wire everything together.
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
builder.Services.AddInMemoryCacheStore(options =>
{
options.MaxEntries = 5000;
options.CleanupInterval = TimeSpan.FromMinutes(10);
});
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 was computed. 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. |
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 2.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 2.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Vali-Mediator (>= 2.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.
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.