200 OK (from cache)原因
Meta标签中的http-equiv用来标记不可缓存或过期时间,但效果一般。而且代理缓存基本不访问HTML文档内容,所以尽量少用meta标签控制缓存。
Pragma: no-cache
Forces caches to submit the request to the origin server for validation before releasing a cached copy.
Pragma is not specified for HTTP responses .Use[a1] Pragma only for backwards compatibility with HTTP/1.0 clients.
Cache-Control
used to specify directives for caching mechanisms in both, requests and responses. Caching directives are unidirectional(单向), meaning that a given directive in a request is not implying that the same directive is to be given in the response.
通用指令
Cache-Control: max-age=<seconds>
Specifies the maximum amount of time a resource will be considered fresh. Contrary to Expires, this directive is relative to the time of the request.
Cache-Control: no-cache(数据会被缓存)
Forces caches to submit the request to the origin server for validation before releasing a cached copy.
Cache-Control: no-store
The cache should not store anything about the client request or server response.
Cache-Control: no-transform
Cache-Control: max-age=<seconds>
request指令
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-Control: only-if-cached
response指令
Cache-Control: must-revalidate
The cache must verify the status of the stale resources before using it and expired ones should not be used.
Cache-Control: public
Cache-Control: private
Cache-Control: proxy-revalidate
Cache-Control: s-maxage=<seconds>
Expires: <http-date>
The Expires header contains the date/time after which the response is considered stale.
Invalid dates, like the value 0, represent a date in the past and mean that the resource is already expired.
If there is a Cache-Control header with the "max-age" or "s-max-age" directive in the response, the Expires header is ignored.
[a1]请求缓存和响应缓存有什么不同???
带条件请求和无条件请求
An unconditional request is made when the client browser does not have a cached copy of the resource available locally. In this case, the server is expected to return the resource with a HTTP/200 OK response. If the response’s headers permit it, the client may cache this response in order to reuse it later.
If the browser later needs a resource which is in the cache, but that response is expired (older than its max-age or past the Expires date), then the client will make a conditional request to the server to determine whether the previously cached response is still valid and should be reused. The conditional request contains an If-Modified-Since and/or If-None-Match header that indicates to the server what version of the content the browser already has in its cache. The server can indicate that the client’s copy is still fresh by returning HTTP/304 Not Modified headers with no body, or it can indicate that the client’s copy is stale by returning a HTTP/200 OK response with the new version of the resource.
In the first case (Normal-Refresh,F5), we will perform HTTP requests (conditional, if possible) to revalidate all of the resources on the page, regardless of freshness.In the second case (Super-Refresh,Ctrl+F5,强制刷新), we perform unconditional HTTP requests to redownload all of the content on the page, bypassing the cache altogether.
区分响应是来自原始服务器还是代理服务器?
The Date general-header field represents the date and time at which the message(报文) was originated.
The Age header contains the time in seconds the object has been in a proxy cache. If it is Age: 0, it was probably just fetched from the origin server; otherwise It is usually calculated as a difference between the proxy's current date and the Date general header included in the HTTP response.
200 OK (from cache) vs 304 Not Modified
200 OK (from cache) 是浏览器没有跟服务器确认,直接用了浏览器缓存,最快。一般在expires/max-age头部有效时不会发送请求。因为请求根本没有产生,所以在chrome下请求头部会显示:Provisional headers are shown。
304 Not Modified 是浏览器和服务器确认了一次缓存的有效性,再使用缓存,多一次确认(If-Modified-Since/etag)请求。
为什么有"Last-Modified"时也会有200 OK from cache?
不同的浏览器会根据不同的用户行为(刷新/强制刷新)对请求添加不同的头部,对响应采用不同的缓存策略。
When "Expires" and "Cache-Control" headers are not specified, but a "Last-Modified" header is specified, browsers have to guess at how long they should keep the document in cache. Some browsers do use algorithms that let the page remain in cache for a day or more.