为什么Chrome花费时间“下载”来自缓存的内容? [英] Why does Chrome spend time "downloading" content from cache?

查看:136
本文介绍了为什么Chrome花费时间“下载”来自缓存的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提供旨在由浏览器无限期缓存的静态内容。 Chrome按预期缓存它,但仍花时间下载它。我使用的是Chrome 46.0.2490.71。





当你可以看到,内容是从缓存中提供的,但内容下载仍需要68ms。这会导致每个页面加载时svg图像都会闪烁,即使文件被缓存。



以下是相关时间轴信息在隐身模式下加载页面:

< img src =https://i.stack.imgur.com/ZKP8C.pngalt =Chrome时间轴选项卡>

总计每个事件的时间和事件时间字段为零。使用本地提供的文件复制这个文件(但仍然从缓存中)接收数据事件只能看到一次。



有几点值得注意的地方:




  • 使用相同的编码从本地计算机提供相同的文件不会产生相同的下载延迟。
  • 切换到隐身模式(无扩展名)会将下载延迟减半,但不会将其消除。

  • 刷新页面更快,因为它似乎需要更少的时间来接收304而不是仅仅从缓存中加载它。

  • 关闭开发工具似乎对延迟没有任何影响。

  • IE 11,Edge和Firefox 41不会显示任何延迟。


可能存在什么原因?

解决方案

因此,这与缓存在Chrome中的工作方式有关。我没有Chrome代码库的个人经验,但我对它的理论知之甚少。 (我还发现Chrome的缓存实现在这里更引人好奇:铬盘缓存



以下是我在Chrome中加载实际Stack Overflow问题的截图,网络面板打开并且网络节流选项设置为离线。请注意,此列表中的每个条目均来自缓存!





您还会注意到Chrome 花时间下载每个文件。为什么是这样?那么,Chrome的缓存是一个数据库,并且该数据库也被压缩以节省空间。当您从缓存中检索文档时,该检索的价格不为零。 Chrome必须查找缓存数据库中的项目,然后将该条目充入内存,以便Chrome可以使用它。我不知道关于Network chrome-dev-tools面板如何显示时间的确切细节,但我猜想从磁盘获取该文件,解压缩该文件,然后解析并处理结果就是您所看到的内容反映在下载时间。



我不能评论为什么其他浏览器不会有这种延迟,因为我没有太多的经验他们。这可能是因为他们要么使用从缓存(可能)获取内容的更有效的方法,要么可能是他们始终将缓存保存在内存中(不太可能),或者他们跳过一些完整性检查正在缓存数据(可能)


I am serving static content intended to be cached by the browser indefinitely. Chrome is caching it as expected, but is still spending time "downloading" it. I am using Chrome 46.0.2490.71.

As you can see, the content is being served from cache, but still takes 68ms for content download. This is causing the svg images to flash in on every page load, even though the file is cached.

Here is the relevant timeline information from a page load in Incognito mode:

The "Total Time" and "Event Time" fields for each of those events is zero. Replicating this with the file served locally (but still from cache) the "Receive Data" event is only seen once.

A few interesting points to note:

  • Serving the same file from my local machine, with the same encoding, does not produce the same download delay.
  • Switching to Incognito mode (no extensions) halves the download delay, but does not eliminate it.
  • It is quicker to refresh the page, as it appears to take less time to receive a 304 response from the server than to merely load it from cache.
  • Closing the dev tools does not appear to have any effect on the delay.
  • IE 11, Edge, and Firefox 41 do not show any delay.

What possible causes could there be?

解决方案

So this has to do with the way caching works in Chrome. I don't have personal experience with Chrome's codebase, but I do know a bit about the theory of it. (I also found a reference to Chrome's cache implementation here for the more curious: chromium disk cache)

For reference, here is a screenshot of my loading your actual Stack Overflow question in Chrome with the Network panel open and the Network Throttling option set to "Offline". Notice that every single entry in this list is got from cache!

You'll also notice that Chrome is spending time "downloading" every file. Why is this? Well, Chrome's cache is a database, and that database is also compressed to save space. When you retrieve a document from cache, the price of that retrieval is not zero. Chrome has to look up the item in the cache database, and then inflate that entry into memory so Chrome can work with it. I don't know the exact details concerning how the Network chrome-dev-tools panel shows the times, but I would guess that getting that file from disk, uncompressing it, and then parsing and working with the result is what you're seeing reflected in "Time downloaded."

I can't comment on why other browsers don't also have this delay, since I don't have a lot of experience with them. It might be that they either use a more efficient method of getting things from cache (possible), or it could be that they keep the cache in memory at all times (unlikely), or that they're skipping some of the integrity checks Chrome is making on cache data (possible)

这篇关于为什么Chrome花费时间“下载”来自缓存的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆