Redis 是一个高级的 key-value 存储系统,类似 memcached,所有内容都存在内存中,因此每秒钟可以超过 10 万次 GET 操作、访问。
我下面介绍的解决方案是在 Redis 中缓存所有输出的 HTML 内容而无需再让 WordPress 重复执行页面脚本。这里使用 Redis 代替 Varnish 设置简单,而且可能更快。
为wordpress提供闪存级加速体验方案,实现良好的用户体验
安装Redis
- yum install -y redis #前提是你的repo源中存在redis安装包
使用 Predis 作为 Redis 的 PHP 客户端
我们需要一个程序作为redis的客户端,可以理解为服务器的中间文件,将php与redis相互链接。这里推荐predis,一个非常不错的开源程序。
项目地址:https://packagist.org/packages/predis/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. -->";
- }
- ?>
步骤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)。
性能测试
- 没有 Redis 的情况下,平均首页执行 1.614 秒,文章页 0.174 秒(无任何缓存插件)
- 使用 Redis 的情况下,平均页面执行时间 0.00256 秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议
我的网站环境是Nginx -Tengine2.2 、 PHP-FPM 、PHP7、 Mysql5.6、Redis,无缓存插件。
再为网站开启Gzip加速就非常完美了
访问 wp-admin
有一点需要特别注意,要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.

2019年2月12日 09:11 1F
这玩意占用运行内存太大了
2019年2月12日 14:49 B1
@ 青山 个人站不会占用太多内存,如果需求大可以进行优化,以快照的形式存储到磁盘
2019年10月19日 16:14 2F
靠谱的教程,比那些互相抄袭的好多了