设为首页 收藏本站
查看: 612|回复: 0

[经验分享] 让 WordPress 使用 Redis 缓存来进行加速

[复制链接]

尚未签到

发表于 2016-12-20 08:45:35 | 显示全部楼层 |阅读模式
  原文 http://www.oschina.net/question/12_60107
  Redis 是一个高级的 key-value 存储系统,类似 memcached,所有内容都存在内存中,因此每秒钟可以超过 10 万次 GET 操作。
我下面提出的解决方案是在 Redis 中缓存所有输出的 HTML 内容而无需再让 WordPress 重复执行页面脚本。这里使用 Redis 代替 Varnish 设置简单,而且可能更快。
安装 Redis
如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:
1apt-get install redis-server



 
或者阅读 安装指南
使用 Predis 作为 Redis 的 PHP 客户端
你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。
这里我们推荐 Predis. 上传 predis.php 到 WordPress 的根目录。
前端缓存的 PHP 脚本
步骤1: 在 WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:
001<?php

002 

003// Change these two variables:

004 

005$seconds_of_caching = 60*60*24*7; // 7 days.

006 

007$ip_of_this_website = '204.62.14.112';

008 

009  

010 

011/*

012 

013- This file is written by Jim Westergren, copyright all rights reserved.

014 

015- See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/

016 

017- 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.

018 

019- Change $ip_of_this_website to the IP of your website above.

020 

021- Add ?refresh=yes to the end of a URL to refresh it's cache

022 

023- 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".

024 

025*/

026 

027  

028 

029// Very necessary if you use Cloudfare:

030 

031if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {

032 

033$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];

034 

035}

036 

037  

038 

039// This is from WordPress:

040 

041define('WP_USE_THEMES', true);

042 

043  

044 

045// Start the timer:

046 

047function getmicrotime($t) {

048 

049list($usec, $sec) = explode(" ",$t);

050 

051return ((float)$usec + (float)$sec);

052 

053}

054 

055$start = microtime();

056 

057  

058 

059// Initiate redis and the PHP client for redis:

060 

061include("predis.php");

062 

063$redis = new Predis\Client('');

064 

065  

066 

067// few variables:

068 

069$current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

070 

071$current_page_url = str_replace('?refresh=yes', '', $current_page_url);

072 

073$redis_key = md5($current_page_url);

074 

075  

076 

077// This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment

078 

079if (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)) {

080 

081require('./wp-blog-header.php');

082 

083$redis->del($redis_key);

084 

085  

086 

087// Second case: cache exist in redis, let's display it

088 

089} else if ($redis->exists($redis_key)) {

090 

091$html_of_current_page = $redis->get($redis_key);

092 

093echo $html_of_current_page;

094 

095echo "<!-- This is cache -->";

096 

097  

098 

099// third: a normal visitor without cache. And do not cache a preview page from the wp-admin:

100 

101} else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website &&strstr($current_page_url, 'preview=true') == false) {

102 

103require('./wp-blog-header.php');

104 

105$html_of_current_page = file_get_contents($current_page_url);

106 

107$redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);

108 

109echo "<!-- Cache has been set -->";

110 

111  

112 

113// last case: the normal WordPress. Should only be called with file_get_contents:

114 

115} else {

116 

117require('./wp-blog-header.php');

118 

119}

120 

121  

122 

123  

124 

125// Let's display some page generation time (note: CloudFlare may strip out comments):

126 

127$end = microtime();

128 

129$t2 = (getmicrotime($end) - getmicrotime($start));

130 

131if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {

132 

133echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";

134 

135}

136 

137?>



 
或者直接下载 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)。
性能测试

  • 没有 Redis 的情况下,平均首页执行 1.614 秒,文章页 0.174 秒(无任何缓存插件)
  • 使用 Redis 的情况下,平均页面执行时间 0.00256 秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议
我的环境是 Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 nano VPS 中,无缓存插件。
请确认使用了 gzip 压缩,可加快访问速度。
访问 wp-admin
要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.
英文原文,OSCHINA原创翻译

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-316708-1-1.html 上篇帖子: redis server的shutdown命令和debug segfault等管理命令有点危险 下篇帖子: Redis-- 超高性能的key-value数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表