http缓存(http caching)

通过使用缓存web网站和web应用的性能能够得到显著的提升。Web caches能够减小延迟和网络流量,从而缩短展示资源所花费的时间。

在http中控制缓存行为的首部字段是Cache-Control,Cache-Control可以有多个指令,指令之间用逗号分割。Cache-Control是通用首部字段,它即能出现在响应头中也能出现在请求头中

一.缓存请求指令

  1. Cache-Control: max-age=<seconds>
  2. Cache-Control: max-stale[=<seconds>]
  3. Cache-Control: min-fresh=<seconds>
  4. Cache-Control: no-cache
  5. Cache-Control: no-store
  6. Cache-Control: no-transform
  7. Cache-Control: only-if-cached

二.缓存响应指令

  1. Cache-Control: must-revalidate
  2. Cache-Control: no-cache
  3. Cache-Control: no-store
  4. Cache-Control: no-transform
  5. Cache-Control: public
  6. Cache-Control: private
  7. Cache-Control: proxy-revalidate
  8. Cache-Control: max-age=<seconds>
  9. Cache-Control: s-maxage=<seconds>

三.指令介绍

1.与缓存能力相关的指令

1.public:指明任何缓存区都能够缓存响应

2.private:指明响应是针对单一用户的,不能存储在共享缓存区中,只有私有缓存区能存储响应

3.no-cache:在使用缓存资源之前要向源服务器验证缓存的有效性

4.only-if-cached:指明客户端只想从缓存中获取响应,不需要与源服务器联系确定资源是否更新

2.与到期时间相关的指令

1.max-age=<seconds>:指定资源被视为有效的最大时间量,单位为秒

2.s-maxage=<seconds>:与max-age=<seconds>指令的作用相似,但是它只对共享缓存有效,对私有缓存无效

3.max-stale[=<seconds>]:即使缓存资源过期客户端还是接受缓存区中的资源。如果未指定数值,那么无论过期多久,客户端都接受缓存区中的响应,如果指定了具体数值,那么即使过期,只要处于max-stale指定的时间内,客户端还是接受缓存区中的资源

4.min-fresh=<seconds>:表明从缓存区中返回未过指定时间的缓存资源

5.stale-while-revalidate=<seconds>:指示客户端愿意接受一个过期的响应,同时在后台异步检查一个新的响应。秒值表示客户机愿意接受过期响应的时间。

6.stale-if-error=<seconds>:如果检查新资源失败,客户端愿意接受过期的资源。秒值指示客户端在初始过期后愿意接受过期响应的时间。

3.与重新验证和重新加载相关的指令

1.must-revalidate:在使用过期的缓存资源之前,必须向源服务器确认缓存资源的状态并且不会使用过期的资源。它会忽略max-stale[=<seconds>]指令

2.proxy-revalidate:和must-revalidate作用类似,但是它只应用于共享缓存,会被私有缓存忽略

3.immutable:指明在任何时候都不能改变响应体

4.其他指令

1.no-store:不缓存请求和响应中的任何内容

2.no-transform:缓存不能改变请求和响应中的任何实体主体(The Content-Encoding, Content-Range, Content-Type 头部字段不能被修改)

posted @ 2018-03-06 20:47  QxQstar  阅读(723)  评论(1编辑  收藏  举报