MitMediator.InMemoryCache 9.0.0-alfa-2

This is a prerelease version of MitMediator.InMemoryCache.
dotnet add package MitMediator.InMemoryCache --version 9.0.0-alfa-2
                    
NuGet\Install-Package MitMediator.InMemoryCache -Version 9.0.0-alfa-2
                    
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="MitMediator.InMemoryCache" Version="9.0.0-alfa-2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MitMediator.InMemoryCache" Version="9.0.0-alfa-2" />
                    
Directory.Packages.props
<PackageReference Include="MitMediator.InMemoryCache" />
                    
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 MitMediator.InMemoryCache --version 9.0.0-alfa-2
                    
#r "nuget: MitMediator.InMemoryCache, 9.0.0-alfa-2"
                    
#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 MitMediator.InMemoryCache@9.0.0-alfa-2
                    
#: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=MitMediator.InMemoryCache&version=9.0.0-alfa-2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MitMediator.InMemoryCache&version=9.0.0-alfa-2&prerelease
                    
Install as a Cake Tool

MitMediator.InMemoryCache

An attribute-driven in-memory caching extension for the MitMediator

Build and Test NuGet .NET 9.0 NuGet Downloads License

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 is response.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 each BookId

See samples

License

MIT

Product 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. 
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
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