ResponseCache 在 net core 3.1 中不起作用
ResponseCache not working in net core 3.1
我试图了解 .NET Core 3.1 中的响应缓存。但它没有像我希望的那样工作。我在 Chrome devtool 中查看了网络,它显示响应 header 和 cache-control: no-cache, no-store
。
我还发现 Response header 与 Actionfilter 中的 HeaderCacheControl{public,max-age=100}
一起。这是我预期的值,但浏览器中的实际响应 header 是 no-cache
.
Startup
class:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching(options=>
{
options.SizeLimit = 1024;
options.MaximumBodySize = 1024 * 1024 * 100;
options.UseCaseSensitivePaths = false;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCookiePolicy();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCaching();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器:
[ResponseCache(Duration = 100, NoStore = false)]
public IActionResult Index()
{
return View();
}
这是正常行为,是否从磁盘缓存中检索页面有多种因素影响。
我将尝试列出用户请求页面的常见场景以及它是否被缓存。
AddResponseCaching
和 UseResponseCaching
控制服务器端缓存,而 ResponseCacheAttribute
通过设置适当的 headers 控制客户端缓存。
我喜欢控制客户端缓存的方式是设置如下配置文件:
services.AddControllersWithViews(options =>
{
options.CacheProfiles.Add("Caching", new CacheProfile()
{
Duration = 120,
Location = ResponseCacheLocation.Any,
VaryByHeader = "cookie"
});
options.CacheProfiles.Add("NoCaching", new CacheProfile()
{
NoStore = true,
Location = ResponseCacheLocation.None
});
})
你可以这样使用它:
[ResponseCache(CacheProfileName = "Caching")]
Chrome 防止在没有证书的情况下使用 HTTPS 或证书无效时进行任何类型的缓存(在开发中,系统会提示您信任开发证书以便通过 HTTPS 工作)
浏览器在打开开发工具时禁用缓存,它们通常会发送 Cache-Control: no-cache
在 chrome 上点击重新加载 (Ctrl + F5) 也会禁用缓存并发送一个 Cache-Control: max-age=0
header 以及服务器尊重的请求。
如果您想测试缓存是否真的有效,请执行以下操作:
打开您的 Chrome 开发工具并取消选中 Disable cache
复选框(如果已选中)。
像往常一样请求您的页面。
不要尝试通过引用同一页面的页面中的锚标记请求同一页面(您应该看到该页面是从磁盘缓存中检索的)(如果满足以下条件) .
您还可以从另一个页面导航到您的(缓存激活的)页面,如果它被缓存,它将从磁盘缓存中提取(如果满足以下条件)。
带有表单的页面 and/or 任何类型的授权都不会被缓存。包含防伪令牌的页面将与 Cache-Control
和 Pragma
header 一起发送到 no-cache
。在MSDN.
上可以看到缓存的所有条件
还有一些工具如 Fiddler 和 Postman 默认发送 no-cache
header。
我试图了解 .NET Core 3.1 中的响应缓存。但它没有像我希望的那样工作。我在 Chrome devtool 中查看了网络,它显示响应 header 和 cache-control: no-cache, no-store
。
我还发现 Response header 与 Actionfilter 中的 HeaderCacheControl{public,max-age=100}
一起。这是我预期的值,但浏览器中的实际响应 header 是 no-cache
.
Startup
class:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching(options=>
{
options.SizeLimit = 1024;
options.MaximumBodySize = 1024 * 1024 * 100;
options.UseCaseSensitivePaths = false;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCookiePolicy();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCaching();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器:
[ResponseCache(Duration = 100, NoStore = false)]
public IActionResult Index()
{
return View();
}
这是正常行为,是否从磁盘缓存中检索页面有多种因素影响。
我将尝试列出用户请求页面的常见场景以及它是否被缓存。
AddResponseCaching
和UseResponseCaching
控制服务器端缓存,而ResponseCacheAttribute
通过设置适当的 headers 控制客户端缓存。
我喜欢控制客户端缓存的方式是设置如下配置文件:
services.AddControllersWithViews(options =>
{
options.CacheProfiles.Add("Caching", new CacheProfile()
{
Duration = 120,
Location = ResponseCacheLocation.Any,
VaryByHeader = "cookie"
});
options.CacheProfiles.Add("NoCaching", new CacheProfile()
{
NoStore = true,
Location = ResponseCacheLocation.None
});
})
你可以这样使用它:
[ResponseCache(CacheProfileName = "Caching")]
Chrome 防止在没有证书的情况下使用 HTTPS 或证书无效时进行任何类型的缓存(在开发中,系统会提示您信任开发证书以便通过 HTTPS 工作)
浏览器在打开开发工具时禁用缓存,它们通常会发送
Cache-Control: no-cache
在 chrome 上点击重新加载 (Ctrl + F5) 也会禁用缓存并发送一个
Cache-Control: max-age=0
header 以及服务器尊重的请求。如果您想测试缓存是否真的有效,请执行以下操作:
打开您的 Chrome 开发工具并取消选中
Disable cache
复选框(如果已选中)。像往常一样请求您的页面。
不要尝试通过引用同一页面的页面中的锚标记请求同一页面(您应该看到该页面是从磁盘缓存中检索的)(如果满足以下条件) .
您还可以从另一个页面导航到您的(缓存激活的)页面,如果它被缓存,它将从磁盘缓存中提取(如果满足以下条件)。
带有表单的页面 and/or 任何类型的授权都不会被缓存。包含防伪令牌的页面将与
上可以看到缓存的所有条件Cache-Control
和Pragma
header 一起发送到no-cache
。在MSDN.还有一些工具如 Fiddler 和 Postman 默认发送
no-cache
header。