• Exceptional Performance--Expires Or Cache-COntrol Header - [xnix]

    2008-05-18

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://timoqi.blogbus.com/logs/21103620.html

    翻译自:http://developer.yahoo.com/performance/rules.html

    译者:Timo

    发布格式:黑色为英文原文,红色为中文翻译

    发布时间:从2008年5月16日每天1个章节,共10天

    发布协议:new bsd

    Add an Expires or a Cache-Control Header

    增加一个断开或者缓存控制头

    tag: server

    tag:服务器

    There are two things in this rule:

    • For static components: implement "Never expire" policy by setting far future Expires header

    • For dynamic components: use an appropriate Cache-Control header to help the browser with conditional requests

    在这条法则中有两件事情:

    • 对于静态组件:在不远的断开设置中实现“从不断开”策略

    • 对于动态组件:使用一个合适的高速缓存控制头来帮助浏览器进行有条件的请求。

    Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on all components including scripts, stylesheets, and Flash components.

    web页面被设计的越来越庞大,这意味着含有更多的脚本,样式表,图片和flash在页面中。第一次访问你页面的用户将需要做很多的http请求,但是使用“断开Header”将使你的组件保存在缓存中。这个就避免了在接下来查看页面中不必要的HTTP请求。“断开Header”经常被用在图像上,但是它同样可以用在所有组件中如脚本,样式表和Flash上等等。

    Browsers (and proxies) use a cache to reduce the number and size of HTTP requests, making web pages load faster. A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. This is a far future Expires header, telling the browser that this response won't be stale until April 15, 2010.

          Expires: Thu, 15 Apr 2010 20:00:00 GMT
    浏览器(和代理)使用缓存来减少HTTP请求的数量和大小,让web页面加载更快。一个web服务器使用“断开Header”HTTP响应中来告诉客户端一个组件要被缓存多久。下面是一个将来的”断开header”来告诉浏览器它将在2010415日才失效。
    	Expires: Thu, 15 Apr 2010 20:00:00 GMT

    If your server is Apache, use the ExiresDefault directive to set an expiration date relative to the current date. This example of the ExpiresDefault directive sets the Expires date 10 years out from the time of the request.

          ExpiresDefault "access plus 10 years"
    如果你的服务器是Apache,使用断开默认指令去设置一个基于当前时间的终止日期。下面这个例子是断开默认指令设置10年后才请求过期。
    	ExpiresDefault "access plus 10 years"

    Keep in mind, if you use a far future Expires header you have to change the component's filename whenever the component changes. At Yahoo! we often make this step part of the build process: a version number is embedded in the component's filename, for example, yahoo_2.0.6.js.

    值得注意的是,如果你使用的是将来断开header无论何时改变组件你将不得不改变组件的文件名。在Yahoo!我们建立一个处理流程来实施这一步:版本号内嵌到组件文件名中,例如:yahoo_2.0.6.js





    Using a far future Expires header affects page views only after a user has already visited your site. It has no effect on the number of HTTP requests when a user visits your site for the first time and the browser's cache is empty. Therefore the impact of this performance improvement depends on how often users hit your pages with a primed cache. (A "primed cache" already contains all of the components in the page.) We measured this at Yahoo! and found the number of page views with a primed cache is 75-85%. By using a far future Expires header, you increase the number of components that are cached by the browser and re-used on subsequent page views without sending a single byte over the user's Internet connection.

    只用当用户浏览过你的网站后使用将来断开header才能影响页面观感。当用户首次访问你的网站是它是没有任何作用的对于减少HTTP请求数量,这时候浏览器的缓存是空的。因此性能提升的效果依赖于用户多久访问你网站来命中缓存的页面。(缓存的页面包含了页面中所有的组件)在Yahoo!我们测试过发现缓存页面的命中在75-85%。通过使用将来断开header,你将增加被浏览器缓存的组件数量同时在接下来的页面观感中被复用而无须发送任何字节通过用户的连接。


    收藏到:Del.icio.us




发表评论

您将收到博主的回复邮件
记住我