Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP缓存机制 #6

Open
AILINGANGEL opened this issue May 27, 2019 · 0 comments
Open

HTTP缓存机制 #6

AILINGANGEL opened this issue May 27, 2019 · 0 comments

Comments

@AILINGANGEL
Copy link
Owner

http缓存分为强缓存和协商缓存,强缓存的优先级高于协商缓存

强缓存有两种方式: expires响应头(http1.0)和cache-control(http1.1)

  • expires
    服务器在请求头上带上这个字段告诉浏览器什么时间过期

  • cache-control
    max-age字段设置多少s之后缓存失效

协商缓存也有两种方式: last-modified和if-modified-since 以及 etag和if-none-match

  • last-modified和if-modified-since
    如果强缓存过期就浏览器就会发送请求到服务器并带上if-modified-since字段,服务器收到之后会返回last-modified字段,如果没炒作if-modified-since字段就会告诉浏览器当前的缓存还是有效的,服务器没有修改过

这种方式存在的问题就在于时间精度问题,如果在1s内修改了文件服务器无法检测出来

  • etag和if-none-match
    服务器为每个文件生成一个etag值,只要文件不变这个etag的值就不变。浏览器请求资源的时候会在if-none-match中带上这个etag值,如果服务器检测发现etag没变就会告诉浏览器当前缓存还是有效的

协商缓存和http 304相关联

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant