Advertisement
Guest User

index-with-redis.php

a guest
Aug 6th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Author: Jim Westergren, Jeedo Aquino & Flynsarmy
  5. File: index-with-redis.php
  6. Updated: 2012-10-25
  7.  
  8. This is a redis caching system for wordpress.
  9. see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
  10.  
  11. Originally written by Jim Westergren but improved by Jeedo Aquino.
  12.  
  13. some caching mechanics are different from jim's script which is summarized below:
  14.  
  15. - cached pages do not expire not unless explicitly deleted or reset
  16. - appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
  17. - appending a ?r=y to a url deletes the cache of that url
  18. - submitting a comment deletes the cache of that page
  19. - refreshing (f5) a page deletes the cache of that page
  20. - includes a debug mode, stats are displayed at the bottom most part after </html>
  21.  
  22. for setup and configuration see more here:
  23.  
  24. www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
  25.  
  26. use this script at your own risk. i currently use this albeit a slightly modified version
  27. to display a redis badge whenever a cache is displayed.
  28.  
  29. */
  30.  
  31. // change vars here
  32. $debug = 0; // set to 1 if you wish to see execution time and cache actions
  33. $display_powered_by_redis = 1; // set to 1 if you want to display a powered by redis message with execution time, see below
  34.  
  35. $start = microtime(); // start timing page exec
  36.  
  37. // if cloudflare is enabled
  38. if ( isset($_SERVER['HTTP_CF_CONNECTING_IP']) )
  39. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  40.  
  41. // from wp
  42. define('WP_USE_THEMES', true);
  43.  
  44. // init predis
  45. require 'predis/src/Autoloader.php';
  46. Predis\Autoloader::register();
  47. $redis = new Predis\Client();
  48.  
  49. // init vars
  50. $cached = 0;
  51. $domain = $_SERVER['HTTP_HOST'];
  52. $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  53. $url = str_replace('?r=y', '', $url);
  54. $url = str_replace('?c=y', '', $url);
  55. $dkey = md5($domain);
  56. $ukey = md5($url);
  57.  
  58. // check if page isn't a comment submission
  59. $submit = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0';
  60.  
  61. // check if logged in to wp
  62. $cookie = var_export($_COOKIE, true);
  63. $loggedin = preg_match("/wordpress_logged_in/", $cookie);
  64.  
  65. // check if a cache of the page exists
  66. if (!$loggedin && !$submit && !strpos($url, '/feed/') && !strpos($url, '/page/') && !strpos($url, '/category/') && $redis->hexists($dkey, $ukey)) {
  67.  
  68. echo $redis->hget($dkey, $ukey);
  69. $cached = 1;
  70. $msg = 'this is a cache';
  71.  
  72. // if a comment was submitted or clear page cache request was made delete cache of page
  73. } else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') {
  74.  
  75. require('./wp-blog-header.php');
  76. $redis->hdel($dkey, $ukey);
  77. $msg = 'cache of page deleted';
  78.  
  79. // delete entire cache, works only if logged in
  80. } else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') {
  81.  
  82. require('./wp-blog-header.php');
  83. if ($redis->exists($dkey)) {
  84. $redis->del($dkey);
  85. $msg = 'domain cache flushed';
  86. } else {
  87. $msg = 'no cache to flush';
  88. }
  89.  
  90. // if logged in don't cache anything
  91. } else if ($loggedin) {
  92.  
  93. require('./wp-blog-header.php');
  94. $msg = 'not cached';
  95.  
  96. // cache the page
  97. } else {
  98.  
  99. // turn on output buffering
  100. ob_start();
  101.  
  102. require('./wp-blog-header.php');
  103.  
  104. // get contents of output buffer
  105. $html = ob_get_contents();
  106.  
  107. // clean output buffer
  108. ob_end_clean();
  109. echo $html;
  110.  
  111. // Store to cache only if the page exist and is not a search result.
  112. if (!is_404() && !is_search()) {
  113. // store html contents to redis cache
  114. $redis->hset($dkey, $ukey, $html);
  115. $msg = 'cache is set';
  116. }
  117. }
  118.  
  119. $end = microtime(); // get end execution time
  120.  
  121. // show messages if debug is enabled
  122. if ($debug) {
  123. echo "REDIS DEBUG ";
  124. echo $msg.': ';
  125. echo t_exec($start, $end);
  126. }
  127.  
  128. if ($cached && $display_powered_by_redis) {
  129. // You should move this CSS to your CSS file and change the: float:right;margin:20px 0;
  130. 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;}
  131. #redis_powered div{width:190px;text-align:right;font:10px/11px arial,sans-serif;color:#000;}</style>";
  132. echo "<a href='http://www.jimwestergren.com/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>";
  133. }
  134.  
  135. // time diff
  136. function t_exec($start, $end) {
  137. $t = (getmicrotime($end) - getmicrotime($start));
  138. return round($t,5);
  139. }
  140.  
  141. // get time
  142. function getmicrotime($t) {
  143. list($usec, $sec) = explode(" ",$t);
  144. return ((float)$usec + (float)$sec);
  145. }
  146.  
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement