麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > CMS > Wordpress > 正文

WordPress給網站配置Redis 緩存的例子

2024-09-07 00:51:05
字體:
來源:轉載
供稿:網友

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

你也可以在 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/.

本文其實在國內已經有很翻譯過了,但我看到作者也一直在更新此文,反而國內譯者都不怎么更新,我就自己去重新折騰了一遍.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一本一本久久a久久精品综合小说 | 国产精品久久av | 羞羞的视频 | 国产亚洲精品久久久久久久久久 | 国产精品久久久久久久久久尿 | 午夜精品小视频 | 在线免费av网站 | 亚洲精品一区二区三区大胸 | lutube成人福利在线观看污 | 欧美毛片 | 在线成人一区二区 | 欧美综合在线观看视频 | 一级免费毛片 | 一级啪啪片 | 久久久久久久久久网 | 国产精品自拍av | 国产精品99久久久久久宅女 | 亚洲网站免费观看 | 91香蕉国产亚洲一区二区三区 | 国产一区免费观看 | 操碰97 | 日韩毛片毛片久久精品 | 免费在线观看国产精品 | 日本aaa一级片 | 国产成人羞羞视频在线 | 国产91精品久久久久久 | a一级黄色大片 | 黄色香蕉视频 | 黄色网欧美 | 久久吊| 久久久99精品视频 | 5xsq在线视频| 成人羞羞在线观看网站 | 在线播放免费播放av片 | 日本综合久久 | 一级做受大片免费视频 | 国产精品刺激对白麻豆99 | 亚洲一区二区三区高清 | 九九热精 | 另类亚洲孕妇分娩网址 | 羞羞网站 |