Hflex.MediatR.Extensions.Redis 1.0.6

dotnet add package Hflex.MediatR.Extensions.Redis --version 1.0.6
NuGet\Install-Package Hflex.MediatR.Extensions.Redis -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.Redis" 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.Redis --version 1.0.6
#r "nuget: Hflex.MediatR.Extensions.Redis, 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.Redis as a Cake Addin
#addin nuget:?package=Hflex.MediatR.Extensions.Redis&version=1.0.6

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

Hflex.MediatR.Extensions.Caching

Caching extension for MediatR

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

Installation

  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, 
  typeof(UpdateWeatherForecastsCommand));
  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);

Usage

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-browser 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)
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
1.0.6 286 1/9/2022
1.0.5 231 1/6/2022
1.0.4 225 1/6/2022
1.0.3 232 1/6/2022
1.0.2 221 1/6/2022
1.0.0 236 1/5/2022

First release of MediatR Redis caching