Key In Http Protocol (一):'Cache-Control'
Brief
HTTP (Hypertext Transfer Protocol) is perhaps the most popular application protocol used in the Internet (or The WEB).
HTTP is an asymmetric request-response client-server protocol as illustrated. An HTTP client sends a request message to an HTTP server. The server, in turn, returns a response message.
In other words, HTTP is a pull protocol, the client pulls information from the server (instead of server pushes information down to the client).
HTTP is a client-server application-level protocol.
① http response message
② http request message
‘Cache-Control’ Key
Cache-Control header was defined as part of the HTTP/1.1 specification and supersedes previous headers (e.g. Expires) used to define response caching policies. All modern browsers support Cache-Control, hence that is all we will need.
Each resource can define its caching policy via Cache-Control HTTP header Cache-Control directives control who can cache the response, under which conditions, and for how long.
Cache-Control 指令 | 说明 | |
---|---|---|
max-age=86400 | 浏览器和任何intermediary caches缓存均可以将响应(如果是public的)缓存长达一天(60 秒 x 60 分 x 24 小时) | |
private, max-age=600 | 客户端浏览器只能将响应缓存最长 10 分钟(60 秒 x 10 分) | |
no-store | 不允许缓存响应,每个请求必须获取完整的响应。 | |
no-cache | 表示必须先与服务器确认返回的响应是否被更改,然后才能使用该响应来满足后续对同一个网址的请求。因此,如果存在合适的验证令牌 (ETag),no-cache 会发起往返通信来验证缓存的响应,如果资源未被更改,可以避免下载。 |
Defining optimal Cache-Control policy
浏览器发出的所有 HTTP 请求会首先被路由到浏览器的缓存,以查看是否缓存了可以用于实现请求的有效响应。如果有匹配的响应,会直接从缓存中读取响应,这样就避免了网络延迟以及传输产生的数据成本。然而,如果我们希望更新或废弃已缓存的响应,该怎么办?
例如,假设我们已经告诉访问者某个 CSS 样式表缓存长达 24 小时 (max-age=86400),但是设计人员刚刚提交了一个更新,我们希望所有用户都能使用。我们该如何通知所有访问者缓存的 CSS 副本已过时,需要更新缓存? 很简单,在资源内容更改时,我们可以更改资源的网址,强制用户下载新响应。通常情况下,可以通过在文件名中嵌入文件的指纹码(或版本号)来实现 - 例如 style_x234dff.css。 图片:
上述方案的具体实践,可参考百度的FIS3。
Caching checklist:
- Use consistent URLs
- Ensure the server provides a validation token (ETag)
- Identify which resources can be cached by intermediaries
- Determine the optimal cache lifetime for each resource
- Determine the best cache hierarchy for your site
- Minimize churn
附录