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

2016-04-13cms教程
77

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 PredisClient('');
// 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/.

The End

相关推荐

网站图片丢失或者获取失败时显示默认图片的办法
当我们的网站时间久了难免会出现图片丢失或者误删,这样文章的图片就无法显示了,会让页面变的很难看。那么当网站图片丢失或者获取失败时如何显示我们设置的默认图片呢,或者默认多张图片随机显示呢。 方法一:当图片调用失败后,会用图片后面设置的默认图片...
2022-10-24 cms教程
71

修改帝国CMS默认提示框样式为layui
修改帝国CMS默认提示框样式为layui,修改文件:e/message/index.php link href="?=$public_r[newsurl]?hmb2019/css/layui.css" rel="stylesheet" type="text/css"div class="layui-container" div class="layui-card" style="width:400px;box-shadow: 1px 1px...
2022-10-20 cms教程
331

WordPress上一篇、下一篇文章链接添加title属性的方法
上一篇、下一篇文章代码: ?php previous_post_link(%link,) ??php next_post_link(%link,) ? 该代码解析出来的代码大概如下: a href= rel=external nofollow rel=external nofollow /aa href= rel=external nofollow rel=external nofollow /a 通过 get_pr...
2017-08-03 cms教程
358

WordPress去除链接添加 Noopener Noreferrer 的教程
WordPress目前在最近的版本中为所有需要新窗口打开的链接都自动添加了新的 noopener noreferrer 属性。noopener noreferrer 属性并不是新发布的标准,但 WordPress 4.7.4 版开始的编辑器默认都会添加该属性。新窗口打开超链接的属性 target=_blank 增加 rel=...
2017-07-31 cms教程
232

dicuz搭建的论坛如何修改改后台地址?
用discuz搭建的论坛,后台默认地址是 域名加 admin.php,很多站长怕网站被黑客攻击,一般黑客会扫描admin文件,然后破解管理员账户登录后台,所以想修改后台的地址。直接修改 admin.php 名并放到其他文件夹里,访问后台用修改过的路径虽然能访问,但是会出错...
2017-07-08 cms教程
364

phpcms筛选功能(经典无错)---汇总网上各种版本!
phpcms的筛选功能,网上有各种版本,但是多多少少都会有点问题,不是很完美,下面我总结网上各种版本,加上自己的研究,得出一个算是比较全的一个资料,既可以分页,也可以分栏目! 第一步:后台添加字段 添加字段: 后台--内容--内容相关设置--模型管理--文...
2017-06-26 cms教程
436