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

缓存位置 #10

Open
sihai00 opened this issue Jul 25, 2019 · 0 comments
Open

缓存位置 #10

sihai00 opened this issue Jul 25, 2019 · 0 comments
Assignees

Comments

@sihai00
Copy link
Owner

sihai00 commented Jul 25, 2019

从缓存位置上来说分为四种,并且各自有优先级,当依次查找缓存且都没有命中的时候,才会去请求网络。

  1. Service Worker
  2. Memory Cache
  3. Disk Cache
  4. Push Cache

优先级:Service Worker > memory cache > disk cache

1.Service Worker

Service Worker 是运行在浏览器背后的独立线程(HTTPS或者localhost)。让我们自由控制缓存哪些文件、如何匹配缓存、如何读取缓存,并且缓存是持续性的。

2.Memory Cache

内存中的缓存。一旦我们关闭 Tab 页面,内存中的缓存也就被释放了。例如<link rel="prefetch">

3.Disk Cache

硬盘中的缓存。比之 Memory Cache 胜在容量和存储时效性上。

4.Push Cache

Push Cache(推送缓存)是 HTTP/2 中的内容。它只在会话(Session)中存在,一旦会话结束就被释放,并且缓存时间也很短暂。在Chrome浏览器中只有5分钟左右,同时它也并非严格执行HTTP头中的缓存指令。

  • 所有的资源都能被推送,并且能够被缓存,但是 Edge 和 Safari 浏览器支持相对比较差
  • 可以推送 no-cache 和 no-store 的资源
  • 一旦连接被关闭,Push Cache 就被释放
  • 多个页面可以使用同一个HTTP/2的连接,也就可以使用同一个Push Cache。这主要还是依赖浏览器的实现而定,出于对性能的考虑,有的浏览器会对相同域名但不同的tab标签使用同一个HTTP连接。
  • Push Cache 中的缓存只能被使用一次
  • 浏览器可以拒绝接受已经存在的资源推送
  • 你可以给其他域名推送资源

5.浏览器会把哪些文件丢进内存中?哪些丢进硬盘中?

关于这点,网上说法不一,不过以下观点比较靠得住:

  • 对于大文件来说,大概率是不存储在内存中的,反之优先
  • 当前系统内存使用率高的话,文件优先存储进硬盘

参考

深入理解浏览器的缓存机制

@sihai00 sihai00 self-assigned this Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant