Hflex.MediatR.Extensions.Caching
1.0.6
dotnet add package Hflex.MediatR.Extensions.Caching --version 1.0.6
NuGet\Install-Package Hflex.MediatR.Extensions.Caching -Version 1.0.6
<PackageReference Include="Hflex.MediatR.Extensions.Caching" Version="1.0.6" />
paket add Hflex.MediatR.Extensions.Caching --version 1.0.6
#r "nuget: Hflex.MediatR.Extensions.Caching, 1.0.6"
// Install Hflex.MediatR.Extensions.Caching as a Cake Addin #addin nuget:?package=Hflex.MediatR.Extensions.Caching&version=1.0.6 // Install Hflex.MediatR.Extensions.Caching as a Cake Tool #tool nuget:?package=Hflex.MediatR.Extensions.Caching&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 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. |
-
net6.0
- MediatR (>= 9.0.0)
- MediatR.Extensions.Microsoft.DependencyInjection (>= 9.0.0)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.Caching.Memory (>= 6.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Hflex.MediatR.Extensions.Caching:
Package | Downloads |
---|---|
Hflex.MediatR.Extensions.InMemoryCaching
MediatR extension for in memory caching |
|
Hflex.MediatR.Extensions.Redis
MediatR extension for Redis cache |
GitHub repositories
This package is not used by any popular GitHub repositories.
First release of MediatR caching