加速WordPress技巧:Redis缓存输出的HTML页面

所属分类: CMS教程 / WordPress 阅读数: 1228
收藏 0 赞 0 分享
Redis是一个高级的key-value存储系统,类似memcached,所有内容都存在内存中,因此每秒钟可以超过10万次GET操作。
我下面提出的解决方案是在Redis中缓存所有输出的HTML 内容而无需再让WordPress重复执行页面脚本。这里使用Redis代替Varnish设置简单,而且可能更快。
安装 Redis
如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:
apt-get install redis-server
或者阅读 安装指南
使用 Predis 作为 Redis 的 PHP 客户端
你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。
这里我们推荐 Predis. 上传 predis.php 到 WordPress 的根目录。
前端缓存的PHP脚本
步骤1:在WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:

复制代码
代码如下:

<?php
// Change these two variables:
$seconds_of_caching = 60*60*24*7; // 7 days.
$ip_of_this_website = '204.62.14.112';
/*
- This file is written by Jim Westergren, copyright all rights reserved.
- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
- 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.
- Change $ip_of_this_website to the IP of your website above.
- Add ?refresh=yes to the end of a URL to refresh it's cache
- 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".
*/
// Very necessary if you use Cloudfare:
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
// This is from WordPress:
define('WP_USE_THEMES', true);
// Start the timer:
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
// Initiate redis and the PHP client for redis:
include("predis.php");
$redis = new Predis\Client('');
// few variables:
$current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$current_page_url = str_replace('?refresh=yes', '', $current_page_url);
$redis_key = md5($current_page_url);
// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
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)) {
require('./wp-blog-header.php');
$redis->del($redis_key);
// Second case: cache exist in redis, let's display it
} else if ($redis->exists($redis_key)) {
$html_of_current_page = $redis->get($redis_key);
echo $html_of_current_page;
echo "<!-- This is cache -->";
// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
} else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website && strstr($current_page_url, 'preview=true') == false) {
require('./wp-blog-header.php');
$html_of_current_page = file_get_contents($current_page_url);
$redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);
echo "<!-- Cache has been set -->";
// last case: the normal WordPress. Should only be called with file_get_contents:
} else {
require('./wp-blog-header.php');
}
// Let's display some page generation time (note: CloudFlare may strip out comments):
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {
echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";
}
?>

或者直接下载 index-with-redis.php
步骤2:将上述代码中的 IP 地址替换成你网站的 IP 地址
步骤3:在.htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。
性能测试
1.没有Redis 的情况下,平均首页执行1.614 秒,文章页0.174 秒(无任何缓存插件)
2.使用Redis 的情况下,平均页面执行时间0.00256秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议
我的环境是Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 nano VPS 中,无缓存插件。
请确认使用了gzip压缩,可加快访问速度。
访问 wp-admin
要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.
更多精彩内容其他人还在看

WordPress中让Widget 标题支持简单的HTML标签

在默认情况下,WordPress 的 Widget 标题是不支持任何 HTML 标签的,下面的技巧教你使用简单的代码替换实现在 Widget 标题实现支持 HTML 标签
收藏 0 赞 0 分享

不使用wordpress插件添加页面关键词和描述信息

WordPress存在一个问题,就是页面中keywords和description信息的缺失,其实这个工作应该由主题来完成,遗憾的是大部分主题并没有涉及,因此造成这两个信息的缺失,对于SEO很不利。下面说下如何用非插件的方式在模板中实现keywords和description的调
收藏 0 赞 0 分享

wordpress数据库优化和清理冗余数据的方法

本文主要介绍了wordpress数据库优化和清理冗余数据的方法,大家参考使用吧
收藏 0 赞 0 分享

wordpress博客多站点获取当前博客信息示例

在开通WordPress多站点之后,你或许需要在插件中获取当前的博客信息。本文帮你解决这个问题
收藏 0 赞 0 分享

wordpress恶意代码解决方法分享

发现自己的测试站的主题带上了恶意代码,非常明显的就是出现了一个函数_verifyactivate_widgets,通常情况下,一旦出现了这个函数在你的主题中,使用PHP可以任意的获取用户名、博客的主题文件等等,下面是解决方法
收藏 0 赞 0 分享

wordpress中短代码失效解决办法

在WordPress中我们偶尔会使用短代码,但是在一些特殊的主题中,我们偶尔会发现,短代码失效了,没有显示为我们想要的音乐播放器,却只显示为原本的字符串。这个时候,你可能需要对主题进行一些简单的处理来实现这个打印效果
收藏 0 赞 0 分享

wordpress文章标题为空时其它内容代替的方法

本文主要介绍了wordpress文章标题为空时使用其它内容代替的方法,大家参考使用吧
收藏 0 赞 0 分享

为wordpress绑定多个域名的方法分享

本文主要介绍了WordPress绑定多个域名的方法,wordpress默认情况下会自动跳转到后台规定的home_url上去,如何实现绑定多个域名的功能,详细看下文吧
收藏 0 赞 0 分享

wordpress在postname中支持大写字母的方法

本文主要介绍了wordpress在postname中支持大写字母的方法,wordpress默认会将标题中的英文大写字母lower到小写,要在URL中使用中文,那么使用大写也是常有的,这里提供一种取消wordpress自动降级字母大写的方法
收藏 0 赞 0 分享

wordpress实现用户历史阅读记录功能分享

历史记录是一种较为私密的功能,主要为当前用户提供服务,不同的用户使用不同的终端看到的结果是不同的,不同用户之间不能共享,以保持用户对自己浏览记录的独享性。一般而言,我们有以下几种实现思路
收藏 0 赞 0 分享
查看更多