index-wp-redis.php 6.72 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 19 20 21 22 23 24 25
/**
 * 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',
);

// Do not edit these values!
$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'] );

/**
 * DO NOT EDIT BELOW THIS LINE!
 */
Benjamin Adams's avatar
Benjamin Adams committed
26

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

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

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

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

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

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

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

64
handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
65

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

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

Erick Hitter's avatar
Erick Hitter committed
77 78 79
		$redis = new Redis();

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

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

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
96 97
	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
98 99
			echo "<!-- manual refresh was required -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
100

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

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

105 106 107 108
		// $unlimited           = get_option( 'wp-redis-cache-debug',   false );
		$unlimited           = false;
		// $seconds_cache_redis = get_option( 'wp-redis-cache-seconds', 43200 );
		$seconds_cache_redis = 300;
Erick Hitter's avatar
Erick Hitter committed
109
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
110 111
	} 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
112 113 114
			echo "<!-- displaying page without cache -->\n";
		}

115 116
		$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
117

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

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

Erick Hitter's avatar
Erick Hitter committed
129
			if ( ! is_numeric( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
130 131 132 133 134
				$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
135
			if ( ! is_404() && ! is_search() ) {
Erick Hitter's avatar
Erick Hitter committed
136
				if ( $unlimited ) {
137
					$redis->set( $GLOBALS['wp_redis_cache_config']['redis_key'], $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
138
				} else {
139
					$redis->setex( $GLOBALS['wp_redis_cache_config']['redis_key'], $seconds_cache_redis, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
140 141 142
				}
			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
143
			require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
144
		}
145
	} elseif ( $_SERVER['REMOTE_ADDR'] != $GLOBALS['wp_redis_cache_config']['server_ip'] && true === strstr( $GLOBALS['wp_redis_cache_config']['current_url'], 'preview=true' ) ) {
146
		require dirname( __FILE__ ) . '/wp-blog-header.php';
147
	// This page is cached, lets display it
148 149 150
	} 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";
151 152
		}

153
		$GLOBALS['wp_redis_cache_config']['cache']  = true;
154

155
		$html_of_page = trim( $redis->get( $GLOBALS['wp_redis_cache_config']['redis_key'] ) );
156 157
		echo $html_of_page;

158 159
	} else {
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
160
	}
Erick Hitter's avatar
Erick Hitter committed
161
} catch ( Exception $e ) {
162
	require dirname( __FILE__ ) . '/wp-blog-header.php';
Benjamin Adams's avatar
Benjamin Adams committed
163 164
}

Hendrik Klemp's avatar
Hendrik Klemp committed
165
$end  = microtime();
Erick Hitter's avatar
Erick Hitter committed
166
$time = @get_micro_time( $end ) - @get_micro_time( $start );
167
if ( $GLOBALS['wp_redis_cache_config']['debug'] ) {
Erick Hitter's avatar
Erick Hitter committed
168
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
169
	echo "<!-- Site was cached  = " . $GLOBALS['wp_redis_cache_config']['cache'] . " -->\n";
Erick Hitter's avatar
Erick Hitter committed
170
	if ( isset( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
171 172
		echo "<!-- wp-redis-cache-seconds  = " . $seconds_cache_redis . " -->\n";
	}
173
	echo "<!-- wp-redis-cache-ip  = " . $GLOBALS['wp_redis_cache_config']['server_ip'] . "-->\n";
Erick Hitter's avatar
Erick Hitter committed
174
	if ( isset( $unlimited ) ) {
Erick Hitter's avatar
Erick Hitter committed
175 176
		echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n";
	}
177
	echo "<!-- wp-redis-cache-debug  = " . $GLOBALS['wp_redis_cache_config']['debug'] . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
178
}