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

所属分类: CMS教程 / WordPress 阅读数: 1209
收藏 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 防止恶意评论的方法

本文章详细的介绍了关于WordPress 防止恶意评论代码处理办法
收藏 0 赞 0 分享

wordpress制作自定义菜单的方法

本文介绍了wordpress中自定义菜单制作详细教程
收藏 0 赞 0 分享

WordPress增加文章排序方式

很多网站的文章列表页面都可以查看排序方法,但是在wp系统中是没有这个功能,下面我们来看看关于WordPress系统中增加选择文章的排序方式的解决办法
收藏 0 赞 0 分享

wordpress get_posts函数的使用方法 禁止输出指定类别的文章

本文向大家介绍wordpress使用get_posts函数功能禁止输出指定类别文章的方法,大家可以参考一下
收藏 0 赞 0 分享

在wordpress文章末尾添加内容的简单方法

如何在wordpress的文章末尾添加内容,在wordpress每篇文章的末尾添加文字、链接等内容,比如你想加个“原创文章如转载,请注明本文链接:”,其实很简单,有多种方法,下面就介绍给大家
收藏 0 赞 0 分享

wordpress dynamic_sidebar()函数使用方法

本文简单介绍wordpress dynamic_sidebar()函数使用的方法,在wordpress开发中会经常用到
收藏 0 赞 0 分享

WordPress导航菜单函数wp_nav_menu()详解

本文主讲内容是WordPress导航菜单函数wp_nav_menu()详细使用的说明,大家可以看参考一下,在开发WordPress模板时会使用到这个函数自定义菜单
收藏 0 赞 0 分享

WordPress自定义多个边栏的方法

在wordpress后台中,在外观>小工具里面,可以自定义边栏要显示的内容,一般的主题都支持至少一个自定义边栏,那么它是如何实现的呢
收藏 0 赞 0 分享

wordpress全局变量$wpdb初始化并声明为全局变量的方法

wordpress操作数据库用一个全局变量$wpdb来进行各种操作,使用的时候先在函数声明global $wpdb,然后调用它的数据库操作方法。那么它是在哪里进行初始化并声明为全局变量的呢
收藏 0 赞 0 分享

wordpress中强大的调用文章函数query posts 用法

query posts是一个非常好用的调用文章函数,可以做到同页面内显示多种特定范围的文章,下面为大家详细的介绍下wordpress中强大的query posts 用法,喜欢的朋友可以参考下
收藏 0 赞 0 分享
查看更多