第一次请求的时候发送的数据包如下
<div id="_mcePaste">(Request-Line) GET /cache/index.php HTTP/1.1</div>
<div id="_mcePaste">Host localhost</div>
<div id="_mcePaste">User-Agent Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12</div>
<div id="_mcePaste">Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</div>
<div id="_mcePaste">Accept-Language zh-cn,en-us;q=0.7,en;q=0.3</div>
<div id="_mcePaste">Accept-Encoding gzip,deflate</div>
<div id="_mcePaste">Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7</div>
<div id="_mcePaste">Keep-Alive 115</div>
<div id="_mcePaste">Connection keep-alive</div>
<div id="_mcePaste">Cookie cataedit_0=1; cataedit_6=1; cataedit_10=1; cataedit_12=1; cataedit_23=1; cataedit_30=1</div>
(Request-Line) GET /cache/index.php HTTP/1.1Host localhostUser-Agent Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language zh-cn,en-us;q=0.7,en;q=0.3Accept-Encoding gzip,deflateAccept-Charset GB2312,utf-8;q=0.7,*;q=0.7Keep-Alive 115Connection keep-aliveCookie cataedit_0=1; cataedit_6=1; cataedit_10=1; cataedit_12=1; cataedit_23=1; cataedit_30=1
返回的数据包
(Status-Line)<span style="white-space: pre;"> </span>HTTP/1.1 200 OK
Date<span style="white-space: pre;"> </span>Thu, 02 Dec 2010 01:59:07 GMT
Server<span style="white-space: pre;"> </span>Apache/2.2.14 (Ubuntu)
X-Powered-By<span style="white-space: pre;"> </span>PHP/5.3.2-1ubuntu4
Etag<span style="white-space: pre;"> </span>cached
Vary<span style="white-space: pre;"> </span>Accept-Encoding
Content-Encoding<span style="white-space: pre;"> </span>gzip
Content-Length<span style="white-space: pre;"> </span>39
Keep-Alive<span style="white-space: pre;"> </span>timeout=15, max=99
Connection<span style="white-space: pre;"> </span>Keep-Alive
Content-Type<span style="white-space: pre;"> </span>text/html
缓存以后,浏览器发送的请求如下
(Request-Line)<span style="white-space: pre;"> </span>GET /cache/index.php HTTP/1.1
Host<span style="white-space: pre;"> </span>localhost
User-Agent<span style="white-space: pre;"> </span>Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12
Accept<span style="white-space: pre;"> </span>text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language<span style="white-space: pre;"> </span>zh-cn,en-us;q=0.7,en;q=0.3
Accept-Encoding<span style="white-space: pre;"> </span>gzip,deflate
Accept-Charset<span style="white-space: pre;"> </span>GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive<span style="white-space: pre;"> </span>115
Connection<span style="white-space: pre;"> </span>keep-alive
Cookie<span style="white-space: pre;"> </span>cataedit_0=1; cataedit_6=1; cataedit_10=1; cataedit_12=1; cataedit_23=1; cataedit_30=1
If-None-Match<span style="white-space: pre;"> </span>cached
Server返回的请求如下
<div id="_mcePaste">(Status-Line) HTTP/1.1 304 Not Modified</div>
<div id="_mcePaste">Date Thu, 02 Dec 2010 01:59:18 GMT</div>
<div id="_mcePaste">Server Apache/2.2.14 (Ubuntu)</div>
<div id="_mcePaste">Connection Keep-Alive</div>
<div id="_mcePaste">Keep-Alive timeout=15, max=98</div>
<div id="_mcePaste">Etag cached</div>
<div id="_mcePaste">Vary Accept-Encoding</div>
(Status-Line) HTTP/1.1 304 Not ModifiedDate Thu, 02 Dec 2010 01:59:18 GMTServer Apache/2.2.14 (Ubuntu)Connection Keep-AliveKeep-Alive timeout=15, max=98Etag cachedVary Accept-Encoding
主要区别在于请求的数据在已经缓存之后,也就是If-None-Match这个被设置之后,在发送的数据中多了一行
If-None-Match<span style="white-space: pre;"> </span>cached
而返回的数据也由200状态变成了304
PHP设置及检查此标识位的代码如下:
function cache() {
$etag = "cached";
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('Etag:' . $etag, true, 304);
exit;
}
else
header('Etag:' . $etag);
}
另外与缓存相关的还有Last-Modified和expires,前者与etag相似,所不同的是标识不同,前者是http的编辑时间,后者是一个标识,相对来说后者较为灵活。
最后一个就是expires标识,这个默认在apache中不会开启,需要手动启用,
另外在含有SSI的服务器中的Last-Modified有不同的生成方法
本文参考了《构建高性能WEB站点》一书