index-wp-redis.php 7.9 KB
Newer Older
Benjamin Adams's avatar
Benjamin Adams committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * WP REDIS CACHE
 */

/**
 * GLOBAL CONFIGURATION
 */
$GLOBALS['wp_redis_cache_config'] = array(
	'debug'         => false,
	'cache'         => true,
	'server_ip'     => '127.0.0.1',
	'redis_server'  => '127.0.0.1',
	'redis_port'    => 6379,
	'redis_db'      => 0,
	'secret_string' => 'changeme',
);

19 20 21
// Uncomment either option below to fix the values here and disable the admin UI
// $GLOBALS['wp_redis_cache_config']['cache_duration'] = 43200;
// $GLOBALS['wp_redis_cache_config']['unlimited']      = false;
22

23 24 25 26 27
// Modify this function to introduce custom handling when exceptions occur
function wp_redis_cache_exception_handler( $exception ) {
	return;
}

28 29 30
/**
 * DO NOT EDIT BELOW THIS LINE!
 */
Erick Hitter's avatar
Erick Hitter committed
31
$GLOBALS['wp_redis_cache_config']['current_url'] = wp_redis_cache_get_clean_url( $GLOBALS['wp_redis_cache_config']['secret_string'] );
32
$GLOBALS['wp_redis_cache_config']['redis_key']   = md5( $GLOBALS['wp_redis_cache_config']['current_url'] );
Benjamin Adams's avatar
Benjamin Adams committed
33

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

Erick Hitter's avatar
Erick Hitter committed
37
function wp_redis_cache_get_micro_time( $time ) {
Erick Hitter's avatar
Erick Hitter committed
38 39
	list( $usec, $sec ) = explode( " ", $time );
	return ( (float) $usec + (float) $sec );
Ulrich Block's avatar
Ulrich Block committed
40 41
}

Erick Hitter's avatar
Erick Hitter committed
42
function wp_redis_cache_refresh_has_secret( $secret ) {
Erick Hitter's avatar
Erick Hitter committed
43
	return isset( $_GET['refresh'] ) && $secret == $_GET['refresh'];
Benjamin Adams's avatar
merged  
Benjamin Adams committed
44
}
Ulrich Block's avatar
Ulrich Block committed
45

Erick Hitter's avatar
Erick Hitter committed
46
function wp_redis_cache_request_has_secret( $secret ) {
Erick Hitter's avatar
Erick Hitter committed
47
	return false !== strpos( $_SERVER['REQUEST_URI'], "refresh=${secret}" );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
48
}
Hendrik Klemp's avatar
Hendrik Klemp committed
49

Erick Hitter's avatar
Erick Hitter committed
50
function wp_redis_cache_is_remote_page_load( $current_url, $server_ip ) {
Erick Hitter's avatar
Erick Hitter committed
51
	return ( isset( $_SERVER['HTTP_REFERER'] )
52
			&& $_SERVER['HTTP_REFERER'] == $current_url
Erick Hitter's avatar
Erick Hitter committed
53
			&& $_SERVER['REQUEST_URI'] != '/'
54
			&& $_SERVER['REMOTE_ADDR'] != $server_ip );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
55
}
Hendrik Klemp's avatar
Hendrik Klemp committed
56

Erick Hitter's avatar
Erick Hitter committed
57
function wp_redis_cache_handle_cdn_remote_addressing() {
Erick Hitter's avatar
Erick Hitter committed
58
	// so we don't confuse the cloudflare server
Erick Hitter's avatar
Erick Hitter committed
59
	if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
Erick Hitter's avatar
Erick Hitter committed
60 61
		$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
62
}
63

Erick Hitter's avatar
Erick Hitter committed
64
function wp_redis_cache_get_clean_url( $secret ) {
65
	$replace_keys = array( "?refresh=${secret}","&refresh=${secret}" );
Erick Hitter's avatar
Erick Hitter committed
66
	$url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
67
	$current_url = str_replace( $replace_keys, '', $url );
Erick Hitter's avatar
Erick Hitter committed
68
	return $current_url;
Benjamin Adams's avatar
Benjamin Adams committed
69
}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
70

Erick Hitter's avatar
Erick Hitter committed
71
wp_redis_cache_handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
72

Erick Hitter's avatar
Erick Hitter committed
73 74
if ( ! defined( 'WP_USE_THEMES' ) ) {
	define( 'WP_USE_THEMES', true );
Vasili Pascal's avatar
Vasili Pascal committed
75
}
Benjamin Adams's avatar
Benjamin Adams committed
76

Ulrich Block's avatar
Ulrich Block committed
77
try {
Erick Hitter's avatar
Erick Hitter committed
78
	// check if PECL Extension is available
Erick Hitter's avatar
Erick Hitter committed
79
	if ( class_exists( 'Redis' ) ) {
80
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
81 82
			echo "<!-- Redis PECL module found -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
83

Erick Hitter's avatar
Erick Hitter committed
84 85 86
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
87 88
		$redis->connect( $GLOBALS['wp_redis_cache_config']['redis_server'], $GLOBALS['wp_redis_cache_config']['redis_port'] );
		$redis->select( $GLOBALS['wp_redis_cache_config']['redis_db'] );
Erick Hitter's avatar
Erick Hitter committed
89
	} else { // Fallback to predis5.2.php
90
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
91 92
			echo "<!-- using predis as a backup -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
93

94
		include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php'; //we need this to use Redis inside of PHP
95
		$redis = new Predis_Client( array(
96 97 98
			'host'     => $GLOBALS['wp_redis_cache_config']['redis_server'],
			'port'     => $GLOBALS['wp_redis_cache_config']['redis_port'],
			'database' => $GLOBALS['wp_redis_cache_config']['redis_db'],
99
		) );
Erick Hitter's avatar
Erick Hitter committed
100 101 102
	}

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
Erick Hitter's avatar
Erick Hitter committed
103
	if ( wp_redis_cache_refresh_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || wp_redis_cache_request_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || wp_redis_cache_is_remote_page_load( $GLOBALS['wp_redis_cache_config']['current_url'], $GLOBALS['wp_redis_cache_config']['server_ip'] ) ) {
104
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
105 106
			echo "<!-- manual refresh was required -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
107

108
		$redis->del( $GLOBALS['wp_redis_cache_config']['redis_key'] );
Erick Hitter's avatar
Erick Hitter committed
109

110
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
111
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
112 113
	} elseif ( $_SERVER['REMOTE_ADDR'] != $GLOBALS['wp_redis_cache_config']['server_ip'] && false === strstr( $GLOBALS['wp_redis_cache_config']['current_url'], 'preview=true' ) ) {
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
114 115 116
			echo "<!-- displaying page without cache -->\n";
		}

117 118
		$is_post   = (bool) 'POST' === $_SERVER['REQUEST_METHOD'];
		$logged_in = (bool) preg_match( "#(wordpress_(logged|sec)|comment_author)#", var_export( $_COOKIE, true ) );
Erick Hitter's avatar
Erick Hitter committed
119

120 121 122 123 124
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
			echo "<!-- POST request: . " . ( $is_post ? 'yes' : 'no' ) . "-->\n";
			echo "<!-- Logged in: . " . ( $logged_in ? 'yes' : 'no' ) . "-->\n";
		}

125
		if ( ! $is_post && ! $logged_in ) {
Erick Hitter's avatar
Erick Hitter committed
126
			ob_start();
127
			require dirname( __FILE__ ) . '/wp-blog-header.php';
128
			$html_of_page = trim( ob_get_clean() );
Erick Hitter's avatar
Erick Hitter committed
129 130 131 132
			echo $html_of_page;

			// When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
			// When the search was used, do not cache
133
			if ( ! is_404() && ! is_search() ) {
134 135 136 137 138 139 140 141
				if ( isset( $GLOBALS['wp_redis_cache_config']['unlimited'] ) ) {
					$unlimited = $GLOBALS['wp_redis_cache_config']['unlimited'];
				} else {
					$unlimited = (bool) get_option( 'wp-redis-cache-debug', false );
					$GLOBALS['wp_redis_cache_config']['unlimited'] = $unlimited;
				}

				// Determine how long to cache the page for and set the cache
Erick Hitter's avatar
Erick Hitter committed
142
				if ( $unlimited ) {
143
					$redis->set( $GLOBALS['wp_redis_cache_config']['redis_key'], $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
144
				} else {
145 146 147 148 149 150 151 152 153 154 155 156
					if ( isset( $GLOBALS['wp_redis_cache_config']['cache_duration'] ) ) {
						$cache_duration = $GLOBALS['wp_redis_cache_config']['cache_duration'];
					} else {
						$cache_duration = (int) get_option( 'wp-redis-cache-seconds', 43200 );
						$GLOBALS['wp_redis_cache_config']['cache_duration'] = $cache_duration;
					}

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

					$redis->setex( $GLOBALS['wp_redis_cache_config']['redis_key'], $cache_duration, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
157 158 159
				}
			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
160
			require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
161
		}
162
	} elseif ( $_SERVER['REMOTE_ADDR'] != $GLOBALS['wp_redis_cache_config']['server_ip'] && true === strstr( $GLOBALS['wp_redis_cache_config']['current_url'], 'preview=true' ) ) {
163
		require dirname( __FILE__ ) . '/wp-blog-header.php';
164
	// This page is cached, lets display it
165 166 167
	} elseif ( $redis->exists( $GLOBALS['wp_redis_cache_config']['redis_key'] ) ) {
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
			echo "<!-- serving page from cache: key: " . $GLOBALS['wp_redis_cache_config']['redis_key'] . " -->\n";
168 169
		}

Erick Hitter's avatar
Erick Hitter committed
170
		$GLOBALS['wp_redis_cache_config']['cache'] = true;
171

172
		$html_of_page = trim( $redis->get( $GLOBALS['wp_redis_cache_config']['redis_key'] ) );
173 174
		echo $html_of_page;

175 176
	} else {
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
177
	}
Erick Hitter's avatar
Erick Hitter committed
178
} catch ( Exception $e ) {
179
	require dirname( __FILE__ ) . '/wp-blog-header.php';
180
	wp_redis_cache_exception_handler( $e );
Benjamin Adams's avatar
Benjamin Adams committed
181 182
}

Hendrik Klemp's avatar
Hendrik Klemp committed
183
$end  = microtime();
Erick Hitter's avatar
Erick Hitter committed
184
$time = @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start );
185
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
186
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
Erick Hitter's avatar
Erick Hitter committed
187
	echo "<!-- Site was cached = " . $GLOBALS['wp_redis_cache_config']['cache'] . " -->\n";
188
	if ( isset( $GLOBALS['wp_redis_cache_config']['cache_duration'] ) ) {
Erick Hitter's avatar
Erick Hitter committed
189
		echo "<!-- wp-redis-cache-seconds = " . $GLOBALS['wp_redis_cache_config']['cache_duration'] . " -->\n";
Erick Hitter's avatar
Erick Hitter committed
190
	}
Erick Hitter's avatar
Erick Hitter committed
191
	echo "<!-- wp-redis-cache-ip = " . $GLOBALS['wp_redis_cache_config']['server_ip'] . "-->\n";
192 193
	if ( isset( $GLOBALS['wp_redis_cache_config']['unlimited'] ) ) {
		echo "<!-- wp-redis-cache-unlimited = " . $GLOBALS['wp_redis_cache_config']['unlimited'] . "-->\n";
Erick Hitter's avatar
Erick Hitter committed
194
	}
Erick Hitter's avatar
Erick Hitter committed
195
	echo "<!-- wp-redis-cache-debug = " . $GLOBALS['wp_redis_cache_config']['debug'] . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
196
}