前言
响应缓存是 ASP.NET Core
中很重要的功能,它可以存储响应,并提供来自缓存的响应,以便提高程序性能。
响应缓存通常是通过 Middleware
实现的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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
包:
1
2
3
|
Install-Package MediatR
Install-Package MediatR.Extensions.Microsoft.DependencyInjection
Install-Package Newtonsoft.Json
|
2. 新增接口
新增 ICacheableQuery
接口,表示需要缓存:
1
2
3
4
|
public interface ICacheableQuery
{
TimeSpan? SlidingExpiration { get; }
}
|
3. 新增缓存管道
利用 MediatR
的 IPipelineBehavior
功能,实现缓存管道:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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 秒:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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,而后的请求都很快,说明使用了缓存: