index-wp-redis.php 7.53 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

/**
 * DO NOT EDIT BELOW THIS LINE!
 */
26 27
$GLOBALS['wp_redis_cache_config']['current_url'] = get_clean_url( $GLOBALS['wp_redis_cache_config']['secret_string'] );
$GLOBALS['wp_redis_cache_config']['redis_key']   = md5( $GLOBALS['wp_redis_cache_config']['current_url'] );
Benjamin Adams's avatar
Benjamin Adams committed
28

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

Erick Hitter's avatar
Erick Hitter committed
32 33 34
function get_micro_time( $time ) {
	list( $usec, $sec ) = explode( " ", $time );
	return ( (float) $usec + (float) $sec );
Ulrich Block's avatar
Ulrich Block committed
35 36
}

Erick Hitter's avatar
Erick Hitter committed
37 38
function refresh_has_secret( $secret ) {
	return isset( $_GET['refresh'] ) && $secret == $_GET['refresh'];
Benjamin Adams's avatar
merged  
Benjamin Adams committed
39
}
Ulrich Block's avatar
Ulrich Block committed
40

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

45
function is_remote_page_load( $current_url, $server_ip ) {
Erick Hitter's avatar
Erick Hitter committed
46
	return ( isset( $_SERVER['HTTP_REFERER'] )
47
			&& $_SERVER['HTTP_REFERER'] == $current_url
Erick Hitter's avatar
Erick Hitter committed
48
			&& $_SERVER['REQUEST_URI'] != '/'
49
			&& $_SERVER['REMOTE_ADDR'] != $server_ip );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
50
}
Hendrik Klemp's avatar
Hendrik Klemp committed
51

52
function handle_cdn_remote_addressing() {
Erick Hitter's avatar
Erick Hitter committed
53
	// so we don't confuse the cloudflare server
Erick Hitter's avatar
Erick Hitter committed
54
	if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
Erick Hitter's avatar
Erick Hitter committed
55 56
		$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
57
}
58

Erick Hitter's avatar
Erick Hitter committed
59
function get_clean_url( $secret ) {
60
	$replace_keys = array( "?refresh=${secret}","&refresh=${secret}" );
Erick Hitter's avatar
Erick Hitter committed
61
	$url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
62
	$current_url = str_replace( $replace_keys, '', $url );
Erick Hitter's avatar
Erick Hitter committed
63
	return $current_url;
Benjamin Adams's avatar
Benjamin Adams committed
64
}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
65

66
handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
67

Erick Hitter's avatar
Erick Hitter committed
68 69
if ( ! defined( 'WP_USE_THEMES' ) ) {
	define( 'WP_USE_THEMES', true );
Vasili Pascal's avatar
Vasili Pascal committed
70
}
Benjamin Adams's avatar
Benjamin Adams committed
71

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

Erick Hitter's avatar
Erick Hitter committed
79 80 81
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
82 83
		$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
84
	} else { // Fallback to predis5.2.php
85
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
86 87
			echo "<!-- using predis as a backup -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
88

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

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
98 99
	if ( refresh_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || request_has_secret( $GLOBALS['wp_redis_cache_config']['secret_string'] ) || is_remote_page_load( $GLOBALS['wp_redis_cache_config']['current_url'], $GLOBALS['wp_redis_cache_config']['server_ip'] ) ) {
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
100 101
			echo "<!-- manual refresh was required -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
102

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

105
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
106
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
107 108
	} 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
109 110 111
			echo "<!-- displaying page without cache -->\n";
		}

112 113
		$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
114

115 116 117 118 119
		if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
			echo "<!-- POST request: . " . ( $is_post ? 'yes' : 'no' ) . "-->\n";
			echo "<!-- Logged in: . " . ( $logged_in ? 'yes' : 'no' ) . "-->\n";
		}

120
		if ( ! $is_post && ! $logged_in ) {
Erick Hitter's avatar
Erick Hitter committed
121
			ob_start();
122
			require dirname( __FILE__ ) . '/wp-blog-header.php';
123
			$html_of_page = trim( ob_get_clean() );
Erick Hitter's avatar
Erick Hitter committed
124 125 126 127
			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
128
			if ( ! is_404() && ! is_search() ) {
129 130 131 132 133 134 135 136
				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
137
				if ( $unlimited ) {
138
					$redis->set( $GLOBALS['wp_redis_cache_config']['redis_key'], $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
139
				} else {
140 141 142 143 144 145 146 147 148 149 150 151
					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
152 153 154
				}
			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
155
			require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
156
		}
157
	} elseif ( $_SERVER['REMOTE_ADDR'] != $GLOBALS['wp_redis_cache_config']['server_ip'] && true === strstr( $GLOBALS['wp_redis_cache_config']['current_url'], 'preview=true' ) ) {
158
		require dirname( __FILE__ ) . '/wp-blog-header.php';
159
	// This page is cached, lets display it
160 161 162
	} 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";
163 164
		}

165
		$GLOBALS['wp_redis_cache_config']['cache']  = true;
166

167
		$html_of_page = trim( $redis->get( $GLOBALS['wp_redis_cache_config']['redis_key'] ) );
168 169
		echo $html_of_page;

170 171
	} else {
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
172
	}
Erick Hitter's avatar
Erick Hitter committed
173
} catch ( Exception $e ) {
174
	require dirname( __FILE__ ) . '/wp-blog-header.php';
Benjamin Adams's avatar
Benjamin Adams committed
175 176
}

Hendrik Klemp's avatar
Hendrik Klemp committed
177
$end  = microtime();
Erick Hitter's avatar
Erick Hitter committed
178
$time = @get_micro_time( $end ) - @get_micro_time( $start );
179
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
180
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
181
	echo "<!-- Site was cached  = " . $GLOBALS['wp_redis_cache_config']['cache'] . " -->\n";
182 183
	if ( isset( $GLOBALS['wp_redis_cache_config']['cache_duration'] ) ) {
		echo "<!-- wp-redis-cache-seconds  = " . $GLOBALS['wp_redis_cache_config']['cache_duration'] . " -->\n";
Erick Hitter's avatar
Erick Hitter committed
184
	}
185
	echo "<!-- wp-redis-cache-ip  = " . $GLOBALS['wp_redis_cache_config']['server_ip'] . "-->\n";
186 187
	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
188
	}
189
	echo "<!-- wp-redis-cache-debug  = " . $GLOBALS['wp_redis_cache_config']['debug'] . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
190
}