.net core響應緩存

按照官網資料操做無效,這裏使用AspNetCore.ResponseCaching.Extensions的擴展包
安裝AspNetCore.ResponseCaching.Extensions
在Startup.cs添加配置ios

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特性(這部份內容看官網)git

[ResponseCache(VaryByHeader = "User-Agent", Duration = 60,Location =ResponseCacheLocation.Any)]

示例代碼

Startup
ResponseCacheControllergithub

參考資料

AspNetCore.ResponseCaching.Extensions
ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server
響應緩存在 ASP.NET Corec#

相關文章
相關標籤/搜索