按照官网资料操作无效,这里使用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)]
示例代码
Startup
ResponseCacheController
参考资料
AspNetCore.ResponseCaching.Extensions
ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server
响应缓存在 ASP.NET Core