<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; Cache</title>
	<atom:link href="http://zhiqiang.org/blog/tag/cache/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>理工科背景的证券从业人员</description>
	<lastBuildDate>Sun, 05 Feb 2012 03:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>加速blog：设置浏览器缓存</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html#comments</comments>
		<pubDate>Sat, 01 Dec 2007 02:31:14 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=708</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将大大降低页面产生的流量，从而降低页面载入时间。 根据服务器端的响应header（怎么看HTTP Header），一个文件对浏览器而言，有几级不同的缓存状态。 服务器端告...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/%e7%bd%91%e7%ab%99%e6%8f%90%e9%80%9f'>网站提速</a>  » </p><div class="series"><span>系列：<b>网站提速</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/%e7%bd%91%e7%ab%99%e6%8f%90%e9%80%9f">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将大大降低页面产生的流量，从而降低页面载入时间。</p>
<p>根据服务器端的响应header（<a href="http://zhiqiang.org/blog/it/speedup-blog-http-headers.html">怎么看HTTP Header</a>），一个文件对浏览器而言，有几级不同的缓存状态。</p>
<ol>
<li>服务器端告诉浏览器不要缓存此文件，每次都到服务器上更新文件。 </li>
<li>服务器端没有给浏览器任何指示（此时我也不知道这时候浏览器将会怎么做），这种情况多为动态脚本（比如PHP）产生的文件。 </li>
<li>在上次传输中，服务器给浏览器发送了Last-Modified或Etag数据，再次浏览时浏览器将提交这些数据到服务器，验证本地版本是否最新的，如果为最新的则服务器返回304代码，告诉浏览器直接使用本地版本，否则下载新版本。一般来说，有且只有静态文件，服务器端才会给出这些数据。 </li>
<li>服务器强制要求浏览器缓存文件，并设置了过期时间。在缓存未到期之前，浏览器将直接使用本地缓存文件，不会与服务器端产生任何通信。 </li>
</ol>
<p>我们要做的是尽量强制浏览器到第四种状态，特别是对于javascript, css和图片等变动较少的文件。</p>
<p>对于PHP产生的动态内容，只需要在内容输出之前输出强制缓存的header即可，比如下面的代码即要求浏览器缓存文件1个月：</p>
<blockquote><pre><span style="color: #0000ff">&lt;?</span>php
  <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>(&quot;<span style="color: #8b0000">Cache-Control: public</span>&quot;);
  <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>(&quot;<span style="color: #8b0000">Pragma: cache</span>&quot;);

  $offset = 1000*60*60*24; <span style="color: #008000">// cache 1 year</span>
  $ExpStr = &quot;<span style="color: #8b0000">Expires: </span>&quot;.<a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>(&quot;<span style="color: #8b0000">D, d M Y H:i:s</span>&quot;, <a style="color: #ffa500" href="http://cn.php.net/time">time</a>() + $offset).&quot;<span style="color: #8b0000"> GMT</span>&quot;;
  <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>($ExpStr);
<span style="color: #0000ff">?&gt;</span></pre>
</blockquote>
<p>对于静态文件，一般的服务器都支持第3级缓存状态。要想达到第四级的缓存效果，要么像之前GZIP压缩那样，用PHP外包一层，然后用PHP处理。要么需要服务器端的支持，APACHE的一个模块mod_expires支持给文件添加expires header。把下面的代码加入你的blog目录下的.htaccess文件，如果你的服务器安装了mod_expires模块，则将自动生效，图片等强制缓存一个月，html文档缓存10分钟。如果该模块没有安装，也不会出错。</p>
<blockquote>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">IfModule</span> <span style="color: #ff0000">mod_expires</span>.<span style="color: #ff0000">c</span><span style="color: #0000ff">&gt;</span>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType text/css A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/html A600
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IfModule</span><span style="color: #0000ff">&gt;</span></pre>
</blockquote>
<p>在<a href="http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html" target="_blank">这里</a>有mod_expires更详细的文档和教程。不过我要说明的是，mod_expires在绝大多数服务器上都没安装 <img src='http://zhiqiang.org/blog/wp-includes/images/smilies/sad.gif' alt=':(' class='wp-smiley' /> ，因为虽然这个模块包含在Apache的发行版里，但并不是默认安装模块。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html">加速blog：服务器端的中转和缓存</a></li><li><a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">Dreamhost上的GZIP &#038; Cache</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-website-with-css-sprites.html">用CSS Sprites让网站再加速</a></li><li><a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">设置blog的缓存和压缩</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-http-headers.html">加速blog：HTTP Header</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html">加速blog：优化WordPress程序效率</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html">加速blog：WordPress的缓存和静态化</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html#comments">7 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Dreamhost上的GZIP &amp; Cache</title>
		<link>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html</link>
		<comments>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html#comments</comments>
		<pubDate>Sun, 01 Apr 2007 11:09:54 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[Dreamhost]]></category>
		<category><![CDATA[GZIP]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/553.html</guid>
		<description><![CDATA[博客 » IT技术 » Dreamhost » 用WordPress的都知道，WordPress可以打开GZIP传输，压缩比率能达到60%到80%。下表是本blog首页在Web Page Analyzer上的测试报告，此页面上包括css, js所有东西加起来才14K。 QTY SIZE# TYPE URL 1 6999 HTML http://zhiqiang.org/blog/ 1 2593 SCRIPT http://zhiqiang.org/blog/.../comment.js.php 1 2302 SCRIPT http://zhiqiang.org/.../all.js.php 1 2048 CSS http://zhiqiang.org/.../style.css.php 1 431 IMG http://feeds.feedburner.com/...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href='http://zhiqiang.org/blog/tag/dreamhost'>Dreamhost</a>  » </p><p>用WordPress的都知道，WordPress可以打开GZIP传输，压缩比率能达到60%到80%。下表是本<a href="http://zhiqiang.org/blog/">blog首页</a>在<a href="http://www.websiteoptimization.com/services/analyze/index.html" target="_blank">Web Page Analyzer</a>上的测试报告，此页面上包括css, js<strong>所有东西</strong>加起来才<strong>14K。</strong></p>
<p align="center">
<table class="t" border="1">
<tbody>
<tr>
<th>QTY</th>
<th>SIZE#</th>
<th>TYPE</th>
<th>URL</th>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>6999 </td>
<td nowrap>HTML </td>
<td nowrap><a href="http://zhiqiang.org/blog/">http://zhiqiang.org/blog/</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2593 </td>
<td nowrap>SCRIPT </td>
<td nowrap><a href="http://zhiqiang.org/blog/wp-content/plugins/ajaxcomment/comment.js.php">http://zhiqiang.org/blog/.../comment.js.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2302 </td>
<td nowrap>SCRIPT </td>
<td nowrap><a href="http://zhiqiang.org/blog/jscript/all.js.php">http://zhiqiang.org/.../all.js.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>2048 </td>
<td nowrap>CSS </td>
<td nowrap><a href="http://zhiqiang.org/blog/wp-content/themes/phoenixblue2/style.css.php">http://zhiqiang.org/.../style.css.php</a> </td>
</tr>
<tr>
<td nowrap>1 </td>
<td nowrap>431 </td>
<td nowrap>IMG </td>
<td nowrap><a href="http://feeds.feedburner.com/%7Efc/zhiqiang?bg=99CCFF&#038;ampfg=444444&#038;ampanim=0">http://feeds.feedburner.com/~fc/zhiqiang</a> </td>
</tr>
<tr>
<td nowrap>5 ^ </td>
<td nowrap>14373* </td>
<td> </td>
<td>Total (^unique objects)</td>
</tr>
</tbody>
</table>
<p>Dreamhost还用的是Apache 1.3(Am I wrong？)，所以没有mod_deflate模块，而用的是mod_gzip模块，这个模块直接在磁盘上读写，效率比前者要低。不过有总比没有好，毕竟压缩比率很高。</p>
<p>但诡异的是，服务器默认对CSS和JS不作压缩。写信去问了dreamhost客服，也没有什么好方法。最后我采用的方法是用php给包一层：用style.css.php来代替style.css：</p>
<blockquote><pre><span style="color: #0000ff">&lt;?</span>php <span style="color: #008000"># this is the file style.css.php, who contains style.css</span>
<span style="color: #008000"># set the request file name</span>
$<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>="<span style="color: #8b0000">style.css</span>";

<span style="color: #008000"># Set Expires, cache the file on the browse</span>
<span style="color: #008000"># Delete it if you don't want it</span>
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Expires:</span>".<a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>("<span style="color: #8b0000">D, d M Y H:i:s</span>", <a style="color: #ffa500" href="http://cn.php.net/time">time</a>()+15360000)."<span style="color: #8b0000">GMT</span>");
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Cache-Control: max-age=315360000</span>");

<span style="color: #008000"># set the last modified time</span>
$mtime = <a style="color: #ffa500" href="http://cn.php.net/filemtime">filemtime</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>);
$gmt_mtime = <a style="color: #ffa500" href="http://cn.php.net/gmdate">gmdate</a>('<span style="color: #8b0000">D, d M Y H:i:s</span>', $mtime) . '<span style="color: #8b0000"> GMT</span>';
<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Last-Modified:</span>" . $gmt_mtime);

<span style="color: #008000"># output a mediatype header</span>
$ext = <a style="color: #ffa500" href="http://cn.php.net/array_pop">array_pop</a>(<a style="color: #ffa500" href="http://cn.php.net/explode">explode</a>('<span style="color: #8b0000">.</span>', $<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>));
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">switch</a> ($ext){
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">css</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/css</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">js</span>' :
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/javascript</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">gif</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/gif</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">jpg</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/jpeg</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">case</a> '<span style="color: #8b0000">png</span>':
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: image/png</span>");
 <a style="color: #0000ff" href="http://cn.php.net/break">break</a>;
<a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=default&amp;lang=en&amp;scope=404quickref">default</a>:
 <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.header.php">header</a>("<span style="color: #8b0000">Content-type: text/plain</span>");
}

<span style="color: #008000"># GZIP the content</span>
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>();<a style="color: #ffa500" href="http://cn.php.net/ob_start">ob_start</a>('<span style="color: #8b0000">ob_gzhandler</span>');}

<span style="color: #008000"># echo the file's contents</span>
<a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> <a style="color: #ffa500" href="http://cn.php.net/implode">implode</a>('<span style="color: #8b0000"></span>', <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>($<a style="color: #ffa500" href="http://cn.php.net/manual/en/function.file.php">file</a>));

<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a>(<a style="color: #ffa500" href="http://cn.php.net/extension_loaded">extension_loaded</a>('<span style="color: #8b0000">zlib</span>')){
<a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
<span style="color: #008000"># set header the content's length;</span>
<span style="color: #008000"># header("Content-Length: ".ob_get_length()); # (It doesn't work? )</span>
<a style="color: #ffa500" href="http://cn.php.net/ob_end_flush">ob_end_flush</a>();
}
<span style="color: #0000ff">?&gt;</span></pre>
</blockquote>
<p>上面代码不仅压缩代码，而且要求浏览器端Cache这个文件。甚至，可以把所有css文件都放到一个php文件里，只要在echo处加入多个文件即可。</p>
<p>下面是一些常用的测试网站：</p>
<ul>
<li><a href="http://www.gidnetwork.com/tools/gzip-test.php" target="_blank">GZIP Test</a>: 测试一个URL地址是否支持GZIP传输，以及压缩比率。
</li>
<li><a href="http://www.websiteoptimization.com/services/analyze/index.html" target="_blank">Web Page Analyzer</a>：网站页面速度报告
</li>
<li><a href="http://www.ircache.net/cgi-bin/cacheability.py" target="_blank">Cacheability Test</a>: 测试一个页面是否可Cache</li>
</ul>
<p>参考资料：</p>
<ul>
<li><a href="http://www.mnot.net/cache_docs/" target="_blank">Caching Tutorial</a>
</li>
<li><a href="http://blog.htmlor.com/2006/08/03/serving_javascript_fast_chinese/">flickr对javascript干的好事</a>
</li>
<li><a href="http://www.badpenguin.org/php-howto-control-page-caching" target="_blank">Working with cached pages</a></li>
</ul>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/innerhtml-2-cd-issue-dynamic-modification-table-function-of-js.html">innerHTML只读问题 &amp; 两个动态修改Table的js函数</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-website-with-css-sprites.html">用CSS Sprites让网站再加速</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-gzip-javascript-css.html">加速blog：GZIP压缩传输你的文件</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-javascript-css.html">加速blog：关于javascript(css)</a></li><li><a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">设置blog的缓存和压缩</a></li><li><a href="http://zhiqiang.org/blog/it/to-get-gibberish-wordpress-move-to-dreamhost.html">搞定乱码，WordPress搬家到dreamhost</a></li><li><a href="http://zhiqiang.org/blog/it/dreamhost-system-load-average-load-recording-and-analysis.html">Dreamhost系统Load average负载记录和分析</a></li><li><a href="http://zhiqiang.org/blog/it/move-to-the-blog-server-bluehost.html">blog服务器搬家到bluehost</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2007. | <a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html#comments">18 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/dreamhost-on-gzip-cache.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

