MitMediator.InMemoryCache
9.0.0-alfa-2
dotnet add package MitMediator.InMemoryCache --version 9.0.0-alfa-2
NuGet\Install-Package MitMediator.InMemoryCache -Version 9.0.0-alfa-2
<PackageReference Include="MitMediator.InMemoryCache" Version="9.0.0-alfa-2" />
<PackageVersion Include="MitMediator.InMemoryCache" Version="9.0.0-alfa-2" />
<PackageReference Include="MitMediator.InMemoryCache" />
paket add MitMediator.InMemoryCache --version 9.0.0-alfa-2
#r "nuget: MitMediator.InMemoryCache, 9.0.0-alfa-2"
#:package MitMediator.InMemoryCache@9.0.0-alfa-2
#addin nuget:?package=MitMediator.InMemoryCache&version=9.0.0-alfa-2&prerelease
#tool nuget:?package=MitMediator.InMemoryCache&version=9.0.0-alfa-2&prerelease
MitMediator.InMemoryCache
An attribute-driven in-memory caching extension for the MitMediator
Installation
1. Add package
dotnet add package MitMediator.InMemoryCache -v 9.0.0-alfa-2
2. Register services
// Register handlers and IMediator
builder.Services.AddMitMediator();
// Register MemoryCache, InMemoryCacheBehavior,
// scan information about all IRequest<>
builder.Services.AddRequestsInMemoryCache()
⚠️ Important: Make sure .AddRequestsInMemoryCache()
is registered as the last IPipelineBehavior
. Cached responses will short-circuit the pipeline and prevent further execution
To customize MemoryCache
options and specify assemblies to scan:
builder.Services.AddRequestsInMemoryCache(
new MemoryCacheOptions { SizeLimit = 1000 },
new []{typeof(GetQuery).Assembly});`
Usage
Decorate your request classes with attribute [CacheResponse]
Requests decorated with the [CacheResponse]
attribute will have their responses cached in memory. You can control expiration, entry size, and define which requests should invalidate the cache
CacheResponseAttribute params
Name | Description |
---|---|
expirationSeconds |
Absolute expiration time in seconds, relative to the current moment. Set null for indefinitely |
entrySize |
The size of the cache entry item. For collections, size is calculated per element. Default value is 1 |
requestsToClearCache |
Types of requests that will trigger cache invalidation |
Use IMediator
extension methods to clear cached responses:
Clear cache for specific request data:
mediator.ClearResponseCacheAsync(request, ct);
Clear all cached responses for a request type
mediator.ClearAllResponseCacheAsync<GetBookQuery>(ct);
Example usage
Cache indefinitely:
[CacheResponse]
public struct GetGenresQuery : IRequest<Genre[]>;
Default
entrySize
is 1
Cache for 10 seconds:
[CacheResponse(10)]
public struct GetBookQuery : IRequest<Book>
{
public int BookId { get; set; }
}
Invalidate cache on specific requests:
[CacheResponse(typeof(DeleteAuthorCommand), typeof(UpdateAuthorCommand))]
public struct GetAuthorsByFilterQuery : IRequest<Author[]>
{
public int? Limit { get; init; }
public int? Offset { get; init; }
public string? FreeText { get; init; }
}
Set custom entry size and time (30s)
[CacheResponse(30, 4)]
public struct GetBooksByFilterQuery : IRequest<Book[]>
{
public int? Limit { get; init; }
public int? Offset { get; init; }
public string? FreeText { get; init; }
}
For
ICollection
types, the cache entry size isresponse.Count * entrySize
Clear cache after updating data:
public async ValueTask<Book> HandleAsync(UpdateBookTitleCommand command, CancellationToken cancellationToken)
{
var book = await _booksRepository.FirstOrDefaultAsync(b => b.BookId == command.BookId, cancellationToken);
book.SetTitle(command.Title);
await _booksRepository.UpdateAsync(book, cancellationToken);
// Clear cached response for the updated book
await _mediator.ClearResponseCacheAsync(new GetBookQuery() { BookId = command.BookId }, cancellationToken);
return book;
}
Responses are cached per unique request data. For example,
GetBookQuery
caches a separate response for eachBookId
See samples
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 9.0.0)
- MitMediator (>= 9.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.
Version | Downloads | Last Updated |
---|---|---|
9.0.0-alfa-2 | 44 | 9/13/2025 |
9.0.0-alfa | 56 | 9/12/2025 |
v9.0.0-alfa-2
Added extensions methods for IMediator. Unified all cache-related attributes into a single [CacheResponse] attribute