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


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 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. 
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
1.1.0 104 4/13/2026
1.0.1 104 3/18/2026
1.0.0 107 3/16/2026

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.