前言
上次,我们介绍了应该在业务层实现管道模式
响应缓存是ASP.NET Core中很重要的功能,它可以存储响应,并提供来自缓存的响应,以便提高程序性能。
响应缓存通常是通过Middleware实现的:
public static class ResponseCachingExtensions
{
/// <summary>
/// Adds the <see cref="ResponseCachingMiddleware"/> for caching HTTP responses.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
public static IApplicationBuilder UseResponseCaching(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseMiddleware<ResponseCachingMiddleware>();
}
}
现在,我们改成由业务层实现响应缓存。
实现
1.引用Nuget包
新建WebAPI项目,引用需要的Nuget包:
Install-Package MediatR
Install-Package MediatR.Extensions.Microsoft.DependencyInjection
Install-Package Newtonsoft.Json
2.新增接口
新增ICacheableQuery
接口,表示需要缓存:
public interface ICacheableQuery
{
TimeSpan? SlidingExpiration { get; }
}
SlidingExpiration:缓存过期时间
3.新增缓存管道
利用MediatR的IPipelineBehavior功能,实现缓存管道:
public class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : ICacheableQuery
{
private readonly IDistributedCache _cache;
public CachingBehavior(IDistributedCache cache)
{
_cache = cache;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
TResponse response;
if (request.SlidingExpiration == null)
{
return await next();
}
var key =GetCacheKey(request);
var cachedResponse = await _cache.GetAsync(key, cancellationToken);
if (cachedResponse != null)
{
response = JsonConvert.DeserializeObject<TResponse>(Encoding.UTF8.GetString(cachedResponse));
}
else
{
response = await next();
var options = new DistributedCacheEntryOptions { SlidingExpiration = request.SlidingExpiration };
var serializedData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response));
await _cache.SetAsync(key, serializedData, options, cancellationToken);
return response;
}
return response;
}
}
以TRequest作为缓存Key,如果缓存存在,则反序列化得到缓存的响应,否则执行请求,缓存响应。
4.使用缓存管道
修改WeatherForecastController,使用Mediator,设置缓存时间为5秒:
public class WeatherForecastController : ControllerBase
{
private readonly IMediator _mediator;
public WeatherForecastController(IMediator mediator)
{
this._mediator = mediator;
}
[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get()
{
return await this._mediator.Send(new GetWeatherForecastQuery());
}
}
public class GetWeatherForecastQuery : IRequest<IEnumerable<WeatherForecast>>, ICacheableQuery
{
public TimeSpan? SlidingExpiration { get; set; } = TimeSpan.FromSeconds(5);
}
internal class GetWeatherForecastQueryHandler : IRequestHandler<GetWeatherForecastQuery, IEnumerable<WeatherForecast>>
{
public async Task<IEnumerable<WeatherForecast>> Handle(GetWeatherForecastQuery request, CancellationToken cancellationToken)
{
await Task.Delay(1000);
var rng = new Random();
return Enumerable.Range(1, 1).Select(index => new WeatherForecast
{
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
为了体现效果明显,代码里故意加了等待时间。
运行程序,可以看到,第一次请求花了1000多ms,而后的请求都很快,说明使用了缓存:
结论
在本文中,我们学会了使用MediatR实现响应缓存。
如果你觉得这篇文章对你有所启发,请关注我的个人公众号”My IO“