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
                    
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="Vali-Mediator.Caching" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Mediator.Caching" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Mediator.Caching" />
                    
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 Vali-Mediator.Caching --version 2.0.0
                    
#r "nuget: Vali-Mediator.Caching, 2.0.0"
                    
#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 Vali-Mediator.Caching@2.0.0
                    
#: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=Vali-Mediator.Caching&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Mediator.Caching&version=2.0.0
                    
Install as a Cake Tool

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:


License

Apache License 2.0

Contributions

Issues and pull requests are welcome on GitHub.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 131 10/1/2026
1.1.0 158 4/13/2026
1.0.1 144 3/18/2026
1.0.0 142 3/16/2026

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.