当前位置:首页 > CMS教程 > WordPress > 列表

WordPress给网站配置Redis 缓存的例子

发布:smiling 来源: PHP粉丝网  添加日期:2014-06-21 16:15:14 浏览: 评论: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,代码如下:

  1. <?php 
  2. // change vars here 
  3. $cf = 1; // set to 1 if you are using cloudflare 
  4. $debug = 0; // set to 1 if you wish to see execution time and cache actions 
  5. $display_powered_by_redis = 1; // set to 1 if you want to display a powered by redis message with execution time, see below 
  6.  
  7. $start = microtime(); // start timing page exec 
  8.  
  9. // if cloudflare is enabled 
  10. if ($cf) { 
  11. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { 
  12. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 
  13. } 
  14. } 
  15.  
  16. // from wp 
  17. define('WP_USE_THEMES', true); 
  18.  
  19. // init predis 
  20. include("predis.php"); 
  21. $redis = new PredisClient(''); 
  22.  
  23. // init vars 
  24. $domain = $_SERVER['HTTP_HOST']; 
  25. $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
  26. $url = str_replace('?r=y', '', $url); 
  27. $url = str_replace('?c=y', '', $url); 
  28. $dkey = md5($domain); 
  29. $ukey = md5($url); 
  30.  
  31. // check if page isn't a comment submission 
  32. (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0; 
  33.  
  34. // check if logged in to wp 
  35. $cookie = var_export($_COOKIE, true); 
  36. $loggedin = preg_match("/wordpress_logged_in/", $cookie); 
  37.  
  38. // check if a cache of the page exists 
  39. if ($redis->hexists($dkey, $ukey) && !$loggedin && !$submit && !strpos($url, '/feed/')) { 
  40.  
  41. echo $redis->hget($dkey, $ukey); 
  42. $cached = 1; 
  43. $msg = 'this is a cache'; 
  44.  
  45. // if a comment was submitted or clear page cache request was made delete cache of page 
  46. } else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') { 
  47.  
  48. require('./wp-blog-header.php'); 
  49. $redis->hdel($dkey, $ukey); 
  50. $msg = 'cache of page deleted'; 
  51.  
  52. // delete entire cache, works only if logged in 
  53. } else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') { 
  54.  
  55. require('./wp-blog-header.php'); 
  56. if ($redis->exists($dkey)) { 
  57. $redis->del($dkey); 
  58. $msg = 'domain cache flushed'; 
  59. } else { 
  60. $msg ='no cache to flush'; 
  61. } 
  62.  
  63. // if logged in don't cache anything 
  64. } else if ($loggedin) { 
  65.  
  66. require('./wp-blog-header.php'); 
  67. $msg = 'not cached'; 
  68.  
  69. // cache the page 
  70. } else { 
  71.  
  72. // turn on output buffering 
  73. ob_start(); 
  74.  
  75. require('./wp-blog-header.php'); 
  76.  
  77. // get contents of output buffer 
  78. $html = ob_get_contents(); 
  79.  
  80. // clean output buffer 
  81. ob_end_clean(); 
  82. echo $html; 
  83.  
  84. // Store to cache only if the page exist and is not a search result. 
  85. if (!is_404() && !is_search()) { 
  86. // store html contents to redis cache 
  87. $redis->hset($dkey, $ukey, $html); 
  88. $msg = 'cache is set'; 
  89. } 
  90. } 
  91.  
  92. $end = microtime(); // get end execution time 
  93.  
  94. // show messages if debug is enabled 
  95. if ($debug) { 
  96. echo $msg.': '; 
  97. echo t_exec($start, $end); 
  98. } 
  99.  
  100. if ($cached && $display_powered_by_redis) { 
  101. // You should move this CSS to your CSS file and change the: float:right;margin:20px 0; 
  102. echo "<style>#redis_powered{float:right;margin:20px 0;background:url(http://images.staticjw.com/jim/3959/redis.png) 10px no-repeat #fff;border:1px solid #D7D8DF;padding:10px;width:190px;} 
  103. #redis_powered div{width:190px;text-align:right;font:10px/11px arial,sans-serif;color:#000;}</style>"; 
  104. echo "<a href="http://www.aips.me/wordpress-with-redis-as-a-frontend-cache/" style="text-decoration:none;"><div id="redis_powered"><div>Page generated in<br/> ".t_exec($start, $end)." sec</div></div></a>"; 
  105. } 
  106.  
  107. // time diff 
  108. function t_exec($start, $end) { 
  109. $t = (getmicrotime($end) - getmicrotime($start)); 
  110. return round($t,5); 
  111. } 
  112.  
  113. // get time 
  114. function getmicrotime($t) { 
  115. list($usec, $sec) = explode(" ",$t); 
  116. return ((float)$usec + (float)$sec); 
  117. }
  118. ?> 

你也可以在 Github 上查看 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 秒.我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好.

其他建议:本文作者的 WordPress 环境是 Nginx + PHP-FPM + APC + Cloudflare + Redis.安装在一个 VPS 中,无缓存插件.请确认使用了 gzip 压缩,可加快访问速度,

访问 wp-admin:要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.

本文其实在国内已经有很翻译过了,但我看到作者也一直在更新此文,反而国内译者都不怎么更新,我就自己去重新折腾了一遍.


Tags: WordPress缓存 Redis缓存

分享到: