按照官网资料操作无效,这里使用https://github.com/speige/AspNetCore.ResponseCaching.Extensions的扩展包
安装AspNetCore.ResponseCaching.Extensions
在Startup.cs添加配置
services.AddDiskBackedMemoryResponseCaching((x, y) =>
{
//响应主体的最大可缓存大小(以字节为单位),默认64MB,对大多数情况而言可能太大了
//Default of 64MB is probably way too big for most scenarios
x.MaximumBodySize = 5 * 1024 * 1024;
});
//写在app.UseMVC()之前
app.UseCustomResponseCaching();
在接口上添加ResponseCache特性(这部分内容看官网)
[ResponseCache(VaryByHeader = "User-Agent", Duration = 60,Location =ResponseCacheLocation.Any)]
示例代码
https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreWebsite/Startup.cs
https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreWebsite/Controllers/ResponseCacheController.cs
参考资料
https://github.com/speige/AspNetCore.ResponseCaching.Extensions
https://stackoverflow.com/questions/48232911/asp-net-core-2-0-responsecaching-middleware-not-caching-on-server
https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/response?view=aspnetcore-2.1