找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1975|回复: 0

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

[复制链接]
发表于 2016-1-8 08:47:41 | 显示全部楼层 |阅读模式 来自 河南省郑州市

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 ,内容如下:
  1. <?php
  2. // Change these two variables:
  3. $seconds_of_caching = 60*60*24*7; // 7 days.
  4. $ip_of_this_website = '204.62.14.112';
  5. /*
  6. - This file is written by Jim Westergren, copyright all rights reserved.
  7. - See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
  8. - 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.
  9. - Change $ip_of_this_website to the IP of your website above.
  10. - Add ?refresh=yes to the end of a URL to refresh it's cache
  11. - 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".
  12. */
  13. // Very necessary if you use Cloudfare:
  14. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  15. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  16. }
  17. // This is from WordPress:
  18. define('WP_USE_THEMES', true);
  19. // Start the timer:
  20. function getmicrotime($t) {
  21. list($usec, $sec) = explode(" ",$t);
  22. return ((float)$usec + (float)$sec);
  23. }
  24. $start = microtime();
  25. // Initiate redis and the PHP client for redis:
  26. include("predis.php");
  27. $redis = new Predis\Client('');
  28. // few variables:
  29. $current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  30. $current_page_url = str_replace('?refresh=yes', '', $current_page_url);
  31. $redis_key = md5($current_page_url);
  32. // This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment
  33. 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)) {
  34. require('./wp-blog-header.php');
  35. $redis->del($redis_key);
  36. // Second case: cache exist in redis, let's display it
  37. } else if ($redis->exists($redis_key)) {
  38. $html_of_current_page = $redis->get($redis_key);
  39. echo $html_of_current_page;
  40. echo "<!-- This is cache -->";
  41. // third: a normal visitor without cache. And do not cache a preview page from the wp-admin:
  42. } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website && strstr($current_page_url, 'preview=true') == false) {
  43. require('./wp-blog-header.php');
  44. $html_of_current_page = file_get_contents($current_page_url);
  45. $redis->setex($redis_key, $seconds_of_caching, $html_of_current_page);
  46. echo "<!-- Cache has been set -->";
  47. // last case: the normal WordPress. Should only be called with file_get_contents:
  48. } else {
  49. require('./wp-blog-header.php');
  50. }
  51. // Let's display some page generation time (note: CloudFlare may strip out comments):
  52. $end = microtime();
  53. $t2 = (getmicrotime($end) - getmicrotime($start));
  54. if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) {
  55. echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->";
  56. }
  57. ?>
复制代码
步骤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/.

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-5-3 13:02 , Processed in 0.043878 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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