index-wp-redis.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. $start = microtime();
  3. function getMicroTime($t)
  4. {
  5. list($usec, $sec) = explode(" ", $t);
  6. return ((float) $usec + (float) $sec);
  7. }
  8. $debug = true;
  9. $cache = false;
  10. $ip_of_your_website = '127.0.0.1';
  11. $secret_string = 'changeme';
  12. // so we don't confuse the cloudflare server
  13. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  14. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  15. }
  16. if(!defined('WP_USE_THEMES')) {
  17. define('WP_USE_THEMES', true);
  18. }
  19. $current_url = str_replace(array("?refresh=${secret_string}","&refresh=${secret_string}"), '', "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}"); //clean up the URL
  20. $redis_key = md5($current_url);
  21. try {
  22. // check if PECL Extension is available
  23. if (class_exists('Redis')) {
  24. $redis = new Redis();
  25. // Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
  26. $redis->connect('127.0.0.1');
  27. } else // Fallback to predis5.2.php
  28. {
  29. include_once("app/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
  30. $redis = new Predis_Client();
  31. }
  32. //Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
  33. if (isset($_GET['refresh']) || $_GET['refresh'] == $secret_string || strpos($_SERVER['REQUEST_URI'],"refresh=${secret_string}")!==false || ($_SERVER['HTTP_REFERER'] == $current_url && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REMOTE_ADDR'] != $ip_of_your_website)) {
  34. $redis->del($redis_key);
  35. require('./wp-blog-header.php');
  36. // This page is cached, lets display it
  37. } else if ($redis->exists($redis_key)) {
  38. $cache = true;
  39. $html_of_page = $redis->get($redis_key);
  40. echo $html_of_page;
  41. // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
  42. } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == false) {
  43. $isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0;
  44. $loggedIn = preg_match("/wordpress_logged_in/", var_export($_COOKIE, true));
  45. if ($isPost == 0 && $loggedIn == 0) {
  46. ob_start();
  47. require('./wp-blog-header.php');
  48. $html_of_page = ob_get_contents();
  49. ob_end_clean();
  50. echo $html_of_page;
  51. $unlimited = get_option('wp-redis-cache-debug',false);
  52. $seconds_cache_redis = get_option('wp-redis-cache-seconds',43200);
  53. if (!is_numeric($seconds_cache_redis)) {
  54. $seconds_cache_redis = 43200;
  55. }
  56. // When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
  57. // When the search was used, do not cache
  58. if ((!is_404()) and (!is_search())) {
  59. if ($unlimited) {
  60. $redis->set($redis_key, $html_of_page);
  61. }
  62. else
  63. {
  64. $redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
  65. }
  66. }
  67. } else //either the user is logged in, or is posting a comment, show them uncached
  68. {
  69. require('./wp-blog-header.php');
  70. }
  71. } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == true) {
  72. require('./wp-blog-header.php');
  73. }
  74. // else { // This is what your server should get if no cache exists //depricated, as the ob_start() is cleaner
  75. // require('./wp-blog-header.php');
  76. // }
  77. } catch (Exception $e) {
  78. require('./wp-blog-header.php');
  79. }
  80. $end = microtime();
  81. $time = (@getMicroTime($end) - @getMicroTime($start));
  82. if ($debug) {
  83. echo "<!-- Page generated in " . round($time, 5) . " seconds. -->";
  84. echo "<!-- Site was cached = " . $cache . " -->";
  85. echo "<!-- wp-redis-cache-seconds = " . $seconds_cache_redis . " -->";
  86. echo "<!-- wp-redis-cache-secret = " . $secret_string . "-->";
  87. echo "<!-- wp-redis-cache-ip = " . $ip_of_your_website . "-->";
  88. echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->";
  89. echo "<!-- wp-redis-cache-debug = " . $debug . "-->";
  90. }