缓存过期控制与最后修改 [英] Cache Expire Control with Last Modification

查看:217
本文介绍了缓存过期控制与最后修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apache的指定mod_expires 模块,还有就是过期指令有两个基地的时间段,访问修改

In Apache's mod_expires module, there is the Expires directive with two base time periods, access, and modification.

ExpiresByType text/html "access plus 30 days"

理解意味着缓存将在30天的新鲜内容请求。

understandably means that the cache will request for fresh content after 30 days.

然而,

ExpiresByType text/html "modification plus 2 hours"

不作直观的感觉。

doesn't make intuitive sense.

如何在浏览器缓存知道该文件已被修改,除非它向服务器请求?而如果是拨打电话到服务器,有什么用缓存该指令的?在我看来,我不理解缓存的一些重要组成部分。请赐教。

How does the browser cache know that the file has been modified unless it makes a request to the server? And if it is making a call to the server, what is the use of caching this directive? It seems to me that I am not understanding some crucial part of caching. Please enlighten me.

推荐答案

这是到期* 指令以改装为基础指的是文件的修改时间服务器。所以,如果你设定,比如,修改的加2分,该文件被修改后(在服务器上)请求在2小时内任何内容浏览器会缓存内容,直到文件的修改时间后2小时。而且那个时候是因为服务器与适当的到期时间发送过期头浏览器知道。

An Expires* directive with "modification" as its base refers to the modification time of the file on the server. So if you set, say, "modification plus 2 hours", any browser that requests content within 2 hours after the file is modified (on the server) will cache that content until 2 hours after the file's modification time. And the browser knows when that time is because the server sends an Expires header with the proper expiration time.

让我用一个例子来说明一下:说你的Apache配置包括行

Let me explain with an example: say your Apache configuration includes the line

ExpiresDefault modification plus 2 hours

和你有一个文件 index.html的 ExpiresDefault 指令应用于它,在服务器上。假设你在格林威治时间9:53上传版本 index.html的中,覆盖previous现有的 index.html的(如果有一个)。所以现在的修改时间的index.html 是9:53 GMT。如果你在服务器上运行的ls -l <​​/ code>(或 DIR 在Windows上),你会看到它在列表中

and you have a file index.html, which the ExpiresDefault directive applies to, on the server. Suppose you upload a version of index.html at 9:53 GMT, overwriting the previous existing index.html (if there was one). So now the modification time of index.html is 9:53 GMT. If you were running ls -l on the server (or dir on Windows), you would see it in the listing:

-rw-r--r--  1 apache apache    4096  Feb 18 09:53 index.html

现在,每个请求,Apache的发送的Last-Modified 头在文件的最后修改时间。既然你有一个 ExpiresDefault 指令,它也将发送过期头等于的修改时间的时间文件(9:53)加两个小时。因此,这里是浏览器看到的部分:

Now, with every request, Apache sends the Last-Modified header with the last modification time of the file. Since you have that ExpiresDefault directive, it will also send the Expires header with a time equal to the modification time of the file (9:53) plus two hours. So here is part of what the browser sees:

Last-Modified: Wed, 18 Feb 2009 09:53:00 GMT
Expires: Wed, 18 Feb 2009 11:53:00 GMT

如果在该浏览器使得这个请求的时间是11:53之前GMT,浏览器会缓存页面,因为它没有过期。因此,如果用户第一次访问该页面在11:00 GMT,然后在11:30 GMT去同一个页面再次,浏览器会看到它的缓存的版本仍然有效,不会(或者更确切地说,是允许不)创建一个新的HTTP请求。​​

If the time at which the browser makes this request is before 11:53 GMT, the browser will cache the page, because it has not yet expired. So if the user first visits the page at 11:00 GMT, and then goes to the same page again at 11:30 GMT, the browser will see that its cached version is still valid and will not (or rather, is allowed not to) make a new HTTP request.

如果用户进入该页面,第三次在格林威治标准时间12:00时,浏览器看到它的缓存版本现在已经过期(这是后11:53),所以它试图验证页面,向服务器发送一个请求用的If-Modified-Since头。自从它被首次担任该网页的日期还没有改变,没有体304(修改)响应将被退回。由于到期日期已过 - 页是过时 - 一个验证请求将要进行的页面被访问,直到验证失败以后每一次

If the user goes to the page a third time at 12:00 GMT, the browser sees that its cached version has now expired (it's after 11:53) so it attempts to validate the page, sending a request to the server with a If-Modified-Since header. A 304 (not modified) response with no body will be returned since the page's date has not been altered since it was first served. Since the expiry date has passed -- the page is 'stale' -- a validation request will be made every subsequent time the page is visited until validation fails.

现在,而不是在你上传11:57网页的新版本,让我们pretend。在这种情况下,浏览器的企图,以验证旧版本的页面在12:00失败,它接收在响应中,与新的页沿,这两个新的报头

Now, let's pretend instead that you uploaded a new version of the page at 11:57. In this case, the browser's attempt to validate the old version of the page at 12:00 fails and it receives in the response, along with the new page, these two new headers:

Last-Modified: Wed, 18 Feb 2009 11:57:00 GMT
Expires: Wed, 18 Feb 2009 13:57:00 GMT

(文件的最后修改时间成为在新版本上载11:57和Apache计算截止时间为11:57 + 2:00 = 13:57 GMT)。

(The last modification time of the file becomes 11:57 upon upload of the new version, and Apache calculates the expiration time as 11:57 + 2:00 = 13:57 GMT.)

验证(使用更近的日期)将不会被现在需要直到13:57。

Validation (using the more recent date) will not be required now until 13:57.

(当然,需要注意的是很多其他的事情都与两个头我上面列出一起发送,我只是修剪了所有为简单的其余部分)

(Note of course that many other things are sent along with the two headers I listed above, I just trimmed out all the rest for simplicity)

这篇关于缓存过期控制与最后修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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