index-wp-redis.php 4.34 KB
Newer Older
Benjamin Adams's avatar
Benjamin Adams committed
1 2
<?php

Ulrich Block's avatar
Ulrich Block committed
3 4 5 6 7 8 9 10 11
// Start the timer so we can track the page load time
$start = microtime();

function getMicroTime($t)
{
    list($usec, $sec) = explode(" ", $t);
    return ((float) $usec + (float) $sec);
}

Ulrich Block's avatar
Ulrich Block committed
12

Hendrik Klemp's avatar
Hendrik Klemp committed
13 14
$debug               =  true;
$cache				 =  false;
Hendrik Klemp's avatar
Hendrik Klemp committed
15 16 17 18 19
$ip_of_your_website  =  '127.0.0.1';
$secret_string       =  'changeme';



20

Benjamin Adams's avatar
Benjamin Adams committed
21 22
// so we don't confuse the cloudflare server 
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
23
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
Benjamin Adams's avatar
Benjamin Adams committed
24
}
Vasili Pascal's avatar
Vasili Pascal committed
25
 
Hendrik Klemp's avatar
Hendrik Klemp committed
26

Vasili Pascal's avatar
Vasili Pascal committed
27 28 29
if(!defined('WP_USE_THEMES')) {
    define('WP_USE_THEMES', true);
}
Benjamin Adams's avatar
Benjamin Adams committed
30

Ulrich Block's avatar
Ulrich Block committed
31
$current_url = str_replace(array("?refresh=${secret_string}","&refresh=${secret_string}"), '', "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}"); //clean up the URL
Ulrich Block's avatar
Ulrich Block committed
32 33
$redis_key = md5($current_url);

34 35 36 37
// check if the user was  logged in to wp
$cookie = var_export($_COOKIE, true);
$loggedin = preg_match("/wordpress_logged_in/", $cookie);

Ulrich Block's avatar
Ulrich Block committed
38
try {
Ulrich Block's avatar
Ulrich Block committed
39 40 41 42
    // check if PECL Extension is available
    if (class_exists('Redis')) {
        $redis = new Redis();
        
Ulrich Block's avatar
Ulrich Block committed
43
        // Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
Ulrich Block's avatar
Ulrich Block committed
44 45 46 47
        $redis->connect('127.0.0.1');
        
    } else // Fallback to predis5.2.php
    {
48
        include_once("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
Ulrich Block's avatar
Ulrich Block committed
49 50
        $redis = new Predis_Client();
    }
51
    
Ulrich Block's avatar
Ulrich Block committed
52 53 54 55
    //Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
    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)) {
        
        $redis->del($redis_key);
56
        require('./wp-blog-header.php');
57 58 59 60 61 62
    
	// if the user was logged in, don't show a cached site    
	} else if ($loggedin) {
		
		require('./wp-blog-header.php');
		
Ulrich Block's avatar
Ulrich Block committed
63 64
    // This page is cached, lets display it
    } else if ($redis->exists($redis_key)) {
Hendrik Klemp's avatar
Hendrik Klemp committed
65
		$cache  = true;
Ulrich Block's avatar
Ulrich Block committed
66
        $html_of_page = $redis->get($redis_key);
67
		echo $html_of_page;
68
        
Ulrich Block's avatar
Ulrich Block committed
69 70 71 72
     // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
    } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == false) {
        
        $isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0;
73
        
74
        if ($isPost == 0) {
Ulrich Block's avatar
Ulrich Block committed
75 76 77 78
            ob_start();
            require('./wp-blog-header.php');
            $html_of_page = ob_get_contents();
            ob_end_clean();
79
			echo $html_of_page;
Hendrik Klemp's avatar
Hendrik Klemp committed
80 81 82 83 84 85 86
			
			$unlimited			 =  get_option('wp-redis-cache-debug',false);
			$seconds_cache_redis =  get_option('wp-redis-cache-seconds',43200);
			if (!is_numeric($seconds_cache_redis)) {
				$seconds_cache_redis = 43200;
			}
			
Ulrich Block's avatar
Ulrich Block committed
87 88
			
			// When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
Hendrik Klemp's avatar
Hendrik Klemp committed
89
			// When the search was used, do not cache
Hendrik Klemp's avatar
Hendrik Klemp committed
90
            if ((!is_404()) and (!is_search()))  {
Hendrik Klemp's avatar
Hendrik Klemp committed
91
                if ($unlimited) {
Hendrik Klemp's avatar
Hendrik Klemp committed
92
                	$redis->set($redis_key, $html_of_page);
Hendrik Klemp's avatar
Hendrik Klemp committed
93 94 95 96 97 98
                }
				else
				{
					$redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
				}

Ulrich Block's avatar
Ulrich Block committed
99
            }
100 101
		//either the user is logged in, or is posting a comment, show them uncached
        } else {
Ulrich Block's avatar
Ulrich Block committed
102
            require('./wp-blog-header.php');
103
        }
Ulrich Block's avatar
Ulrich Block committed
104 105
        
    } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == true) {
106 107
        require('./wp-blog-header.php');
    }
Ulrich Block's avatar
Ulrich Block committed
108 109 110 111
     // else {   // This is what your server should get if no cache exists  //depricated, as the ob_start() is cleaner
        // require('./wp-blog-header.php');
    // }
} catch (Exception $e) {
Ulrich Block's avatar
Ulrich Block committed
112
    require('./wp-blog-header.php');
Benjamin Adams's avatar
Benjamin Adams committed
113 114
}

Hendrik Klemp's avatar
Hendrik Klemp committed
115 116 117 118 119 120 121 122 123 124 125

$end  = microtime();
$time = (@getMicroTime($end) - @getMicroTime($start));
if ($debug) {
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->";
	echo "<!-- Site was cached  = " . $cache . " -->";
	echo "<!-- wp-redis-cache-seconds  = " . $seconds_cache_redis . " -->";
	echo "<!-- wp-redis-cache-secret  = " . $secret_string . "-->";
	echo "<!-- wp-redis-cache-ip  = " . $ip_of_your_website . "-->";
	echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->";
	echo "<!-- wp-redis-cache-debug  = " . $debug . "-->";
Benjamin Adams's avatar
Benjamin Adams committed
126
}
Hendrik Klemp's avatar
Hendrik Klemp committed
127