Memory Cache & Disk Cache
">
当我们 F12 查看浏览器网络请求的时候,肯定看到过这样的信息,from memory cache(内存缓存)和 from disk cache(磁盘缓存)。
当请求命中强缓存时,浏览器就会从内存或者磁盘中将缓存的资源返回来,请求不会到达服务器。
那么,哪些资源缓存在 memory,哪些缓存在 disk 呢?
关于 memory cache 和 disk cache,Chrome 官方有这么一段描述:
CachingChrome employs two caches — an on-disk cache and a very fast in-memory cache. The lifetime of an in-memory cache is attached to the lifetime of a render process, which roughly corresponds to a tab. Requests that are answered from the in-memory cache are invisible to the web request API. If a request handler changes its behavior (for example, the behavior according to which requests are blocked), a simple page refresh might not respect this changed behavior. To make sure the behavior change goes through, callhandlerBehaviorChanged()to flush the in-memory cache. But don't do it often; flushing the cache is a very expensive operation. You don't need to callhandlerBehaviorChanged()after registering or unregistering an event listener.
以上引用自 Chrome API
读取 memory 中的缓存资源,肯定要比读取 disk 中的更快,但是 memory 中的缓存,会随着进程的释放而释放,也就是说,一旦我们关闭 Tab 标签,memory 中的缓存也就没有了。
那么哪些资源会被缓存到 memory,哪些会缓存到 disk 中呢?关于这点我也没有找到定论,大多数的观点如下,供大家参考: