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
<PackageReference Include="Hflex.MediatR.Extensions.Redis" Version="1.0.6" />
paket add Hflex.MediatR.Extensions.Redis --version 1.0.6
#r "nuget: Hflex.MediatR.Extensions.Redis, 1.0.6"
// 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
- Install-Package Hflex.MediatR.Extensions.Redis for distributed cache via Redis or Install-Package Hflex.MediatR.Extensions.InMemoryCaching for in memory caching.
- 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));
- 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 | Versions |
---|---|
.NET | net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
-
net6.0
- Hflex.MediatR.Extensions.Caching (>= 1.0.5)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First release of MediatR Redis caching