杠杆浏览器缓存 - 到期或最大年龄,最后一次修改或ETAG [英] leverage browser caching - expires or max-age, last-modified or etag

查看:149
本文介绍了杠杆浏览器缓存 - 到期或最大年龄,最后一次修改或ETAG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到一个明确的,实际的解释什么是利用浏览器缓存来提高速度的正确方法。

根据本网站


  

要指定过期或缓存控制最大年龄之一,它是非常重要的,
  与之一的Last-Modified或ETag的,所有的缓存资源。它是
  多余的同时指定过期和Cache-Control:max-age的,或
  同时指定Last-Modified和ETag的。


这是正确的?如果是这样,我应该使用过期最大年龄?我觉得我有什么。这都是一个大致的了解,但不知道这通常是最好用。

如果我也做的Last-Modified 的ETag ,那些哪一个?我觉得我得到的Last-Modified ,但我仍对这个非常模糊的ETag 的概念。

另外,哪些文件应该启用浏览器缓存?


解决方案

  

这是正确的?


是的,Expires和最大年龄做同样的事情,但在两种不同的方式。与Last-Modified和Etag的同样的事情。


  

如果是这样,我该怎么办
  到期或最大年龄?


到期取决于用户的时钟精度,所以它主要是一个不错的选择(因为大多数浏览器都支持HTTP / 1.1)。使用最大年龄,告诉该文件是好的,很多秒钟浏览器。例如,1天的高速缓存将是:

缓存控制:最大年龄= 86400

请注意,当两个的Cache-Control 过期是present,的Cache-Control 花费precedence。


  

如果我也必须做的Last-Modified或ETag的,那些是哪一个?我觉得我得到的Last-Modified


您说得对,最后修改应该会更好。虽然这是一个时间,它是由服务器发送。因此,与用户的时钟没有问题。浏览器发送的Last-Modified服务器发送的最后一次请求的文件,如果是一样的,服务器answsers空响应«304未修改»


  

另外,哪些文件应该启用浏览器缓存?


所有文件都可以受益缓存。你有两个不同的方法:


  • 与最大年龄:对于文件永远不会改变有用(图片,CSS,JavaScript的)。只要将max-age值,浏览器不会向服务器发送任何请求。所以,你会非常快看到第二加载页面加载。如果您需要更新文件,只需添加一个问号,更改日期(例如/image.png?20110602,或者更好的代理缓存,像/20110602/image.png或/image.20110602.png) 。这种方式可以使文件过期,如果是紧急的(记住,浏览器几乎从不打服务器,一旦它有一个最大年龄文件)。主要用途是要加快速度,并限制请求发送到服务器。

  • 用的Last-Modified:可以对所有文件(包括那些具有最大年龄)来设定。即使你有动态网页,您可能不会更改文件的一段时间(即使它是10分钟)的内容,所以这可能是有益的。这里的主要用途是告诉浏览器«不断地问我要这个文件,如果是新的,我给你新一»。因此,有在每个页面加载发送的请求,但得到的答复是空的,如果该文件已经是不错的(304未修改),所以你节省带宽。

您缓存越多,速度越快你的页面会显示出来。但是,这是一个艰巨的任务,刷新缓存,因此要谨慎使用。

一个学习的好地方这一切都与许多解释: http://www.mnot.net/cache_docs/

I'm having difficulty finding a clear-cut, practical explanation for what is the correct way to leverage browser caching to increase page speed.

According to this site:

It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources. It is redundant to specify both Expires and Cache-Control: max-age, or to specify both Last-Modified and ETag.

Is this correct? If so, should I use Expires or max-age? I think I have a general understanding of what both of those are but don't know which is usually best to use.

If I have to also do Last-Modified or ETag, which one of those? I think I get Last-Modified but am still very fuzzy on this ETag concept.

Also, which files should I enable browser caching for?

解决方案

Is this correct?

Yes, Expires and max-age do the same thing, but in two different ways. Same thing with Last-Modified and Etag

If so, should I do expires or max-age?

Expires depends on accuracy of user's clock, so it's mostly a bad choice (as most browsers support HTTP/1.1). Use max-age, to tell the browser that the file is good for that many seconds. For example, a 1 day cache would be:

Cache-Control: max-age=86400

Note that when both Cache-Control and Expires are present, Cache-Control takes precedence. read

If I have to also do Last-Modified or ETag, which one of those? I think I get Last-Modified

You're right, Last-Modified should be better. Although it's a time, it's sent by the server. Hence no issue with user's clock. The browser sends the Last-Modified the server sent last time it asked for the file, and if it's the same, the server answsers with an empty response «304 Not Modified»

Also, which files should I enable browser caching for?

All files can benefit caching. You've got two different approaches:

  • with max-age: useful for files that never change (images, CSS, javascript). For as long as the max-age value, the browser won't send any request to the server. So you'll see the page loading really fast on the second load. If you need to update files, just append a question mark, and the date of change (for example /image.png?20110602, or for better proxies caching, something like /20110602/image.png or /image.20110602.png). This way you can make files expire if it's urgent (remember that the browser almost never hits the server once it has a max-age file). Main use is to speed things up and limit requests sent to the server.
  • with Last-Modified: can be set on all files (including those with max-age). Even if you have dynamic pages, you may not change the content of the file for a while (even if it's 10 min), so that could be useful. The main use here is to tell the browser «keep asking me for this file, if it's new, I'll send you the new one». So, there's a request sent on each page load, but the answer is empty if the file is already good (304 Not Modified), so you save on bandwidth.

The more you cache, the faster your pages will show up. But it's a difficult task to flush caches, so use with care.

A good place to learn all this with many explanations: http://www.mnot.net/cache_docs/

这篇关于杠杆浏览器缓存 - 到期或最大年龄,最后一次修改或ETAG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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