In-memory response caching for MitMediator
dotnet add package MitMediator.InMemoryCache -v 10.0.0For a project that only declares requests, use MitMediator.InMemoryCache.Abstractions instead:
dotnet add package MitMediator.InMemoryCache.Abstractions -v 10.0.0using MitMediator;
builder.Services.AddMitMediator();
builder.Services.AddMitMediatorInMemoryCache();By default, registration scans all loaded assemblies for cache clearing rules. Only the first call per service collection takes effect. Rules and locks are local to each service provider.
Warning
Make sure .AddRequestsInMemoryCache() is registered as the last IPipelineBehavior. Cached responses will short-circuit the pipeline and prevent further execution
Optional settings (use instead of the default cache registration):
using Microsoft.Extensions.Caching.Memory;
builder.Services.AddMitMediatorInMemoryCache(
new MemoryCacheOptions { SizeLimit = 1000 },
lockStripingGatesCount: 128,
assemblies: [typeof(GetBookQuery).Assembly]);Include all assemblies with decorated requests. The lock count defaults to 64 per request type and must be positive.
Add the attribute to a request class or struct. All extensions and the attribute use MitMediator:
using MitMediator;
[CacheResponseInMemory]
public readonly record struct GetGenresQuery : IRequest<Genre[]>;Stays cached until cleared or evicted.
[CacheResponseInMemory(30)]
public readonly record struct GetBookQuery : IRequest<Book>
{
public int BookId { get; init; }
}Expires after 30 seconds. Cache hits do not extend this time.
[CacheResponseInMemory(30, 4)]
public readonly record struct GetBooksQuery : IRequest<Book[]>;Expires after 30 seconds. Each book counts as 4 size units: 5 books use 20 units.
[CacheResponseInMemory(30, 1, typeof(UpdateAuthorCommand))]
public readonly record struct GetAuthorQuery : IRequest<Author>
{
public int AuthorId { get; init; }
}Expires after 30 seconds. A successful UpdateAuthorCommand clears all cached authors sooner.
expirationSeconds is positive, or -1 for no time limit.
entrySize defaults to 1. For System.Collections.ICollection, size is Count * entrySize.
SizeLimit uses these units, not bytes. There is no capacity limit by default.
The attribute is not inherited.
Clear one request value or all values of a request type:
await mediator.ClearResponseCacheAsync(
new GetBookQuery { BookId = bookId },
cancellationToken);
await mediator.ClearAllResponseCacheAsync<GetBookQuery>(cancellationToken);Await clearing after a successful data change.
Automatic clearing also runs on a cached trigger, but not when downstream execution throws.
The trigger needs no caching attribute.
For Query<T>, automatic clearing covers all used variants, such as Query<int> and Query<string>, plus matching entries added directly to the cache.
Clearing waits for active writes before removing entries.
- Cache hits skip the handler and later pipeline behaviors. Register validation and authorization before caching.
- Keys use the runtime type's
FullNameand default JSON serialization. Use properties, not fields, for request data. Assembly identity is not part of the type prefix. nullresults and exceptions are not cached. Cached objects are not copied; avoid changing them.- Concurrent misses for the same key run one handler if its result is cached. Different keys may share a lock.
- Cancellation while waiting for a lock stops that caller without running its handler.
- Do not await clearing your own cache inside a cached handler, even through another command. Nested cached requests can also deadlock.
- The cache is local to one process and is lost on restart.