index-wp-redis.php 5.07 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';
43 44 45
$redis_server  = '127.0.0.1';
$redis_port    = 6379;
$redis_db      = 0;
Erick Hitter's avatar
Erick Hitter committed
46 47 48
$secret_string = 'changeme';
$current_url   = get_clean_url( $secret_string );
$redis_key     = md5( $current_url );
Benjamin Adams's avatar
merged  
Benjamin Adams committed
49

50
handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
51

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

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

Erick Hitter's avatar
Erick Hitter committed
63 64 65
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
66 67
		$redis->connect( $redis_server, $redis_port );
		$redis->select( $redis_db );
Erick Hitter's avatar
Erick Hitter committed
68
	} else { // Fallback to predis5.2.php
Erick Hitter's avatar
Erick Hitter committed
69
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
70 71
			echo "<!-- using predis as a backup -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
72

Erick Hitter's avatar
Erick Hitter committed
73
		include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php' ); //we need this to use Redis inside of PHP
74 75 76 77 78
		$redis = new Predis_Client( array(
			'host'     => $redis_server,
			'port'     => $redis_port,
			'database' => $redis_db,
		) );
Erick Hitter's avatar
Erick Hitter committed
79 80 81
	}

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
82
	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
83
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
84 85
			echo "<!-- manual refresh was required -->\n";
		}
Erick Hitter's avatar
Erick Hitter committed
86 87 88

		$redis->del( $redis_key );

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

91
		$unlimited           = get_option( 'wp-redis-cache-debug',   false );
Erick Hitter's avatar
Erick Hitter committed
92 93
		$seconds_cache_redis = get_option( 'wp-redis-cache-seconds', 43200 );
	// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
94
	} elseif ( $_SERVER['REMOTE_ADDR'] != $server_ip && false === strstr( $current_url, 'preview=true' ) ) {
Erick Hitter's avatar
Erick Hitter committed
95
		if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
96 97 98
			echo "<!-- displaying page without cache -->\n";
		}

99 100
		$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
101

102
		if ( ! $is_post && ! $logged_in ) {
Erick Hitter's avatar
Erick Hitter committed
103
			ob_start();
104
			require dirname( __FILE__ ) . '/wp-blog-header.php';
105
			$html_of_page = trim( ob_get_clean() );
Erick Hitter's avatar
Erick Hitter committed
106 107
			echo $html_of_page;

Erick Hitter's avatar
Erick Hitter committed
108
			if ( ! is_numeric( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
109 110 111 112 113
				$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
114
			if ( ! is_404() && ! is_search() ) {
Erick Hitter's avatar
Erick Hitter committed
115 116
				if ( $unlimited ) {
					$redis->set( $redis_key, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
117
				} else {
Erick Hitter's avatar
Erick Hitter committed
118
					$redis->setex( $redis_key, $seconds_cache_redis, $html_of_page );
Erick Hitter's avatar
Erick Hitter committed
119 120 121
				}
			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
122
			require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
123
		}
124
	} elseif ( $_SERVER['REMOTE_ADDR'] != $server_ip && true === strstr( $current_url, 'preview=true' ) ) {
125
		require dirname( __FILE__ ) . '/wp-blog-header.php';
126 127 128 129 130 131 132 133 134 135 136
	// This page is cached, lets display it
	} elseif ( $redis->exists( $redis_key ) ) {
		if ( $debug ) {
			echo "<!-- serving page from cache: key: $redis_key -->\n";
		}

		$cache  = true;

		$html_of_page = trim( $redis->get( $redis_key ) );
		echo $html_of_page;

137 138
	} else {
		require dirname( __FILE__ ) . '/wp-blog-header.php';
Erick Hitter's avatar
Erick Hitter committed
139
	}
Erick Hitter's avatar
Erick Hitter committed
140
} catch ( Exception $e ) {
141
	require dirname( __FILE__ ) . '/wp-blog-header.php';
Benjamin Adams's avatar
Benjamin Adams committed
142 143
}

Hendrik Klemp's avatar
Hendrik Klemp committed
144
$end  = microtime();
Erick Hitter's avatar
Erick Hitter committed
145 146
$time = @get_micro_time( $end ) - @get_micro_time( $start );
if ( $debug ) {
Erick Hitter's avatar
Erick Hitter committed
147 148
	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
149
	if ( isset( $seconds_cache_redis ) ) {
Erick Hitter's avatar
Erick Hitter committed
150 151
		echo "<!-- wp-redis-cache-seconds  = " . $seconds_cache_redis . " -->\n";
	}
152
	echo "<!-- wp-redis-cache-ip  = " . $server_ip . "-->\n";
Erick Hitter's avatar
Erick Hitter committed
153
	if ( isset( $unlimited ) ) {
Erick Hitter's avatar
Erick Hitter committed
154 155 156
		echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n";
	}
	echo "<!-- wp-redis-cache-debug  = " . $debug . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
157
}