Hflex.MediatR.Extensions.InMemoryCaching 1.0.6

.NET 6.0
dotnet add package Hflex.MediatR.Extensions.InMemoryCaching --version 1.0.6
NuGet\Install-Package Hflex.MediatR.Extensions.InMemoryCaching -Version 1.0.6
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="Hflex.MediatR.Extensions.InMemoryCaching" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hflex.MediatR.Extensions.InMemoryCaching --version 1.0.6
#r "nuget: Hflex.MediatR.Extensions.InMemoryCaching, 1.0.6"
#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.
// Install Hflex.MediatR.Extensions.InMemoryCaching as a Cake Addin
#addin nuget:?package=Hflex.MediatR.Extensions.InMemoryCaching&version=1.0.6

// Install Hflex.MediatR.Extensions.InMemoryCaching as a Cake Tool
#tool nuget:?package=Hflex.MediatR.Extensions.InMemoryCaching&version=1.0.6


Caching extension for MediatR

Inspired by https://github.com/Iamcerba/AspNetCore.CacheOutput


  1. Install-Package Hflex.MediatR.Extensions.Redis for distributed cache via Redis or Install-Package Hflex.MediatR.Extensions.InMemoryCaching for in memory caching.
  2. Define configurations for caching
var cachingConfigurations = new CachingConfiguration();

//GetTodoItemsQuery will be invalidated, when timer notification will be fired on InvalidateCacheHostedService
cachingConfigurations.AddConfiguration<GetTodoItemsQuery>(duration: TimeSpan.FromMinutes(2), false);

//GetWeatherForecastsQuery will be invalidated, when UpdateWeatherForecastsCommand called
cachingConfigurations.AddConfiguration<GetWeatherForecastsQuery>(TimeSpan.FromMinutes(10), false, null, 
  1. In Startup.cs or Program.cs Register accordingly
  • In case of Redis use builder.Services.AddRedisCache(cachingConfigurations, builder.Configuration.GetConnectionString("RedisConnectionString"));
  • In case of InMemory use builder.Services.AddInMemoryCache(cachingConfigurations);


Everything does configuration, just register

var cachingConfigurations = new CachingConfiguration();
cachingConfigurations.AddConfiguration<GetWeatherForecastsQuery>(duration: TimeSpan.FromMinutes(10),//Cache duration
perUser: false, //Set to true for caching per user (includes userID into cache key)
keyFactory: null, //Create Func<string> for replacing default key, which is serialized json from request object
invalidatesOnRequests: typeof(UpdateWeatherForecastsCommand), typeof(secondcommand), typeof(thirdCommand) ..... //Command types to invalidate query);

like this for GetWeatherForecastsQuery query, which will get cached for 10 minutes. And once any of invalidatesOnRequests called it will get invalidated automatically. Alternative way of force invalidating queries is to use Notifications. Just publish

 _mediator.Publish(new InvalidateCacheNotification { RequestTypes = new Type[]{ typeof(queryType1), typeof(queryType2), ...}});

anywhere in the application e.g. in samples I use hostedService with timer to simulate some domain events, which invalidates GetTodoItemsQuery

_mediator.Publish(new InvalidateCacheNotification { RequestTypes = new []{typeof(GetTodoItemsQuery)} });
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 was computed.  net8.0-android 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. 
Compatible target framework(s)
Additional computed target framework(s)
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.0.6 272 1/9/2022
1.0.5 223 1/6/2022
1.0.4 199 1/6/2022
1.0.3 215 1/6/2022
1.0.2 200 1/6/2022
1.0.1 220 1/5/2022
1.0.0 200 1/5/2022

First release of MediatR InMemory caching