使用缓存文件还是一个HTTP请求? [英] Use cache file or one more HTTP Request?

查看:180
本文介绍了使用缓存文件还是一个HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有加速您的网站网站和图书上,他们总是告诉我们不惜一切代价最小化HTTP请求。这很好,但是如果这意味着在每个页面上你必须一次又一次地重新加载120kb,因为用户缓存为空?

on all the "speed up your website" sites and books they always tell us to minimize HTTP requests at all costs. This is fine and nice but what if that means that on every page you have to reload 120kb again and again and again because the user cache is empty?

如果我使用5在我的网站的每个页面上都有js文件,最好不要将它们放在一个文件中并在每个页面上加载这个文件,而不是将它们与所有其他变量文件放在一个大文件中,并保存一个HTTP请求。从哪一点或文件大小就可以缓存一个文件并有另一个HTTP请求?

If i use 5 js files on every page of my website, wouldnt it be better to put them in one file and load this file on every page instead of putting them together with all other variable files in one big file and save one HTTP request. From which point or filesize on is it ok "cache" a file and have another HTTP request?

当我只使用一个HTTP请求时,我给你一个3页的例子每页一个minifed JS文件:

I give you an example of 3 pages when i use only one HTTP request for one minifed JS file per page:


  1. jquery,jquery ui,thickbox,lavalamp menu =>一起缩小为一个文件= 300kb

  2. jquery,jquery ui,cycle plugin =>在一个文件中缩小=> 200kb

  3. jquery,jquery ui,galleria plugin =>一起缩小为一个file => 250kb

现在是另外两种可能,总是有2个HTTP请求:一个文件由jquery和jquery组成ui => 150kb,我们现在叫它jui.js

And now the other possibility with always 2 HTTP requests: One File consisting of jquery and jquery ui => 150kb, lets call it "jui.js" for now


  1. jui.js,thickbox,lavalamp =开始时再次300kb,但现在是jui。 js被缓存为其他2个页面

  2. (jui.js现在被缓存,所以没有加载),只有循环插件=>仅加载50kb,但是当我加载jui时再加载一个HTTP请求。 js和循环插件分开。

  3. (jui.js是alr eady cached),只加载galleria插件=>仅加载100kb,但再加上2个HTTP请求,其中一个请求已被缓存

所以在这一点或Kb大小是否可以在普通的响应Web服务器上发出另一个HTTP请求?

So at which point or Kb size is it ok to have another HTTP request on a normal "responsive" web server?

是否有人有任何最佳实践或只是最小化不惜一切代价提出HTTP请求!?

Does anybody have any best practices or is it just "Minimize HTTP requests at all costs!"?

(我希望自己明确表示:)我会在得到一些积分后立即投票给人们!)

(I hope i made myself clear :) And i will vote up people as soon as i have some points!)

编辑:

这基本上是一个更简单的问题:
多长时间需要额外的HTTP往返缓存js文件吗?如果http请求比我在每个页面上下载额外的非缓存部分所需的时间慢,那么我会在每个页面上放置一个大文件(每页上有一个不同的大文件)。

It is basicly a simpler question: How long does a extra HTTP roundtrip for a cached js file need? If the http request is slower than the time i would need to download the extra non cached parts on every page, then i would put everything in 1 big file on every page(1 different big file on every page).

如果缓存的js文件的HTTP请求几乎没有,那么我会在一个额外的js文件中拆分每个页面需要的部分(当然是minifed)并包含每个部分的动态部分页面在differend(再次缩小)js文件。

If the HTTP request for a cached js file is nearly nothing, then i would split the parts that every page needs in an extra js file(minifed of course) and include the dynamic parts of every page in differend(again minified) js files.

因此,如果在大多数页面上我需要100kb额外(动态部分),我如何测试缓存的时间HTTP请求?有没有数字,有没有人测试过这样的东西?

So if on most pages i need a 100kb extra(the dynamic part), how do i test the time for a cached HTTP request? Are there any numbers, did anybody test something like this already?

感谢你们已经有了很好的答案!

Thanks for the great answers already!

推荐答案

这是一个很复杂的主题。他们写了关于这个主题的全书;)

This is big complex subject. They write whole books on this subject ;)

对于资源(javascript,css等),有时最好单独下载它们。浏览器将并行下载它们。如果页面a需要资源x y z但页面b只需要x和z,将它们分开是一件好事。其他时候,每个页面上所需的资源可能会更好地一次性下载。这取决于。

For resources (javascript, css etc) it is sometimes better to download them individually. The browser will download them in parallel. if page a needs resources x y z but page b only needs x and z, separating them out is a good thing. Other times a resource that is needed on every page might be better downloaded all at once. It depends.

但是使用javascript,浏览器会在呈现页面之前先下载JS(如果脚本标记在head部分中),那么你会看到更好的性能如果您添加延迟属性,或包含在页面底部,并使用body = onload触发您的javascript。

But with javascript, the browser downloads the JS first before it renders the page (if the script tag is in the head section) so you would see better performance if you add a defer attribute, or include at the bottom of the page, and trigger your javascript with a body=onload.

请记住,您也可以在资源上设置缓存标头所以浏览器会将它们缓存在内存或磁盘中。在许多情况下,这会产生巨大的差异。

Remember too you can set caching headers on resources so the browser will cache them in memory or disk. This makes a huge difference in many cases.

实际上没有严格的规则,只有一些指导方针。你最好的选择是测试!哪种方式比拨号更好,但在宽带上效果不佳。

There are really no hard and fast rules, just some guidelines. Your best bet is to test! what works better over dialup doesn't work as well over broadband.

Fiddler是一个不错的程序,如果您使用调制解调器,它将显示您的加载时间。

Fiddler is a nice program that will show you your loading times if you are on a modem for instance.

这篇关于使用缓存文件还是一个HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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