index-wp-redis.php 9.5 KB
Newer Older
Benjamin Adams's avatar
Benjamin Adams committed
1
<?php
2 3 4 5 6 7 8
/**
 * WP REDIS CACHE
 */

/**
 * GLOBAL CONFIGURATION
 */
9 10 11
global $wp_redis_cache_config;

$wp_redis_cache_config = array(
12 13 14 15 16 17 18 19
	'debug'          => false,
	'debug_messages' => '',
	'cache'          => false,
	'server_ip'      => '127.0.0.1',
	'redis_server'   => '127.0.0.1',
	'redis_port'     => 6379,
	'redis_db'       => 0,
	'secret_string'  => 'changeme',
20 21
);

22
// Uncomment either option below to fix the values here and disable the admin UI
23 24
// $wp_redis_cache_config['cache_duration'] = 43200;
// $wp_redis_cache_config['unlimited']      = false;
25

26 27 28 29 30
// Modify this function to introduce custom handling when exceptions occur
function wp_redis_cache_exception_handler( $exception ) {
	return;
}

31
/**
32 33
 * END GLOBAL CONFIGURATION
 *
34 35
 * DO NOT EDIT BELOW THIS LINE!
 */
36 37
$wp_redis_cache_config['current_url'] = wp_redis_cache_get_clean_url( $wp_redis_cache_config['secret_string'] );
$wp_redis_cache_config['redis_key']   = md5( $wp_redis_cache_config['current_url'] );
Benjamin Adams's avatar
Benjamin Adams committed
38

Ulrich Block's avatar
Ulrich Block committed
39
// Start the timer so we can track the page load time
40
if ( $wp_redis_cache_config['debug'] ) {
41 42
	$start = microtime();
}
Ulrich Block's avatar
Ulrich Block committed
43

44 45 46 47
/**
 * MOBILE HANDLING
 */
if ( wp_redis_cache_is_mobile_request() ) {
48
	$wp_redis_cache_config['redis_key'] = 'MO-' . $wp_redis_cache_config['redis_key'];
49 50
}

51 52 53 54 55 56 57 58 59
/**
 * UTILITY FUNCTIONS
 */

/**
 * Compute microtime from a timestamp
 *
 * @return float
 */
Erick Hitter's avatar
Erick Hitter committed
60
function wp_redis_cache_get_micro_time( $time ) {
Erick Hitter's avatar
Erick Hitter committed
61 62
	list( $usec, $sec ) = explode( " ", $time );
	return ( (float) $usec + (float) $sec );
Ulrich Block's avatar
Ulrich Block committed
63 64
}

65 66 67 68 69
/**
 * Is the current request a refresh request with the correct secret key?
 *
 * @return bool
 */
Erick Hitter's avatar
Erick Hitter committed
70
function wp_redis_cache_refresh_has_secret( $secret ) {
Erick Hitter's avatar
Erick Hitter committed
71
	return isset( $_GET['refresh'] ) && $secret == $_GET['refresh'];
Benjamin Adams's avatar
merged  
Benjamin Adams committed
72
}
Ulrich Block's avatar
Ulrich Block committed
73

74 75 76 77 78
/**
 * Does current request include a refresh request?
 *
 * @return bool
 */
Erick Hitter's avatar
Erick Hitter committed
79
function wp_redis_cache_request_has_secret( $secret ) {
Erick Hitter's avatar
Erick Hitter committed
80
	return false !== strpos( $_SERVER['REQUEST_URI'], "refresh=${secret}" );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
81
}
Hendrik Klemp's avatar
Hendrik Klemp committed
82

83 84 85 86 87
/**
 * Determine if request is from a server other than the one running this code
 *
 * @return bool
 */
Erick Hitter's avatar
Erick Hitter committed
88
function wp_redis_cache_is_remote_page_load( $current_url, $server_ip ) {
Erick Hitter's avatar
Erick Hitter committed
89
	return ( isset( $_SERVER['HTTP_REFERER'] )
90
			&& $_SERVER['HTTP_REFERER'] == $current_url
Erick Hitter's avatar
Erick Hitter committed
91
			&& $_SERVER['REQUEST_URI'] != '/'
92
			&& $_SERVER['REMOTE_ADDR'] != $server_ip );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
93
}
Hendrik Klemp's avatar
Hendrik Klemp committed
94

95 96 97 98 99
/**
 * Set proper IP address for proxied requests
 *
 * @return null
 */
Erick Hitter's avatar
Erick Hitter committed
100
function wp_redis_cache_handle_cdn_remote_addressing() {
Erick Hitter's avatar
Erick Hitter committed
101
	// so we don't confuse the cloudflare server
Erick Hitter's avatar
Erick Hitter committed
102
	if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
Erick Hitter's avatar
Erick Hitter committed
103 104
		$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
105
}
106

107 108 109 110 111 112 113 114
/**
 * Prepare a URL for use as a cache key
 *
 * Strips secret key from URL
 *
 * @param string
 * @return string
 */
Erick Hitter's avatar
Erick Hitter committed
115
function wp_redis_cache_get_clean_url( $secret ) {
116
	$replace_keys = array( "?refresh=${secret}","&refresh=${secret}" );
117 118
	$url          = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	return str_replace( $replace_keys, '', $url );
Benjamin Adams's avatar
Benjamin Adams committed
119
}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
120

121 122 123 124 125 126 127
/**
 *
 */
function wp_redis_cache_is_mobile_request() {
	return false;
}

128
/**
129 130 131 132 133
 * Establish a connection to the Redis server
 *
 * Will try the PECL module first, then fall back to PRedis
 *
 * @return object
134
 */
135
function wp_redis_cache_connect_redis() {
136 137
	global $wp_redis_cache_config;

Erick Hitter's avatar
Erick Hitter committed
138
	// check if PECL Extension is available
Erick Hitter's avatar
Erick Hitter committed
139
	if ( class_exists( 'Redis' ) ) {
140 141
		if ( $wp_redis_cache_config['debug'] ) {
			$wp_redis_cache_config['debug_messages'] .= "<!-- Redis PECL module found -->\n";
Erick Hitter's avatar
Erick Hitter committed
142
		}
Erick Hitter's avatar
Erick Hitter committed
143

Erick Hitter's avatar
Erick Hitter committed
144 145 146
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
147 148
		$redis->connect( $wp_redis_cache_config['redis_server'], $wp_redis_cache_config['redis_port'] );
		$redis->select( $wp_redis_cache_config['redis_db'] );
Erick Hitter's avatar
Erick Hitter committed
149
	} else { // Fallback to predis5.2.php
150 151
		if ( $wp_redis_cache_config['debug'] ) {
			$wp_redis_cache_config['debug_messages'] .= "<!-- using predis as a backup -->\n";
Erick Hitter's avatar
Erick Hitter committed
152
		}
Erick Hitter's avatar
Erick Hitter committed
153

154
		include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php'; //we need this to use Redis inside of PHP
155
		$redis = new Predis_Client( array(
156 157 158
			'host'     => $wp_redis_cache_config['redis_server'],
			'port'     => $wp_redis_cache_config['redis_port'],
			'database' => $wp_redis_cache_config['redis_db'],
159
		) );
Erick Hitter's avatar
Erick Hitter committed
160 161
	}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	return $redis;
}

/**
 * BEGIN CACHING LOGIC
 */

// Set proper IP for proxied requests
wp_redis_cache_handle_cdn_remote_addressing();

// Ensure WP uses a theme (this is normally set in index.php)
if ( ! defined( 'WP_USE_THEMES' ) ) {
	define( 'WP_USE_THEMES', true );
}

try {
	// Establish connection with Redis server
	$redis = wp_redis_cache_connect_redis();

181 182 183
	// Whether we need to load WP
	$load_wp = true;

184 185 186 187
	// Relevant details on the current request
	$is_post   = (bool) 'POST' === $_SERVER['REQUEST_METHOD'];
	$logged_in = (bool) preg_match( "#(wordpress_(logged|sec)|comment_author)#", var_export( $_COOKIE, true ) );

188 189 190
	if ( $wp_redis_cache_config['debug'] ) {
		$wp_redis_cache_config['debug_messages'] .= "<!-- POST request: . " . ( $is_post ? 'yes' : 'no' ) . "-->\n";
		$wp_redis_cache_config['debug_messages'] .= "<!-- Logged in: . " . ( $logged_in ? 'yes' : 'no' ) . "-->\n";
191 192 193
	}

	// Refresh request, deletes cache: either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
194 195 196
	if ( wp_redis_cache_refresh_has_secret( $wp_redis_cache_config['secret_string'] ) || wp_redis_cache_request_has_secret( $wp_redis_cache_config['secret_string'] ) || wp_redis_cache_is_remote_page_load( $wp_redis_cache_config['current_url'], $wp_redis_cache_config['server_ip'] ) ) {
		if ( $wp_redis_cache_config['debug'] ) {
			$wp_redis_cache_config['debug_messages'] .= "<!-- manual refresh was required -->\n";
Erick Hitter's avatar
Erick Hitter committed
197
		}
Erick Hitter's avatar
Erick Hitter committed
198

199
		$redis->del( $wp_redis_cache_config['redis_key'] );
200
	// This page is cached, the user isn't logged in, and it isn't a POST request, so let's use the cache
201 202 203
	} elseif ( ! $is_post && ! $logged_in && $redis->exists( $wp_redis_cache_config['redis_key'] ) ) {
		if ( $wp_redis_cache_config['debug'] ) {
			$wp_redis_cache_config['debug_messages'] .= "<!-- serving page from cache: key: " . $wp_redis_cache_config['redis_key'] . " -->\n";
204 205
		}

206 207
		// Page is served from cache, so we don't need WP
		$load_wp = false;
208
		$wp_redis_cache_config['cached'] = true;
209

210
		echo trim( $redis->get( $wp_redis_cache_config['redis_key'] ) );
Erick Hitter's avatar
Erick Hitter committed
211
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
212 213 214 215
	} elseif ( $_SERVER['REMOTE_ADDR'] != $wp_redis_cache_config['server_ip'] ) {
		if ( false === strstr( $wp_redis_cache_config['current_url'], 'preview=true' ) ) {
			if ( $wp_redis_cache_config['debug'] ) {
				$wp_redis_cache_config['debug_messages'] .= "<!-- displaying page without cache -->\n";
216
			}
217

218 219 220 221 222 223 224
			// If user isn't logged in and this isn't a post request, render the requested page and cache if appropriate.
			if ( ! $is_post && ! $logged_in ) {
				// We load WP to generate the cached output, so no need to load again
				$load_wp = false;

				// Render page into an output buffer and display
				ob_start();
225
				require_once dirname( __FILE__ ) . '/wp-blog-header.php';
226 227 228 229 230 231
				$markup_to_cache = trim( ob_get_clean() );
				echo $markup_to_cache;

				// Cache rendered page if appropriate
				if ( ! is_404() && ! is_search() ) {
					// Is unlimited cache life requested?
232 233
					if ( isset( $wp_redis_cache_config['unlimited'] ) ) {
						$unlimited = $wp_redis_cache_config['unlimited'];
234
					} else {
235
						$unlimited = (bool) get_option( 'wp-redis-cache-debug', false );
236
						$wp_redis_cache_config['unlimited'] = $unlimited;
237 238
					}

239 240
					// Cache the page for the chosen duration
					if ( $unlimited ) {
241
						$redis->set( $wp_redis_cache_config['redis_key'], $markup_to_cache );
242
					} else {
243 244
						if ( isset( $wp_redis_cache_config['cache_duration'] ) ) {
							$cache_duration = $wp_redis_cache_config['cache_duration'];
245 246
						} else {
							$cache_duration = (int) get_option( 'wp-redis-cache-seconds', 43200 );
247
							$wp_redis_cache_config['cache_duration'] = $cache_duration;
248 249 250
						}

						if ( ! is_numeric( $cache_duration ) ) {
251
							$cache_duration = $wp_redis_cache_config['cache_duration'] = 43200;
252 253
						}

254
						$redis->setex( $wp_redis_cache_config['redis_key'], $cache_duration, $markup_to_cache );
255
					}
Erick Hitter's avatar
Erick Hitter committed
256
				}
257 258 259
			}
		}
	}
260 261 262

	// The current request wasn't served from cache or isn't cacheable, so we pass off to WP
	if ( $load_wp ) {
263
		require_once dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
264
	}
Erick Hitter's avatar
Erick Hitter committed
265
} catch ( Exception $e ) {
266
	require_once dirname( __FILE__ ) . '/wp-blog-header.php';
267
	wp_redis_cache_exception_handler( $e );
Benjamin Adams's avatar
Benjamin Adams committed
268 269
}

270 271 272
/**
 * DEBUGGING OUTPUT
 */
273
if ( $wp_redis_cache_config['debug'] ) {
274 275
	$end  = microtime();
	$time = @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start );
276 277 278 279
	$wp_redis_cache_config['debug_messages'] .= "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
	$wp_redis_cache_config['debug_messages'] .= "<!-- Site was cached = " . $wp_redis_cache_config['cached'] . " -->\n";
	if ( isset( $wp_redis_cache_config['cache_duration'] ) ) {
		$wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-seconds = " . $wp_redis_cache_config['cache_duration'] . " -->\n";
Erick Hitter's avatar
Erick Hitter committed
280
	}
281 282 283
	$wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-ip = " . $wp_redis_cache_config['server_ip'] . "-->\n";
	if ( isset( $wp_redis_cache_config['unlimited'] ) ) {
		$wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-unlimited = " . $wp_redis_cache_config['unlimited'] . "-->\n";
Erick Hitter's avatar
Erick Hitter committed
284
	}
285
	$wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-debug = " . $wp_redis_cache_config['debug'] . "-->\n";
286

287
	echo $wp_redis_cache_config['debug_messages'];
Benjamin Adams's avatar
Benjamin Adams committed
288
}