response header 来缓存响应?
response header to cache the response?
在我的网络应用程序的一些页面中,我有两种浏览器缓存方案
场景一:-
如果服务器端没有修改,我想从浏览器缓存中获取服务器。例如:-
- 用户为所有员工发出请求
- 回复returns 10 名员工
- 用户再次为所有员工发出请求
- 预计这次应该从浏览器缓存中提供服务
- 用户又创建了一名员工
- 用户再次为所有员工发出请求
- 预计这次应该从服务器而不是浏览器缓存提供最新服务
我打算在下面使用 header
response.setHeader("Cache-Control", "no-cache");
作为
no-cache is not instructing the browser or proxies about whether or
not to cache the content. It just tells the browser and proxies to
validate the cache content with the server before using it
场景 2 :-
但是对于一些我根本不想缓存的敏感页面,我打算在下面使用header
response.setHeader("Cache-Control", "no-store");
但下面的一些文章可以安全使用 header 以使其适用于所有浏览器。所以我将在下面使用
response.setHeader("Cache-Control", "no-cache, no-store");
我提议的实施是否正确?
对于场景 #1,您确实需要将 Cache-Control
设置为 no-cache
(或设置 max-age
以获得更好的可扩展性,但在这种情况下,您将不需要最新值)但您还需要使用 HTTP header ETag 以允许浏览器检查数据内容是否已更改,以便浏览器将能够知道缓存条目是否可以重用。
对于场景#2,您需要将 Cache-Control
设置为 no-store
以防止浏览器缓存数据,因为这是标准方式,但确实如此no-cache, no-store
如果您也需要支持它们,将有助于在旧浏览器上工作。
在我的网络应用程序的一些页面中,我有两种浏览器缓存方案
场景一:- 如果服务器端没有修改,我想从浏览器缓存中获取服务器。例如:-
- 用户为所有员工发出请求
- 回复returns 10 名员工
- 用户再次为所有员工发出请求
- 预计这次应该从浏览器缓存中提供服务
- 用户又创建了一名员工
- 用户再次为所有员工发出请求
- 预计这次应该从服务器而不是浏览器缓存提供最新服务
我打算在下面使用 header
response.setHeader("Cache-Control", "no-cache");
作为
no-cache is not instructing the browser or proxies about whether or not to cache the content. It just tells the browser and proxies to validate the cache content with the server before using it
场景 2 :- 但是对于一些我根本不想缓存的敏感页面,我打算在下面使用header
response.setHeader("Cache-Control", "no-store");
但下面的一些文章可以安全使用 header 以使其适用于所有浏览器。所以我将在下面使用
response.setHeader("Cache-Control", "no-cache, no-store");
我提议的实施是否正确?
对于场景 #1,您确实需要将 Cache-Control
设置为 no-cache
(或设置 max-age
以获得更好的可扩展性,但在这种情况下,您将不需要最新值)但您还需要使用 HTTP header ETag 以允许浏览器检查数据内容是否已更改,以便浏览器将能够知道缓存条目是否可以重用。
对于场景#2,您需要将 Cache-Control
设置为 no-store
以防止浏览器缓存数据,因为这是标准方式,但确实如此no-cache, no-store
如果您也需要支持它们,将有助于在旧浏览器上工作。