<?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; 网站提速</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e7%bd%91%e7%ab%99%e6%8f%90%e9%80%9f/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：WordPress的缓存和静态化</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:40:53 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=732</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数据库结果到完全静态化，都可以实现。 define('ENABLE_CACHE', true); 把这条语句加入WordPress的配置文件wp-config.php里，WordPress便会以文本的方式缓存一些数据库查询的结...]]></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>WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数据库结果到完全静态化，都可以实现。</p>
<h3>define('ENABLE_CACHE', true);</h3>
<p>把这条语句加入WordPress的配置文件wp-config.php里，WordPress便会以文本的方式缓存一些数据库查询的结果到/wp-content/cache/目录下。注意保持这个目录可写。</p>
<h3>使用<a href="http://wordpress.org/extend/plugins/wp-super-cache/">WP-Cache 2.0</a></h3>
<p>这是WordPress比较官方的缓存插件。</p>
<h3>完全静态化</h3>
<p>使用<a href="http://www.storyday.com/html/y2007/1213_cos-html-cache-wordpress-static-release-of-plug-in.html">cos-html-cache</a>，可以为blog的主页和文章页面建立真正的静态页面，也就是说访问者看到的就是静态页面，连php的调用都省了，所以加速效果无限好（达到你的服务器的极限）。不过缺点也是显然的，无法显示动态和个性化内容...</p>
<p>cos-html-cache的作者给过一张示意图，演示WP-Cache和cos-html-cache的原理，很直观的看出它们节约了哪一部分的时间。</p>
<p><img src="http://lh5.ggpht.com/china.cos/Rlf3m8N2U8I/AAAAAAAAAjg/392blJPwSFM/s400/cache.jpg" alt="" /></p>
<h3><a href="http://wordpress.org/extend/plugins/wp-super-cache/">Super Cache</a></h3>
<p>这个是最近炒得比较火的一个插件，号称是WP-Cache 2.0的改进版。但我看了一下它的说明，似乎没有可取之处。比如它为了把留言者的个人信息显示在留言栏，就不直接给曾经留言过的用户静态版本了。但其实<a href="http://zhiqiang.org/blog/it/the-whole-point-of-static-wordpress.html">用javascript读取COOKIE信息</a>很容易实现。</p>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/speedup-your-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-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.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></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-cache-wordpress.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html#comments">5 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>加速blog：优化WordPress程序效率</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:40:19 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=731</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。 get_permalink函数 get_permalink函数非常好用，get_permalink($id)便能直接得到文章ID为$id的永久链接，所以在显示文章列表中常常用到。比如类似下面这个显示最近10篇文章列表的函数，很多人都用到了吧： &#60;?php function get_r...]]></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>这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。</p>
<h3>get_permalink函数</h3>
<p>get_permalink函数非常好用，get_permalink($id)便能直接得到文章ID为$id的永久链接，所以在显示文章列表中常常用到。比如类似下面这个显示最近10篇文章列表的函数，很多人都用到了吧：</p>
<blockquote>
<pre><span style="color: #0000ff;">&lt;?</span>php
<a style="color: #0000ff" href="http://cn.php.net/manual/en/language.functions.php">function</a> get_recent_posts () {
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/language.variables.scope.php">global</a> $wpdb, $tableposts;
  $request = "<span style="color: #8b0000;">SELECT * FROM $tableposts WHERE post_status = 'publish' </span>";
  $request .= "<span style="color: #8b0000;">ORDER BY post_modified DESC LIMIT 0, 10</span>";
  $posts = $wpdb-&gt;get_results($request);
  $output = '';
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.foreach.php">foreach</a> ($posts <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=as&amp;lang=en&amp;scope=404quickref">as</a> $post) {
    $post_title = <a style="color: #ffa500" href="http://cn.php.net/stripslashes">stripslashes</a>($post-&gt;post_title);
    $permalink  = get_permalink($post-&gt;ID);
    $output .= '<span style="color: #8b0000;">&lt;li&gt;&lt;a href="</span>' . $permalink . '<span style="color: #8b0000;">"&gt;</span>' . $post_title . '<span style="color: #8b0000;">&lt;/a&gt;&lt;/li&gt;</span>';
  }
  <a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> $output;
}
<span style="color: #0000ff;">?&gt;</span></pre>
</blockquote>
<p>但不知道有多少人知道调用上面这个函数将产生11次的数据库查询？你只看到了一次？另外10次就在get_permalink的调用里面，不信你去读一读get_permalink函数的实现。阅微堂以前的数据库查询次数高达60多，就是这玩意儿弄出来的，我随便改了一下，就降了30次（我有3个列表 <img src='http://zhiqiang.org/blog/wp-includes/images/smilies/smile.gif' alt=':)' class='wp-smiley' /> ）。</p>
<p>解决办法就是修改get_permalink的实现，毕竟很多人的permalink结构都很简单。也没有必要真的去修改get_permalink的源代码，这样升级起来比较麻烦。另外写一个类似的函数，每次需要时使用这个新的函数即可。</p>
<p><a href="http://www.21andy.com/blog/20070216/591.html">WordPress效率问题剖析及重写WordPress</a>这里还有蛮多，其它的以后遇到了再过来加。</p>
<div><h4>相关文章</h4><ul><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-choose-appropriate-server.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-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a></li><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/speedup-blog-reduce-plugins.html">加速blog：减少和优化插件</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.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-optimize-wordpress.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html#comments">7 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>加速blog：监测和优化WordPress数据库</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:39:01 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=730</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 在WordPress生成页面时，最消耗时间的便是数据库查询了。 监测WordPress的数据库查询 WordPress内置了数据库缓存系统，安装插件WordPress Cache Inspect，它会告诉你这个系统的效率（命中效率），主要为下面四个指标： Cold Cache Hits This is the number of cached items that were loaded from disk. Warm Cache Hits This is the number of cached items acc...]]></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>在WordPress生成页面时，最消耗时间的便是数据库查询了。</p>
<h3>监测WordPress的数据库查询</h3>
<p>WordPress内置了数据库缓存系统，安装插件<a href="http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/">WordPress Cache Inspect</a>，它会告诉你这个系统的效率（命中效率），主要为下面四个指标：</p>
<dt><strong>Cold Cache Hits</strong> </dt>
<dd>This is the number of cached items that were loaded from disk. </dd>
<dt><strong>Warm Cache Hits</strong> </dt>
<dd>This is the number of cached items accessed that were already in memory </dd>
<dt><strong>Cache Misses</strong> </dt>
<dd>This is the number of items that had to be fetched from the db as they were not in the cache. </dd>
<dt><strong>Loaded data</strong> </dt>
<dd>This lists the type and amount of data loaded into memory from that currently stored within the cache..</p>
<h3>define('ENABLE_CACHE', true);</h3>
<p>把这条语句加入WordPress的配置文件wp-config.php里，WordPress便会以文本的方式缓存一些数据库查询的结果到/wp-content/cache/目录下。注意保持这个目录可写。</p>
<p>试试看效果再用。因为这玩意儿是用磁盘Cache保持数据库查询结果。但某些服务器上，磁盘IO比较慢，比如在dreamhost上就不推荐用这种方式。</p>
<h3>显示所有数据库查询语句和消耗时间</h3>
<p>在wp-config.php文件里添加<a style="color: #ffa500" href="http://cn.php.net/define">define</a>('<span style="color: #8b0000;">SAVEQUERIES</span>', <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=true&amp;lang=en&amp;scope=404quickref">true</a>);<span style="color: #0000ff;">，</span>再在footer.php文件的尾部加上一句<span style="color: #0000ff;">&lt;?</span>php <a style="color: #ffa500" href="http://cn.php.net/var_dump">var_dump</a>($wpdb-&gt;queries); <span style="color: #0000ff;">?&gt;</span> 。就可以在页面源代码的尾部找到整个页面执行过程中所提交的所有MySQL查询语句了。数组里每一项都包含一个string和一个float，string存储查询语句，float存储查询时间。</p>
<p>参考：查看WordPress页面执行过程中提交的SQL查询语句。</p>
<h3>比较耗时的数据库语句</h3>
<p><a href="http://www.21andy.com/blog/20060813/374.html">WordPress执行效率问题</a>作者作了一个测试，大家可以看一下具体有什么问题，与自己的做一个对照。我提一下我遇到过的比较慢的查询语句。</p>
<h4>分类和tag相关的都比较消耗时间</h4>
<p>比较消耗时间的基本上都与分类表相关的三个数据表相关。特别是我用的那个相关文章查询，基本上占总查询时间的20%（优化后）。我比较期待快速或者带Cache的相关文章的插件。</p>
<h4>DESC wp_comments</h4>
<p>我不知道这个语句干啥的，经常很慢，所以我把产生它的插件"subscribe comments"插件直接禁用了，反正那个插件提供的功能就没几个人用。</p>
</dd>
<p>持续更新ing...</p>
<div><h4>相关文章</h4><ul><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/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a></li><li><a href="http://zhiqiang.org/blog/it/script-to-add-odbc-source.html">用脚本添加ODBC数据源</a></li><li><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</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-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><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/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-optimize-wordpress-database.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html#comments">2 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>加速blog：减少和优化插件</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html#comments</comments>
		<pubDate>Tue, 25 Dec 2007 07:18:18 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=729</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 尽量少用含javascript和css的插件 很多插件作者需要为blog的慢速度负责。插件作者随意地往'wp_head'里安插js和css，导致很多blog的head部分越来越臃肿。 所以，当你安装一个往你的blog上安插文件的插件时，先考虑一下是否真的需要它。往往我们有别的代替者。举个例子，为了显示格式化的程序代码，就一定需要安装...]]></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>  <h3>尽量少用含javascript和css的插件</h3>
<p>很多插件作者需要为blog的慢速度负责。插件作者随意地往'wp_head'里安插js和css，导致很多blog的head部分越来越臃肿。</p>
<p>所以，当你安装一个往你的blog上安插文件的插件时，先考虑一下是否真的需要它。往往我们有别的代替者。举个例子，为了显示格式化的程序代码，就一定需要安装cool code插件么？在这个系列中，我也会展示一些代码，但我不用cool code，一样有很好的效果。</p>
<h3>确实想用含带javascipt和css的插件怎么办？</h3>
<p>为了实现某些功能，插件必须要引入js和css文件。比如wp-polls，一个实现在blog上ajax投票和查看结果的插件。它肯定要用到javascript文件，这我们就无能为力了么？事实上不然，投票只会产生在特定的页面，也只有在这些页面，这些javascipt才是需要的。而wp-polls确实在每个页面上都引用了一遍，先看一看它是怎么在你的blog上插入javascript文件的：</p>
<blockquote>
<pre><span style="color: #0000ff;">&lt;?</span>php
add_action('<span style="color: #8b0000;">wp_head</span>', '<span style="color: #8b0000;">poll_header</span>');
<a style="color: #0000ff" href="http://cn.php.net/manual/en/language.functions.php">function</a> poll_header() {
wp_register_script('<span style="color: #8b0000;">wp-polls</span>', '<span style="color: #8b0000;">/wp-content/plugins/polls/polls-js.php</span>', <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=false&amp;lang=en&amp;scope=404quickref">false</a>, '<span style="color: #8b0000;">2.20</span>');
wp_print_scripts(<a style="color: #ffa500" href="http://cn.php.net/manual/en/language.types.array.php">array</a>('<span style="color: #8b0000;">sack</span>', '<span style="color: #8b0000;">wp-polls</span>'));
<a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> '<span style="color: #8b0000;">&lt;link rel="stylesheet" href="</span>'.get_option('<span style="color: #8b0000;">siteurl</span>').'<span style="color: #8b0000;">/wp-content/plugins/polls/polls-css.css" type="text/css" media="screen" /&gt;</span>'."<span style="color: #8b0000;">\n</span>";
<span style="color: #008000;">// more here, omited </span>
  }
<span style="color: #0000ff;">?&gt;</span></pre>
</blockquote>
<p>这段代码向你的blog的每个页面的header区域插入了三个文件，两个js文件/wp-includes/js/tw-sack.js，/wp-content/plugins/polls/polls-js.php和一个css文件/wp-content/plugins/polls/polls-css.css。为了让插件正常工作，这3个文件也不能少。但上面已经提到了，只有在页面上有投票项目的时候，这些才是需要的。所以改成下面，只有当页面有投票时，才会包含那3个文件。</p>
<blockquote>
<pre><span style="color: #0000ff;">&lt;?</span>php
<span style="color: #008000;">// delete add_action('wp_head', 'poll_header'); add the two lines below:</span>
  $poll_js_loaded = <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=false&amp;lang=en&amp;scope=404quickref">false</a>; <span style="color: #008000;">// global variable to track if the script file have been loaded</span>

<span style="color: #008000;">// change definition of function display_poll as below:</span>
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/language.functions.php">function</a> display_poll($poll_id){
<a style="color: #0000ff" href="http://cn.php.net/manual/en/language.variables.scope.php">global</a> $poll_js_loaded;
<a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.if.php">if</a> (!$poll_js_loaded)
poll_header();
$poll_js_loaded = <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=true&amp;lang=en&amp;scope=404quickref">true</a>;
<a style="color: #0000ff" href="http://cn.php.net/return">return</a> get_poll($poll_id, <a style="color: #0000ff" href="http://cn.php.net/manual-lookup.php?pattern=false&amp;lang=en&amp;scope=404quickref">false</a>);
}
<span style="color: #0000ff;">?&gt;</span></pre>
</blockquote>
<div><h4>相关文章</h4><ul><li><a href="http://zhiqiang.org/blog/it/speedup-your-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-choose-appropriate-server.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-set-browser-cache.html">加速blog：设置浏览器缓存</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a></li><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/speedup-blog-optimize-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-reduce-plugins.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html#comments">9 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>加速blog：服务器端的中转和缓存</title>
		<link>http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html</link>
		<comments>http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html#comments</comments>
		<pubDate>Sun, 16 Dec 2007 09:12:08 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[网站提速]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=721</guid>
		<description><![CDATA[博客 » IT技术 » 网站提速 » 系列：网站提速 查看该系列所有文章 前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思呢？ 现在很多网站都提供了API，提供比如JSON这样的数据方式以便在blog上引用。比如说picasaweb，我们可以用它来做一个picasa web album的站内镜像，但它的一个缺点就...]]></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>前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思呢？</p>
<p>现在很多网站都提供了API，提供比如JSON这样的数据方式以便在blog上引用。比如说picasaweb，我们可以用它来做一个picasa web album的站内镜像，但它的一个缺点就是慢，每次从Google服务器下载会json都会让我的blog停顿2到3秒钟，这让我很不爽。</p>
<p>但利用我的三线服务器，每次我需要数据的时候，我不是直接引用Google提供的js文件，而是通过我自己的服务器来中转这个数据，由于我自己的服务器无论同我自己还是同Google的服务器之间都有很好的速度，从而使得比我直接引用Google的文件还要快。</p>
<p>这便是服务器的中转。更具体的例子是Google Picasa Web提供给我显示相册列表的JSON地址为<a title="http://picasaweb.google.com/data/feed/base/user/$username?category=album&amp;alt=json&amp;access=public" href="http://picasaweb.google.com/data/feed/base/user/$username?category=album&amp;alt=json&amp;access=public">http://picasaweb.google.com/data/feed/base/user/$username?category=album&amp;alt=json</a>，我只需要像下面这样引用，便能拿到我的相册数据，</p>
<blockquote><pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">script</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">&quot;text/javascript&quot;</span> <span style="color: #ff0000">src</span>=<span style="color: #0000ff">&quot;http://picasaweb.google.com/data/feed/base/user/mathzqy?category=album&amp;alt=json&amp;callback=showalbum&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">script</span><span style="color: #0000ff">&gt;</span></pre>
</blockquote>
<p>然后我可以再写一个showalbum的js函数来控制相册的显示方式。但在实际中我并没有用这种方式，而是通过一个后台的PHP处理文件来中转这个文件，最后我是这样获取相册数据的：</p>
<blockquote>
<pre><span style="color: #0000ff">&lt;</span><span style="color: #800000">script</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">&quot;text/javascript&quot;</span><span style="color: #0000ff">&gt;</span>
	document.write('<span style="color: #0000ff">&lt;</span><span style="color: #800000">scrip' + 't</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">&quot;text/javascript&quot;</span> <span style="color: #ff0000">src</span>=<span style="color: #0000ff">&quot;feed.php?type=js&amp;uri=' + encodeURIComponent('http://picasaweb.google.com/data/feed/base/user/mathzqy?category=album&amp;alt=json&amp;callback=showalbum') + '&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">s' + 'cript</span><span style="color: #0000ff">&gt;</span>');
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">script</span><span style="color: #0000ff">&gt;</span></pre>
</blockquote>
<p>也就是说我向<a href="http://zhiqiang.org/feed.php">http://zhiqiang.org/feed.php</a>索要所需要的js文件，feed.php在服务器上会从Google的服务器上下载所需要的文件，然后转发给我。由于服务器同Google的服务器之间的连接更快更稳定，所以这样比我直接向Google索要此文件还要快。</p>
<p>通过服务器中转的更大的好处是，我可以在服务器上缓存数据。这样不同的用户所要相同的文件时，服务器端可以直接从缓存里读取文件发送给用户，这样速度就更快了。缓存有两种形式，存到数据库或者缓存到文件里。著名的Twitter Tools便将数据缓存在数据库里，每1小时更新一次。当如果自己写的话，缓存到文件更方便。下面的feed.php为一个简单的服务器端中转和缓存处理代码(注意修改$cache_path和$snoopy_file路径)：</p>
<blockquote>
<pre><span style="color: #0000ff">&lt;?</span>php
  <span style="color: #008000">// feed.php, to cache and transfer data</span>
  <span style="color: #008000">// example: feed.php?uri=http://zhiqiang.org/blog/feed&amp;type=xml</span>

  <span style="color: #008000">// the request uri  </span>
  $rss_uri = <a style="color: #ffa500" href="http://cn.php.net/stripslashes">stripslashes</a>($_REQUEST['<span style="color: #8b0000">uri</span>']);
  $rss_uri = <a style="color: #ffa500" href="http://cn.php.net/str_replace">str_replace</a>(<a style="color: #ffa500" href="http://cn.php.net/manual/en/language.types.array.php">array</a>('<span style="color: #8b0000">&quot;</span>', '<span style="color: #8b0000"> </span>'), <a style="color: #ffa500" href="http://cn.php.net/manual/en/language.types.array.php">array</a>('<span style="color: #8b0000">%22</span>','<span style="color: #8b0000">%20</span>'), $rss_uri);
  <span style="color: #008000">// the request file type</span>
  $file_type  = isset($_REQUEST['<span style="color: #8b0000">type</span>']) ? $_REQUEST['<span style="color: #8b0000">type</span>'] : '<span style="color: #8b0000">xml</span>';
  <span style="color: #008000">// the refresh time, control how much time the cache kept, </span>
  <span style="color: #008000">// default cache 1 hour.</span>
  $refresh    = isset($_REQUEST['<span style="color: #8b0000">refresh</span>']) ? $_REQUEST['<span style="color: #8b0000">refresh</span>'] : 3600000;
  <span style="color: #008000">// the cache file directory, make sure the directory is writeable</span>
  $cache_path = '<span style="color: #8b0000">cache/</span>';
  <span style="color: #008000">// this script need class snoopy.</span>
  $snoopy_file = '<span style="color: #8b0000">/wp-includes/class-snoopy.php</span>';

  <span style="color: #008000">// send HTTP header</span>
  <a style="color: #0000ff" href="http://cn.php.net/manual/en/control-structures.switch.php">switch</a> ($file_type){
    <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>(&quot;<span style="color: #8b0000">Content-type: text/css</span>&quot;);
      <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>(&quot;<span style="color: #8b0000">Content-type: application/x-javascript</span>&quot;);
      <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>(&quot;<span style="color: #8b0000">Content-type: image/gif</span>&quot;);
      <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>(&quot;<span style="color: #8b0000">Content-type: image/jpeg</span>&quot;);
      <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>(&quot;<span style="color: #8b0000">Content-type: image/png</span>&quot;);
      <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>(&quot;<span style="color: #8b0000">Content-type: text/plain</span>&quot;);
  }

  $rss_hash = md5($rss_uri);
  $cache_rss_path = $cache_path.$rss_hash.'<span style="color: #8b0000">.</span>'.$file_type;
  <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/is_file">is_file</a>($cache_rss_path) || <a style="color: #ffa500" href="http://cn.php.net/time">time</a>()-<a style="color: #ffa500" href="http://cn.php.net/filemtime">filemtime</a>($cache_rss_path) &gt; $refresh) {
    <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/class_exists">class_exists</a>('<span style="color: #8b0000">Snoopy</span>')) <a style="color: #0000ff" href="http://cn.php.net/require_once">require_once</a> ($snoopy_file);

    $snoopy = new Snoopy;
    $snoopy-&gt;fetch($rss_uri);
    <span style="color: #008000">// this will copy the created tex-image to your cache-folder</span>
    $cache_file = <a style="color: #ffa500" href="http://cn.php.net/manual/en/function.fopen.php">fopen</a>($cache_rss_path, '<span style="color: #8b0000">w</span>');
    <a style="color: #ffa500" href="http://cn.php.net/fputs">fputs</a>($cache_file, $snoopy-&gt;results);
    <a style="color: #ffa500" href="http://cn.php.net/fclose">fclose</a>($cache_file);

    <a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> $snoopy-&gt;results;

  } <a style="color: #0000ff" href="http://cn.php.net/else">else</a> {
    $content = file_get_contents ($cache_rss_path);
    <a style="color: #0000ff" href="http://cn.php.net/echo">echo</a> $content;
  }
<span style="color: #0000ff">?&gt;</span></pre>
</blockquote>
<div><h4>相关文章</h4><ul><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/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-choose-appropriate-server.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-about-images.html">加速blog：处理页面图片</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html">加速blog：减少和优化插件</a></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-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-cache-on-server.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html#comments">3 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

