加速WordPress技巧:Redis缓存输出的HTML页面
<p><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>Redis是一个高级的key-value存储系统,类似memcached,所有内容都存在内存中,因此每秒钟可以超过10万次GET操作。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>我下面提出的解决方案是在Redis中缓存所有输出的HTML 内容而无需再让WordPress重复执行页面脚本。这里使用Redis代替Varnish设置简单,而且可能更快。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>安装 Redis</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>apt-get install redis-server</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>或者阅读 安装指南</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>使用 Predis 作为 Redis 的 PHP 客户端</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>这里我们推荐 Predis. 上传 predis.php 到 WordPress 的根目录。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>前端缓存的PHP脚本</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>步骤1:在WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:</span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<?php<br>
// Change these two variables:<br>
$seconds_of_caching = 60*60*24*7; // 7 days.<br>
$ip_of_this_website = '204.62.14.112';<br>
/*<br>
- This file is written by Jim Westergren, copyright all rights reserved.<br>
- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/<br>
- The code is free for everyone to use how they want but please mention my name and link to my article when writing about this.<br>
- Change $ip_of_this_website to the IP of your website above.<br>
- Add ?refresh=yes to the end of a URL to refresh it's cache<br>
- You can also enter the redis client via the command prompt with the command "redis-cli" and then remove all cache with the command "flushdb".<br>
*/<br>
// Very necessary if you use Cloudfare:<br>
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {<br>
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];<br>
}<br>
// This is from WordPress:<br>
define('WP_USE_THEMES', true);<br>
// Start the timer:<br>
function getmicrotime($t) {<br>
list($usec, $sec) = explode(" ",$t);<br>
return ((float)$usec + (float)$sec);<br>
}<br>
$start = microtime();<br>
// Initiate redis and the PHP client for redis:<br>
include("predis.php");<br>
$redis = new Predis\Client('');<br>
// few variables:<br>
$current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];<br>
$current_page_url = str_replace('?refresh=yes', '', $current_page_url);<br>
$redis_key = md5($current_page_url);<br>
// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment<br>
if (isset($_GET['refresh']) || substr($_SERVER['REQUEST_URI'], -12) == '?refresh=yes' || ($_SERVER['HTTP_REFERER'] == $current_page_url && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REMOTE_ADDR'] != $ip_of_this_website)) {<br>
require('./wp-blog-header.php');<br>
$redis->del($redis_key);<br>
// Second case: cache exist in redis, let's display it<br>
} else if ($redis->exists($redis_key)) {<br>
$html_of_current_page = $redis->get($redis_key);<br>
echo $html_of_current_page;<br>
echo "<!-- This is cache -->";<br>
// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:<br>
} else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website && strstr($current_page_url, 'preview=true') == false) {<br>
require('./wp-blog-header.php');<br>
$html_of_current_page = file_get_contents($current_page_url);<br>
$redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);<br>
echo "<!-- Cache has been set -->";<br>
// last case: the normal WordPress. Should only be called with file_get_contents:<br>
} else {<br>
require('./wp-blog-header.php');<br>
}<br>
// Let's display some page generation time (note: CloudFlare may strip out comments):<br>
$end = microtime();<br>
$t2 = (getmicrotime($end) - getmicrotime($start));<br>
if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {<br>
echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";<br>
}<br>
?></p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>或者直接下载 index-with-redis.php</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>步骤2:将上述代码中的 IP 地址替换成你网站的 IP 地址</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>步骤3:在.htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>性能测试</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>1.没有Redis 的情况下,平均首页执行1.614 秒,文章页0.174 秒(无任何缓存插件)</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>2.使用Redis 的情况下,平均页面执行时间0.00256秒</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>其他建议</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>我的环境是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 nano VPS 中,无缓存插件。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>请确认使用了gzip压缩,可加快访问速度。</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>访问 wp-admin</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.</span></p>
頁:
[1]