http 缓存

缓存处理步骤

1 接收:读取请求报文

2 解析:解析报文,提取 url 和首部

3 查询:查询是否有本地副本可用(metadata)

4 新鲜度检测

5 创建响应

6 发送(cache-control ,age,expires,via)

7 日志

服务器配置

cache-control : max-age=0(秒数) no-store(禁止缓存对响应进行复制)
no-cache(do-not-serve-from-cache-without-revalidation)
must-revalidate(504)
expires(响应失效时间)
age
优先级

1
2
3
cache-control:no-store ->   cache-control:no-cache ->
cache-control: must-revalidate -> cache-control:max-age ->
expires(响应失效时间)

客户端配置

Cache-Control : no-cache
If-Modified-Since :’Mon, 26 Jul 1997 05:00:00 GMT’
pragma: no-cache

IFRAME 配置

1
2
3
4
5
6
7
<meta http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT" />
<meta http-equiv="Last-Modified" content="Sat, 10 Nov 1997 09:08:07 GMT" />
<meta
http-equiv="Cache-Control"
content="no-store, no-cache, must-revalidate"
/>
<meta http-equiv="Pragma" content="no-cache" />

相互影响

http 请求 http 返回
if-modified-since expires last-modified
if-none-match etag(实体标记)

最终解决方案

  • 在前端配置
    Cache-Control : no-cache
    If-Modified-Since :’Mon, 26 Jul 1997 05:00:00 GMT’
    pragma: no-cache

  • 服务器端配置(api 的 http get 请求 )
    在服务器 response 头部添加
    cache-control : max-age=0, no-cache ,must-revalidate
    expires:0
    或者 nginx 配置 response 头部

参考资料 1
参考资料 2
参考资料 3
nginx 配置 ETag