Skip to content

Instantly share code, notes, and snippets.

Created August 3, 2020 07:09
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Adding cache to MediatR
public class CachePipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
private readonly IDistributedCache cache;
private readonly ILogger<SGUKAspNetCore> logger;
public CachePipelineBehavior(
IDistributedCache cache,
ILogger<SGUKAspNetCore> logger)
Ensure.Argument.NotNull(cache, nameof(cache));
Ensure.Argument.NotNull(logger, nameof(logger));
this.cache = cache;
this.logger = logger;
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
if (request is ICacheable cacheableRequest)
var key = cacheableRequest.GetCacheKey();
return await cache.GetOrSet(
miss: () => { Log.CacheMiss(logger, key); return next(); },
hit: (data) => Log.CacheHit(logger, key),
var response = await next();
if (request is ICacheableInvalidation cacheInvalidationRequest)
foreach (var key in cacheInvalidationRequest.GetCacheKeys())
await cache.Remove(key, cancellationToken);
return response;
public interface ICacheable
public string GetCacheKey();
public TimeSpan? GetExpirationTime() => null;
public interface ICacheableInvalidation
public IEnumerable<string> GetCacheKeys();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment