使用Last-Modified和HTTP-IF-MODIFIED-SINCE实现Cache

27 views
Skip to first unread message

netkiller

unread,
Sep 26, 2009, 9:35:00 AM9/26/09
to netkiller
使用Last-Modified和HTTP-IF-MODIFIED-SINCE实现Cache

日期 2009年03月05日 06:33:46 | Topic: 其它

Last-Modified和HTTP-IF-MODIFIED-SINCE是实现Cache的最常用的方法之一。通常在我们访问一个静态页面的时候,
服务器会返回给我们一个200 HTTP Response,里面包括了一个Last-Modified的信息。当我们再次访问相同的页面的时候,在我
们的HTTP Request里面有一个HTTP-IF-MODIFIED-SINCE的信息,它的值就是上次Response的Last-
Modified的值。并且当服务器端的文件没有被修改时,服务器将会返回一个304 HTTP Response。而当服务器端的文件被修改以后,服
务器将会重新返回一个200 HTTP Response,里面的Last-Modified也被更新。
以下是一个普通的HTTP请求的例子。
1. 首先我们通过浏览器访问一个静态页面aa.html;

GET /aa.html HTTP/1.1
Host: linux-test
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive


2. 服务器返回200状态,将aa.html返回,并且附带了Last-Modified信息;

HTTP/1.1 200 OK
Date: Thu, 05 Mar 2009 02:58:54 GMT
Server: Apache/2.2.8 (Unix) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8e PHP/
5.2.5 mod_apreq2-20051231/2.6.0 mod_perl/2.0.2 Perl/v5.10.0
Last-Modified: Thu, 05 Mar 2009 02:56:12 GMT
ETag: "8c296-7a-464564f956b00"
Accept-Ranges: bytes
Content-Length: 122
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
......
//html content……
......

3. 我们再次通过浏览器访问aa.html,注意此时aa.html没有被修改过;

GET /aa.html HTTP/1.1
Host: linux-test
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
If-Modified-Since: Thu, 05 Mar 2009 02:56:12 GMT
If-None-Match: "8c296-7a-464564f956b00"
Cache-Control: max-age=0


4. 服务器返回304状态,表示源文件没有被修改,此时浏览器将使用cache来显示;

HTTP/1.1 304 Not Modified
Date: Thu, 05 Mar 2009 03:01:37 GMT
Server: Apache/2.2.8 (Unix) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8e PHP/
5.2.5 mod_apreq2-20051231/2.6.0 mod_perl/2.0.2 Perl/v5.10.0
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
ETag: "8c296-7a-464564f956b00"


5. 我们修改了aa.html以后,再次访问aa.html;

GET /aa.html HTTP/1.1
Host: linux-test
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
If-Modified-Since: Thu, 05 Mar 2009 02:56:12 GMT
If-None-Match: "8c296-7a-464564f956b00"
Cache-Control: max-age=0


6. 服务器返回200状态,将新的aa.html返回,并且附带新的Last-Modified信息;

HTTP/1.1 200 OK
Date: Thu, 05 Mar 2009 03:30:06 GMT
Server: Apache/2.2.8 (Unix) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8e PHP/
5.2.5 mod_apreq2-20051231/2.6.0 mod_perl/2.0.2 Perl/v5.10.0
Last-Modified: Thu, 05 Mar 2009 03:30:06 GMT
ETag: W/"8c296-76-46456ca4f43c0"
Accept-Ranges: bytes
Content-Length: 118
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
......
//html content...
......

Reply all
Reply to author
Forward
0 new messages