http缓存

2020-05-13  本文已影响0人  redpeanuts

如何让一个资源可缓存

HTTP 1.1 风格的Cache-Control 响应头中的 max-age指令
HTTP 1.0 风格的 Expires 响应头
Last-Modified响应头

缓存的组成

freshness + validation

freshness 缓存有效期

The freshness lifetime is calculated based on several headers. If a "Cache-control: max-age=N" header is specified, then the freshness lifetime is equal to N. If this header is not present, which is very often the case, it is checked if an Expires header is present. If an Expires header exists, then its value minus the value of the Date header determines the freshness lifetime. Finally, if neither header is present, look for a Last-Modified header. If this header is present, then the cache's freshness lifetime is equal to the value of the Date header minus the value of the Last-modified header divided by 10.
缓存有效期是根据多个header信息来计算得出,如果含有"Cache-control: max-age=N"则有效期等于N,如果不存在,多数情况下是不存在~~。
检查是否存在Expires,如果存在则有效期等于Expires值减去Date的值。如果两者都不存在,则检查Last-Modified是否存在,然后利用Date减去其值再除以10即:(Date-(Last-modified))/10

缓存失效时间

The expiration time is computed as follows:

expirationTime = responseTime + freshnessLifetime - currentAge

validation缓存校验

缓存过期后,请求会通过cache server向origin server 访问请求资源

vary

Vary头域值指定了一些请求头域,这些请求头域用来决定: 当缓存中存在一个响应,并且该缓存没有过期失效,是否被允许去利用此响应去回复后续请求而不需要重验证(revalidation)。
第一次请求时获取相应,在缓存了该响应的同时会缓存vary中存在的头部信息例如user-agent
第二次请求时,校验该请求中user-agent是否与第一次的相同,如果相同则可以直接使用缓存,否则需要从orifgin获取

参考

MDN Cache
Cache tutorial
Cache-contral : must-revalidate

上一篇 下一篇

猜你喜欢

热点阅读