Skip to main content

Caching

Vali-Mediator.Caching adds declarative pipeline caching to any IRequest<T> without modifying handler code.

Installation

dotnet add package Vali-Mediator.Caching

Setup

builder.Services.AddValiMediator(config =>
{
config.RegisterServicesFromAssemblyContaining<Program>();
config.AddCachingBehavior();
});

// Register the cache store
builder.Services.AddInMemoryCacheStore();

How It Works

loading...

Quick Example

// Mark request as cacheable
public record GetProductQuery(Guid Id)
: IRequest<Result<ProductDto>>, ICacheable
{
public string CacheKey => $"product:{Id}";
public TimeSpan? AbsoluteExpiration => TimeSpan.FromMinutes(10);
public TimeSpan? SlidingExpiration => null;
public string? CacheGroup => "products";
public bool BypassCache => false;
public CacheOrder Order => CacheOrder.CheckThenStore;
}

The handler needs no changes — caching is applied transparently by the pipeline behavior.

Key Concepts

ConceptDescription
ICacheableMarks a request as cacheable; provides cache key and expiration
IInvalidatesCacheMarks a request that invalidates cached entries on execution
ICacheStoreAbstraction for the cache backend (in-memory, Redis, etc.)
CacheOrderControls check-then-store vs store-only vs check-only
CacheGroupGroups related cache entries for bulk invalidation

Cache Store Options

StorePackageUse Case
InMemoryCacheStoreBuilt-inDevelopment, single-node
RedisImplement ICacheStoreProduction, distributed
SQL ServerImplement ICacheStoreDurable caching