服务器可以通过HTTP定义的几种方式来指定文档在过期之前可以将其缓存多长时间,按照优先级递减的顺序,服务器可以:
- 附加一个Cache-Control : no-store 首部到响应中去
- 附加一个Cache-Control : no-cache 首部到响应中去
- 附加一个Cache-Control : must-revalidate 首部到响应中去
- 附加一个Cache-Control : max-age 首部到响应中去
- 附加一个Expires日期到响应中去
- 不附加过期信息,让缓存自己定义过期信息
Cache-Control请求指令
指令 描述
Cache-Control : max-stale
Cache-Control : max-stale=<s> 缓存可以随意提供过期的文档。如果指定的参数<s>,在这段时间内,文档就不能过期,这条指令放松的缓存的规则
Cache-Control : min-fresh=<s> 至少在未来<s>秒内文档要保持新鲜,这使得缓存规则更加严格了
Cache-Control : max-age=<s> 缓存文档在<s>秒内有效,时间计算从第一次生成缓存文档开始算起,这种指令使得缓存规则更加严格;
Cache-Control : s-maxage=<s> s-maxage与max-age类似,但适于共享缓存使用。
Cache-Control : no-cache 除非资源进行了再验证,否则这个客户端不会接受已缓存的资源,HTTP/1.1提供Pragma:no-cache首部为了兼容
Pragma : no-cache HTTP/1.0+,除了为与HTTP/1.0应用程序交互时,都应该使用Cache-Control:no-cache首部
Cache-Control : no-store 缓存应该尽快从存储器中删除文档的所有踪迹,因为其中可以包含敏感信息
Cache-Control : only-if-cached 只有缓存有副本存在时,客户端才会获取一份副本
Cache-Control : must-revalidate 在事先没有与原始服务器再验证情况下,不能提供文档的陈旧副本
指令 | 描述 |
Cache-Control : max-stale Cache-Control : max-stale=<s> | 缓存可以随意提供过期的文档。如果指定的参数<s>,在这段时间内,文档就不能过期,这条指令放松的缓存的规则 |
Cache-Control : min-fresh=<s> | 至少在未来<s>秒内文档要保持新鲜,这使得缓存规则更加严格了 |
Cache-Control : max-age=<s> Cache-Control : s-maxage=<s> | 缓存文档在<s>秒内有效,时间计算从第一次生成缓存文档开始算起,这种指令使得缓存规则更加严格; s-maxage与max-age类似,但适于共享缓存使用。 |
Cache-Control : no-cache Pragma : no-cache | 除非资源进行了再验证,否则这个客户端不会接受已缓存的资源,HTTP/1.1提供Pragma:no-cache首部为了兼容HTTP/1.0+,除了为与HTTP/1.0应用程序交互时,都应该使用Cache-Control:no-cache首部 |
Cache-Control : no-store | 缓存应该尽快从存储器中删除文档的所有踪迹,因为其中可以包含敏感信息 |
Cache-Control : only-if-cached | 只有缓存有副本存在时,客户端才会获取一份副本 |
Cache-Control : must-revalidate | 在事先没有与原始服务器再验证情况下,不能提供文档的陈旧副本 |