<?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>zhiqiang&#039;s personal blog</description>
	<lastBuildDate>Thu, 02 Sep 2010 00:59:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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[WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数据库结果到完全静态化，都可以实现。
define('ENABLE_CACHE', true);
把这条语句加入WordPress的配置文件wp-config.php里，WordPress便会以文本的方式缓存一些数据库查询的结果到/wp-content/cache/目录下。注意保持这个目录可写。
使用WP-Cache 2.0
...]]></description>
			<content:encoded><![CDATA[<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-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-2.html">cos-html-cache</a>，可以为blog的主页和文章页面建立真正的静态页面，也就是说访问者看到的就是静态页面，连php的调用都省了，所以加速效果无限好（达到你的服务器的极限）。不过缺点也是显然的，无法显示动态和个性化内容...</p>
<p>cos-html-cache的作者给过一张示意图，演示WP-Cache和cos-html-cache的原理，很直观的看出它们节约了哪一部分的时间。</p>
<p><img src="http://lh5.google.com/image/china.cos/Rlf3m8N2U8I/AAAAAAAAAjg/392blJPwSFM/s400/cache.jpg?imgmax=800" 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/posts/the-whole-point-of-static-wordpress.html">用javascript读取COOKIE信息</a>很容易实现。</p>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/it/speedup-your-blog.html">加速blog：序</a> <small>这是一个介绍如何提速blog的系列文章，特别是基于WordPress的blog。通过这些方法，你我完全能做到一个飞速的blog，至少要比与你同服务器上的其它blog要...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html">加速blog：服务器端的中转和缓存</a> <small>前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a> <small>本文隶属加速blog系列

既然我们要谈加速blog，第一重要的是给blog的速度一个量化的评价。

Firefox的fasterfox插件会在Firefox的右下角给出每个网页的载...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a> <small>在WordPress生成页面时，最消耗时间的便是数据库查询了。
监测WordPress的数据库查询
WordPress内置了数据库缓存系统，安装插件WordPress Cache Inspect，它会...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-choose-appropriate-server.html">加速blog：选择合适的服务器</a> <small>要想打造一个响应快速的blog，一个快速的服务器（虚拟主机空间）是必不可少的。其实这才是决定因素，因为这可能会导致速度在数量级上的差距（我...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html">加速blog：优化WordPress程序效率</a> <small>这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。
get_permalink函数
get_permalink函数非常好用，get_permalink($i...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a> <small>本文隶属加速blog系列 

为什么有的网站即点即开，有些却慢得要死？影响网站速度的因素有但不限于下面这些：许多因素会影响到网页初次访问的响...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a> <small>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a> <small>用FTP更新WordPress的可能有体会，如果是打包上传的话，速度很快，但如果解压后一个文件一个文件上传的话，可能就要好几分钟。  下载也是如此，不...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a> <small>作为文章的或者功能性的或者美学意义上的需求，现在blog越来越丰富多彩，图片也越来越多。  选择合适的图片尺寸  现在数码相机越来越好，动辄800...</small></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">3 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html/feed</wfw:commentRss>
		<slash:comments>3</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[这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。
get_permalink函数
get_permalink函数非常好用，get_permalink($id)便能直接得到文章ID为$id的永久链接，所以在显示文章列表中常常用到。比如类似下面这个显示最近10篇文章列表的函数，很多人都用到了吧：

&#60;?php
function get_recent_posts () {
  global $wpdb, $tableposts;
  $request = "SELECT * FROM $tableposts W...]]></description>
			<content:encoded><![CDATA[<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://www.php.net/function">function</a> get_recent_posts () {
  <a style="color: #0000ff" href="http://www.php.net/global">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://www.php.net/foreach">foreach</a> ($posts <a style="color: #0000ff" href="http://www.php.net/as">as</a> $post) {
    $post_title = <a style="color: #ffa500" href="http://www.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://www.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><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/it/speedup-your-blog.html">加速blog：序</a> <small>这是一个介绍如何提速blog的系列文章，特别是基于WordPress的blog。通过这些方法，你我完全能做到一个飞速的blog，至少要比与你同服务器上的其它blog要...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html">加速blog：服务器端的中转和缓存</a> <small>前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a> <small>本文隶属加速blog系列

既然我们要谈加速blog，第一重要的是给blog的速度一个量化的评价。

Firefox的fasterfox插件会在Firefox的右下角给出每个网页的载...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a> <small>在WordPress生成页面时，最消耗时间的便是数据库查询了。
监测WordPress的数据库查询
WordPress内置了数据库缓存系统，安装插件WordPress Cache Inspect，它会...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-choose-appropriate-server.html">加速blog：选择合适的服务器</a> <small>要想打造一个响应快速的blog，一个快速的服务器（虚拟主机空间）是必不可少的。其实这才是决定因素，因为这可能会导致速度在数量级上的差距（我...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html">加速blog：WordPress的缓存和静态化</a> <small>WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a> <small>本文隶属加速blog系列 

为什么有的网站即点即开，有些却慢得要死？影响网站速度的因素有但不限于下面这些：许多因素会影响到网页初次访问的响...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a> <small>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a> <small>用FTP更新WordPress的可能有体会，如果是打包上传的话，速度很快，但如果解压后一个文件一个文件上传的话，可能就要好几分钟。  下载也是如此，不...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a> <small>作为文章的或者功能性的或者美学意义上的需求，现在blog越来越丰富多彩，图片也越来越多。  选择合适的图片尺寸  现在数码相机越来越好，动辄800...</small></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[在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 accessed that were already in memory 
Cache Misses 
This is the number of items that had...]]></description>
			<content:encoded><![CDATA[<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://www.php.net/define">define</a>('<span style="color: #8b0000;">SAVEQUERIES</span>', <a style="color: #0000ff" href="http://www.php.net/true">true</a>);<span style="color: #0000ff;">，</span>再在footer.php文件的尾部加上一句<span style="color: #0000ff;">&lt;?</span>php <a style="color: #ffa500" href="http://www.php.net/var_dump">var_dump</a>($wpdb-&gt;queries); <span style="color: #0000ff;">?&gt;</span> 。就可以在页面源代码的尾部找到整个页面执行过程中所提交的所有MySQL查询语句了。数组里每一项都包含一个string和一个float，string存储查询语句，float存储查询时间。</p>
<p>参考：<a href="http://yskin.net/2006/11/wordpress-sql-statement.html">查看WordPress页面执行过程中提交的SQL查询语句</a>。</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><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a> <small>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将...</small></li><li><a href="http://zhiqiang.org/blog/it/change-pivotcache-source.html">动态修改Excel数据表的数据来源</a> <small>Excel有一个很有用的功能是直接导入外部数据库或者使用外部数据源建立数据透视表和数据透视图。但比较可惜的是，这个数据源的查询语句是静态的...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-http-headers.html">加速blog：HTTP Header</a> <small>本文隶属加速blog系列  HTTP请求和相应Header  一个经典的HTTP连接是，读者通过浏览器（下称为浏览器端），向服务器（下称为服务器端）申请浏览某网页...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html">加速blog：减少和优化插件</a> <small>尽量少用含javascript和css的插件
很多插件作者需要为blog的慢速度负责。插件作者随意地往'wp_head'里安插js和css，导致很多blog的head部分越来越臃肿。

...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a> <small>本文隶属加速blog系列

既然我们要谈加速blog，第一重要的是给blog的速度一个量化的评价。

Firefox的fasterfox插件会在Firefox的右下角给出每个网页的载...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html">加速blog：优化WordPress程序效率</a> <small>这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。
get_permalink函数
get_permalink函数非常好用，get_permalink($i...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-choose-appropriate-server.html">加速blog：选择合适的服务器</a> <small>要想打造一个响应快速的blog，一个快速的服务器（虚拟主机空间）是必不可少的。其实这才是决定因素，因为这可能会导致速度在数量级上的差距（我...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html">加速blog：WordPress的缓存和静态化</a> <small>WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a> <small>本文隶属加速blog系列 

为什么有的网站即点即开，有些却慢得要死？影响网站速度的因素有但不限于下面这些：许多因素会影响到网页初次访问的响...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/database-query-is-np-hard.html">数据库查询是NP-Hard问题</a> <small>问题来自美人他爹和Wangjianshuo's blog  一个查询的例子：NOT (AND ((C1&gt;5), OR ((C2&lt;6),(C3&lt;&gt;9))))  问题1：给出两个这样的查询Q1和Q2，如何确定Q1的结果是...</small></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[尽量少用含javascript和css的插件
很多插件作者需要为blog的慢速度负责。插件作者随意地往'wp_head'里安插js和css，导致很多blog的head部分越来越臃肿。
所以，当你安装一个往你的blog上安插文件的插件时，先考虑一下是否真的需要它。往往我们有别的代替者。举个例子，为了显示格式化的程序代码，就一定需要安装cool code插件么？在这个系列中，我也会展示一些代码，但我不用cool...]]></description>
			<content:encoded><![CDATA[<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://www.php.net/function">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://www.php.net/false">false</a>, '<span style="color: #8b0000;">2.20</span>');
wp_print_scripts(<a style="color: #ffa500" href="http://www.php.net/array">array</a>('<span style="color: #8b0000;">sack</span>', '<span style="color: #8b0000;">wp-polls</span>'));
<a style="color: #0000ff" href="http://www.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://www.php.net/false">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://www.php.net/function">function</a> display_poll($poll_id){
<a style="color: #0000ff" href="http://www.php.net/global">global</a> $poll_js_loaded;
<a style="color: #0000ff" href="http://www.php.net/if">if</a> (!$poll_js_loaded)
poll_header();
$poll_js_loaded = <a style="color: #0000ff" href="http://www.php.net/true">true</a>;
<a style="color: #0000ff" href="http://www.php.net/return">return</a> get_poll($poll_id, <a style="color: #0000ff" href="http://www.php.net/false">false</a>);
}
<span style="color: #0000ff;">?&gt;</span></pre>
</blockquote>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/it/speedup-your-blog.html">加速blog：序</a> <small>这是一个介绍如何提速blog的系列文章，特别是基于WordPress的blog。通过这些方法，你我完全能做到一个飞速的blog，至少要比与你同服务器上的其它blog要...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-on-server.html">加速blog：服务器端的中转和缓存</a> <small>前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a> <small>本文隶属加速blog系列

既然我们要谈加速blog，第一重要的是给blog的速度一个量化的评价。

Firefox的fasterfox插件会在Firefox的右下角给出每个网页的载...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html">加速blog：优化WordPress程序效率</a> <small>这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。
get_permalink函数
get_permalink函数非常好用，get_permalink($i...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-choose-appropriate-server.html">加速blog：选择合适的服务器</a> <small>要想打造一个响应快速的blog，一个快速的服务器（虚拟主机空间）是必不可少的。其实这才是决定因素，因为这可能会导致速度在数量级上的差距（我...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html">加速blog：WordPress的缓存和静态化</a> <small>WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a> <small>本文隶属加速blog系列 

为什么有的网站即点即开，有些却慢得要死？影响网站速度的因素有但不限于下面这些：许多因素会影响到网页初次访问的响...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a> <small>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a> <small>用FTP更新WordPress的可能有体会，如果是打包上传的话，速度很快，但如果解压后一个文件一个文件上传的话，可能就要好几分钟。  下载也是如此，不...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-about-images.html">加速blog：处理页面图片</a> <small>作为文章的或者功能性的或者美学意义上的需求，现在blog越来越丰富多彩，图片也越来越多。  选择合适的图片尺寸  现在数码相机越来越好，动辄800...</small></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">7 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/speedup-blog-reduce-plugins.html/feed</wfw:commentRss>
		<slash:comments>7</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[前面已经提到了浏览器端的缓存，通过适当的Header可以建议和命令浏览器缓存页面内容比如javascript, css, 图片等。这里的服务器端的缓存又是什么意思呢？
现在很多网站都提供了API，提供比如JSON这样的数据方式以便在blog上引用。比如说picasaweb，我们可以用它来做一个picasa web album的站内镜像，但它的一个缺点就是慢，每次从Google服务器下载会json都会让我的blog停顿2到3秒钟，这...]]></description>
			<content:encoded><![CDATA[<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://www.php.net/stripslashes">stripslashes</a>($_REQUEST['<span style="color: #8b0000">uri</span>']);
  $rss_uri = <a style="color: #ffa500" href="http://www.php.net/str_replace">str_replace</a>(<a style="color: #ffa500" href="http://www.php.net/array">array</a>('<span style="color: #8b0000">&quot;</span>', '<span style="color: #8b0000"> </span>'), <a style="color: #ffa500" href="http://www.php.net/array">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://www.php.net/switch">switch</a> ($file_type){
    <a style="color: #0000ff" href="http://www.php.net/case">case</a> '<span style="color: #8b0000">css</span>':
      <a style="color: #ffa500" href="http://www.php.net/header">header</a>(&quot;<span style="color: #8b0000">Content-type: text/css</span>&quot;);
      <a style="color: #0000ff" href="http://www.php.net/break">break</a>;
    <a style="color: #0000ff" href="http://www.php.net/case">case</a> '<span style="color: #8b0000">js</span>' :
      <a style="color: #ffa500" href="http://www.php.net/header">header</a>(&quot;<span style="color: #8b0000">Content-type: application/x-javascript</span>&quot;);
      <a style="color: #0000ff" href="http://www.php.net/break">break</a>;
    <a style="color: #0000ff" href="http://www.php.net/case">case</a> '<span style="color: #8b0000">gif</span>':
      <a style="color: #ffa500" href="http://www.php.net/header">header</a>(&quot;<span style="color: #8b0000">Content-type: image/gif</span>&quot;);
      <a style="color: #0000ff" href="http://www.php.net/break">break</a>;
    <a style="color: #0000ff" href="http://www.php.net/case">case</a> '<span style="color: #8b0000">jpg</span>':
      <a style="color: #ffa500" href="http://www.php.net/header">header</a>(&quot;<span style="color: #8b0000">Content-type: image/jpeg</span>&quot;);
      <a style="color: #0000ff" href="http://www.php.net/break">break</a>;
    <a style="color: #0000ff" href="http://www.php.net/case">case</a> '<span style="color: #8b0000">png</span>':
      <a style="color: #ffa500" href="http://www.php.net/header">header</a>(&quot;<span style="color: #8b0000">Content-type: image/png</span>&quot;);
      <a style="color: #0000ff" href="http://www.php.net/break">break</a>;
    <a style="color: #0000ff" href="http://www.php.net/default">default</a>:
      <a style="color: #ffa500" href="http://www.php.net/header">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://www.php.net/if">if</a> ( !<a style="color: #ffa500" href="http://www.php.net/is_file">is_file</a>($cache_rss_path) || <a style="color: #ffa500" href="http://www.php.net/time">time</a>()-<a style="color: #ffa500" href="http://www.php.net/filemtime">filemtime</a>($cache_rss_path) &gt; $refresh) {
    <a style="color: #0000ff" href="http://www.php.net/if">if</a> (!<a style="color: #ffa500" href="http://www.php.net/class_exists">class_exists</a>('<span style="color: #8b0000">Snoopy</span>')) <a style="color: #0000ff" href="http://www.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://www.php.net/fopen">fopen</a>($cache_rss_path, '<span style="color: #8b0000">w</span>');
    <a style="color: #ffa500" href="http://www.php.net/fputs">fputs</a>($cache_file, $snoopy-&gt;results);
    <a style="color: #ffa500" href="http://www.php.net/fclose">fclose</a>($cache_file);

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

  } <a style="color: #0000ff" href="http://www.php.net/else">else</a> {
    $content = file_get_contents ($cache_rss_path);
    <a style="color: #0000ff" href="http://www.php.net/echo">echo</a> $content;
  }
<span style="color: #0000ff">?&gt;</span></pre>
</blockquote>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/it/speedup-blog-set-browser-cache.html">加速blog：设置浏览器缓存</a> <small>在用户浏览blog的不同页面时，很多内容是重复的，比如相同的javascript，css，背景图片等。如果我们能够建议甚至强制浏览器在本地缓存这些文件，将...</small></li><li><a href="http://zhiqiang.org/blog/it/using-htaccess-to-speed-blog.html">设置blog的缓存和压缩</a> <small>在网站根目录下的.htaccess文件中添加以下代码  &lt;IfModule mod_expires.c&gt;
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/pn...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-page.html">加速blog：分析页面内容</a> <small>用FTP更新WordPress的可能有体会，如果是打包上传的话，速度很快，但如果解压后一个文件一个文件上传的话，可能就要好几分钟。  下载也是如此，不...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-http-headers.html">加速blog：HTTP Header</a> <small>本文隶属加速blog系列  HTTP请求和相应Header  一个经典的HTTP连接是，读者通过浏览器（下称为浏览器端），向服务器（下称为服务器端）申请浏览某网页...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress-database.html">加速blog：监测和优化WordPress数据库</a> <small>在WordPress生成页面时，最消耗时间的便是数据库查询了。
监测WordPress的数据库查询
WordPress内置了数据库缓存系统，安装插件WordPress Cache Inspect，它会...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-analysis-your-website.html">加速blog：速度检测</a> <small>本文隶属加速blog系列

既然我们要谈加速blog，第一重要的是给blog的速度一个量化的评价。

Firefox的fasterfox插件会在Firefox的右下角给出每个网页的载...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-optimize-wordpress.html">加速blog：优化WordPress程序效率</a> <small>这里本质上要谈的还是优化数据库的效率，不过是改写WordPress的代码使得降低数据库查询次数。
get_permalink函数
get_permalink函数非常好用，get_permalink($i...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-choose-appropriate-server.html">加速blog：选择合适的服务器</a> <small>要想打造一个响应快速的blog，一个快速的服务器（虚拟主机空间）是必不可少的。其实这才是决定因素，因为这可能会导致速度在数量级上的差距（我...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-cache-wordpress.html">加速blog：WordPress的缓存和静态化</a> <small>WordPress消耗时间最多的便是数据库的查询，所以缓存是一个比较好的解决方案。WordPress强大的'hook'机制，使得可以为之建立强大的缓存机制，从缓存数...</small></li><li><a href="http://zhiqiang.org/blog/it/speedup-blog-factors-of-slowness.html">加速blog：网站响应缓慢的因素</a> <small>本文隶属加速blog系列 

为什么有的网站即点即开，有些却慢得要死？影响网站速度的因素有但不限于下面这些：许多因素会影响到网页初次访问的响...</small></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>
