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

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

Erick Hitter's avatar
Erick Hitter committed
6 7 8
function get_micro_time( $time ) {
	list( $usec, $sec ) = explode( " ", $time );
	return ( (float) $usec + (float) $sec );
Ulrich Block's avatar
Ulrich Block committed
9 10
}

Erick Hitter's avatar
Erick Hitter committed
11 12
function refresh_has_secret( $secret ) {
	return isset( $_GET['refresh'] ) && $secret == $_GET['refresh'];
Benjamin Adams's avatar
merged  
Benjamin Adams committed
13
}
Ulrich Block's avatar
Ulrich Block committed
14

Erick Hitter's avatar
Erick Hitter committed
15 16
function request_has_secret( $secret ) {
	return false !== strpos( $_SERVER['REQUEST_URI'], "refresh=${secret}" );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
17
}
Hendrik Klemp's avatar
Hendrik Klemp committed
18

19
function is_remote_page_load( $current_url, $server_ip ) {
Erick Hitter's avatar
Erick Hitter committed
20
	return ( isset( $_SERVER['HTTP_REFERER'] )
21
			&& $_SERVER['HTTP_REFERER'] == $current_url
Erick Hitter's avatar
Erick Hitter committed
22
			&& $_SERVER['REQUEST_URI'] != '/'
23
			&& $_SERVER['REMOTE_ADDR'] != $server_ip );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
24
}
Hendrik Klemp's avatar
Hendrik Klemp committed
25

26
function handle_cdn_remote_addressing() {
Erick Hitter's avatar
Erick Hitter committed
27
	// so we don't confuse the cloudflare server
Erick Hitter's avatar
Erick Hitter committed
28
	if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
Erick Hitter's avatar
Erick Hitter committed
29 30
		$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
31
}
32

Erick Hitter's avatar
Erick Hitter committed
33
function get_clean_url( $secret ) {
34
	$replace_keys = array( "?refresh=${secret}","&refresh=${secret}" );
Erick Hitter's avatar
Erick Hitter committed
35
	$url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
36
	$current_url = str_replace( $replace_keys, '', $url );
Erick Hitter's avatar
Erick Hitter committed
37
	return $current_url;
Benjamin Adams's avatar
Benjamin Adams committed
38
}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
39

Erick Hitter's avatar
Erick Hitter committed
40 41
$debug         = true;
$cache         = true;
42
$server_ip     = '127.0.0.1';
Erick Hitter's avatar
Erick Hitter committed
43 44 45 46
$reddis_server = '127.0.0.1';
$secret_string = 'changeme';
$current_url   = get_clean_url( $secret_string );
$redis_key     = md5( $current_url );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
47

48
handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
49

Erick Hitter's avatar
Erick Hitter committed
50 51
if ( ! defined( 'WP_USE_THEMES' ) ) {
	define( 'WP_USE_THEMES', true );
Vasili Pascal's avatar
Vasili Pascal committed
52
}
Benjamin Adams's avatar
Benjamin Adams committed
53

Ulrich Block's avatar
Ulrich Block committed
54
try {
Erick Hitter's avatar
Erick Hitter committed
55
	// check if PECL Extension is available
Erick Hitter's avatar
Erick Hitter committed
56 57
	if ( class_exists( 'Redis' ) ) {
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
58 59
			echo "<!-- Redis PECL module found -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
60

Erick Hitter's avatar
Erick Hitter committed
61 62 63
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
Erick Hitter's avatar
Erick Hitter committed
64
		$redis->connect( $reddis_server );
Erick Hitter's avatar
Erick Hitter committed
65 66

	} else { // Fallback to predis5.2.php
Erick Hitter's avatar
Erick Hitter committed
67
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
68 69
			echo "<!-- using predis as a backup -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
70 71

		include_once( "wp-content/plugins/wp-redis-cache/predis5.2.php" ); //we need this to use Redis inside of PHP
Erick Hitter's avatar
Erick Hitter committed
72 73 74 75
		$redis = new Predis_Client();
	}

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
76
	if ( refresh_has_secret( $secret_string ) || request_has_secret( $secret_string ) || is_remote_page_load( $current_url, $server_ip ) ) {
Erick Hitter's avatar
Erick Hitter committed
77
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
78 79
			echo "<!-- manual refresh was required -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
80 81 82

		$redis->del( $redis_key );

83
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
84

85
		$unlimited           = get_option( 'wp-redis-cache-debug',   false );
Erick Hitter's avatar
Erick Hitter committed
86 87
		$seconds_cache_redis = get_option( 'wp-redis-cache-seconds', 43200 );

Erick Hitter's avatar
Erick Hitter committed
88
	// This page is cached, lets display it
Erick Hitter's avatar
Erick Hitter committed
89 90
	} elseif ( $redis->exists( $redis_key ) ) {
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
91 92
			echo "<!-- serving page from cache: key: $redis_key -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
93

Erick Hitter's avatar
Erick Hitter committed
94
		$cache  = true;
Erick Hitter's avatar
Erick Hitter committed
95

96
		$html_of_page = trim( $redis->get( $redis_key ) );
Erick Hitter's avatar
Erick Hitter committed
97 98
		echo $html_of_page;

Erick Hitter's avatar
Erick Hitter committed
99
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
100
	} elseif ( $_SERVER['REMOTE_ADDR'] != $server_ip && false === strstr( $current_url, 'preview=true' ) ) {
Erick Hitter's avatar
Erick Hitter committed
101
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
102 103 104
			echo "<!-- displaying page without cache -->\n";
		}

105 106
		$is_post  = (int) 'POST' === $_SERVER['REQUEST_METHOD'];
		$logged_in = preg_match( "/wordpress_logged_in/", var_export( $_COOKIE, true ) );
Erick Hitter's avatar
Erick Hitter committed
107

108
		if ( ! $is_post && ! $logged_in ) {
Erick Hitter's avatar
Erick Hitter committed
109
			ob_start();
110
			require dirname( __FILE__ ) . '/wp-blog-header.php';
111
			$html_of_page = trim( ob_get_clean() );
Erick Hitter's avatar
Erick Hitter committed
112 113
			echo $html_of_page;

Erick Hitter's avatar
Erick Hitter committed
114
			if ( ! is_numeric( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
115 116 117 118 119
				$seconds_cache_redis = 43200;
			}

			// When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
			// When the search was used, do not cache
Erick Hitter's avatar
Erick Hitter committed
120 121 122
			if ( ( ! is_404() ) and ( ! is_search() ) )  {
				if ( $unlimited ) {
					$redis->set( $redis_key, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
123
				} else {
Erick Hitter's avatar
Erick Hitter committed
124
					$redis->setex( $redis_key, $seconds_cache_redis, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
125 126 127
				}
			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
128
			require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
129
		}
130
	} elseif ( $_SERVER['REMOTE_ADDR'] != $server_ip && true === strstr( $current_url, 'preview=true' ) ) {
131
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
132 133
	}
	 // else {   // This is what your server should get if no cache exists  //deprecated, as the ob_start() is cleaner
134
		// require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
135
	// }
Erick Hitter's avatar
Erick Hitter committed
136
} catch ( Exception $e ) {
137
	//require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
138
	echo "something went wrong";
Benjamin Adams's avatar
Benjamin Adams committed
139 140
}

Hendrik Klemp's avatar
Hendrik Klemp committed
141
$end  = microtime();
Erick Hitter's avatar
Erick Hitter committed
142 143
$time = @get_micro_time( $end ) - @get_micro_time( $start );
if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
144 145
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
	echo "<!-- Site was cached  = " . $cache . " -->\n";
Erick Hitter's avatar
Erick Hitter committed
146
	if ( isset( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
147 148 149
		echo "<!-- wp-redis-cache-seconds  = " . $seconds_cache_redis . " -->\n";
	}
	echo "<!-- wp-redis-cache-secret  = " . $secret_string . "-->\n";
150
	echo "<!-- wp-redis-cache-ip  = " . $server_ip . "-->\n";
Erick Hitter's avatar
Erick Hitter committed
151
	if ( isset( $unlimited ) ) {
Erick Hitter's avatar
Erick Hitter committed
152 153 154
		echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n";
	}
	echo "<!-- wp-redis-cache-debug  = " . $debug . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
155
}