using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using MediatR; using Microsoft.Extensions.Logging; namespace BuildingBlocks.Caching { public class InvalidateCachingBehavior : IPipelineBehavior where TRequest : notnull, IRequest where TResponse : notnull { private readonly ILogger> _logger; private readonly IEasyCachingProvider _cachingProvider; private readonly IInvalidateCacheRequest _invalidateCacheRequest; public InvalidateCachingBehavior(IEasyCachingProviderFactory cachingFactory, ILogger> logger, IInvalidateCacheRequest invalidateCacheRequest) { _logger = logger; _cachingProvider = cachingFactory.GetCachingProvider("mem"); _invalidateCacheRequest = invalidateCacheRequest; } public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) { if (request is not IInvalidateCacheRequest || _invalidateCacheRequest == null) { // No cache request found, so just continue through the pipeline return await next(); } var cacheKey = _invalidateCacheRequest.CacheKey; var response = await next(); await _cachingProvider.RemoveAsync(cacheKey); _logger.LogDebug("Cache data with cache key: {CacheKey} removed.", cacheKey); return response; } } }