一、cache-control
cache-control
是web性能优化的一部分,通用消息头字段,被用于在http请求和响应
中,通过指定指令来实现缓存
机制。缓存指令是单向
的,这意味着在请求中设置的指令,不一定被包含在响应中。
指令不区分大小写
,并且具有可选参数,可以用令牌或者带引号的字符串语法。多个指令以逗号
分隔。
详细文档查看: cache-control(MDN)
缓存请求指令
客户端可以在HTTP请求
中使用的标准 Cache-Control
指令。
PS: 目前只用到max-age
,等用到其他指令再补充解释
Cache-Control: max-age=<seconds> : 设置过期时长,默认单位是秒,直接截断请求,直接从内存中返回。
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: only-if-cached
缓存响应指令
服务器可以在响应
中使用的标准 Cache-Control
指令。
Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>
【注意】不允许给首页设置缓存,因为设置缓存期间用户没办法去获取最新得网页信息。尤其是html文件,不能设置。一般js和css设置十年时间,如果要更新js和css,就改一下这两个的url(加上?v=1)。还有一种方式就是在文件名加一个随机数(比如:1fdsgdc.txt)。
二、Expires
Expires
响应头包含日期/时间, 即在此时候之后,响应过期。
无效的日期,比如 0, 代表着过去的日期,即该资源已经过期。---可以用来删除Cookie。
语法: Expires: <http-date>
<http-date>指一个 HTTP-日期 时间戳(GMT格式的)
示例:
Expires: Wed, 21 Oct 2015 07:28:00 GMT
【
cache-control
和expires
同时存在时,优先使用cache-control
,它是新的API】
网友评论