Skip to content
Snippets Groups Projects
Commit 449e7411 authored by Erick Hitter's avatar Erick Hitter
Browse files

Spaces -> Tabs

parent 4f209302
Branches
No related tags found
No related merge requests found
...@@ -4,149 +4,149 @@ ...@@ -4,149 +4,149 @@
$start = microtime(); $start = microtime();
function getMicroTime($time) { function getMicroTime($time) {
list($usec, $sec) = explode(" ", $time); list($usec, $sec) = explode(" ", $time);
return ((float) $usec + (float) $sec); return ((float) $usec + (float) $sec);
} }
function refreshHasSecret($secret) { function refreshHasSecret($secret) {
return isset($_GET['refresh']) && $_GET['refresh'] == $secret; return isset($_GET['refresh']) && $_GET['refresh'] == $secret;
} }
function requestHasSecret($secret) { function requestHasSecret($secret) {
return strpos($_SERVER['REQUEST_URI'],"refresh=${secret}")!==false; return strpos($_SERVER['REQUEST_URI'],"refresh=${secret}")!==false;
} }
function isRemotePageLoad($currentUrl, $websiteIp) { function isRemotePageLoad($currentUrl, $websiteIp) {
return (isset($_SERVER['HTTP_REFERER']) return (isset($_SERVER['HTTP_REFERER'])
&& $_SERVER['HTTP_REFERER']== $currentUrl && $_SERVER['HTTP_REFERER']== $currentUrl
&& $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != '/'
&& $_SERVER['REMOTE_ADDR'] != $websiteIp); && $_SERVER['REMOTE_ADDR'] != $websiteIp);
} }
function handleCDNRemoteAddressing() { function handleCDNRemoteAddressing() {
// so we don't confuse the cloudflare server // so we don't confuse the cloudflare server
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
} }
} }
function getCleanUrl($secret) { function getCleanUrl($secret) {
$replaceKeys = array("?refresh=${secret}","&refresh=${secret}"); $replaceKeys = array("?refresh=${secret}","&refresh=${secret}");
$url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}"; $url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
$current_url = str_replace($replaceKeys, '', $url); $current_url = str_replace($replaceKeys, '', $url);
return $current_url; return $current_url;
} }
$debug = true; $debug = true;
$cache = true; $cache = true;
$websiteIp = '127.0.0.1'; $websiteIp = '127.0.0.1';
$reddis_server = '127.0.0.1'; $reddis_server = '127.0.0.1';
$secret_string = 'changeme'; $secret_string = 'changeme';
$current_url = getCleanUrl($secret_string); $current_url = getCleanUrl($secret_string);
$redis_key = md5($current_url); $redis_key = md5($current_url);
handleCDNRemoteAddressing(); handleCDNRemoteAddressing();
if(!defined('WP_USE_THEMES')) { if(!defined('WP_USE_THEMES')) {
define('WP_USE_THEMES', true); define('WP_USE_THEMES', true);
} }
try { try {
// check if PECL Extension is available // check if PECL Extension is available
if (class_exists('Redis')) { if (class_exists('Redis')) {
if ($debug) { if ($debug) {
echo "<!-- Redis PECL module found -->\n"; echo "<!-- Redis PECL module found -->\n";
} }
$redis = new Redis(); $redis = new Redis();
// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection // Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
$redis->connect($reddis_server); $redis->connect($reddis_server);
} else { // Fallback to predis5.2.php } else { // Fallback to predis5.2.php
if ($debug) { if ($debug) {
echo "<!-- using predis as a backup -->\n"; echo "<!-- using predis as a backup -->\n";
} }
include_once("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP include_once("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
$redis = new Predis_Client(); $redis = new Predis_Client();
} }
//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment //Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
if (refreshHasSecret($secret_string) || requestHasSecret($secret_string) || isRemotePageLoad($current_url, $websiteIp)) { if (refreshHasSecret($secret_string) || requestHasSecret($secret_string) || isRemotePageLoad($current_url, $websiteIp)) {
if ($debug) { if ($debug) {
echo "<!-- manual refresh was required -->\n"; echo "<!-- manual refresh was required -->\n";
} }
$redis->del($redis_key); $redis->del($redis_key);
require('./wp-blog-header.php'); require('./wp-blog-header.php');
$unlimited = get_option('wp-redis-cache-debug',false); $unlimited = get_option('wp-redis-cache-debug',false);
$seconds_cache_redis = get_option('wp-redis-cache-seconds',43200); $seconds_cache_redis = get_option('wp-redis-cache-seconds',43200);
// This page is cached, lets display it // This page is cached, lets display it
} else if ($redis->exists($redis_key)) { } else if ($redis->exists($redis_key)) {
if ($debug) { if ($debug) {
echo "<!-- serving page from cache: key: $redis_key -->\n"; echo "<!-- serving page from cache: key: $redis_key -->\n";
} }
$cache = true; $cache = true;
$html_of_page = $redis->get($redis_key); $html_of_page = $redis->get($redis_key);
echo $html_of_page; echo $html_of_page;
// If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
} else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == false) { } else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == false) {
if ($debug) { if ($debug) {
echo "<!-- displaying page without cache -->\n"; echo "<!-- displaying page without cache -->\n";
} }
$isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0; $isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0;
$loggedIn = preg_match("/wordpress_logged_in/", var_export($_COOKIE, true)); $loggedIn = preg_match("/wordpress_logged_in/", var_export($_COOKIE, true));
if (!$isPOST && !$loggedIn) { if (!$isPOST && !$loggedIn) {
ob_start(); ob_start();
require('./wp-blog-header.php'); require('./wp-blog-header.php');
$html_of_page = ob_get_contents(); $html_of_page = ob_get_contents();
ob_end_clean(); ob_end_clean();
echo $html_of_page; echo $html_of_page;
if (!is_numeric($seconds_cache_redis)) { if (!is_numeric($seconds_cache_redis)) {
$seconds_cache_redis = 43200; $seconds_cache_redis = 43200;
} }
// When a page displays after an "HTTP 404: Not Found" error occurs, do not cache // When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
// When the search was used, do not cache // When the search was used, do not cache
if ((!is_404()) and (!is_search())) { if ((!is_404()) and (!is_search())) {
if ($unlimited) { if ($unlimited) {
$redis->set($redis_key, $html_of_page); $redis->set($redis_key, $html_of_page);
} else { } else {
$redis->setex($redis_key, $seconds_cache_redis, $html_of_page); $redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
} }
} }
} else { //either the user is logged in, or is posting a comment, show them uncached } else { //either the user is logged in, or is posting a comment, show them uncached
require('./wp-blog-header.php'); require('./wp-blog-header.php');
} }
} else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == true) { } else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == true) {
require('./wp-blog-header.php'); require('./wp-blog-header.php');
} }
// else { // This is what your server should get if no cache exists //deprecated, as the ob_start() is cleaner // else { // This is what your server should get if no cache exists //deprecated, as the ob_start() is cleaner
// require('./wp-blog-header.php'); // require('./wp-blog-header.php');
// } // }
} catch (Exception $e) { } catch (Exception $e) {
//require('./wp-blog-header.php'); //require('./wp-blog-header.php');
echo "something went wrong"; echo "something went wrong";
} }
$end = microtime(); $end = microtime();
$time = (@getMicroTime($end) - @getMicroTime($start)); $time = (@getMicroTime($end) - @getMicroTime($start));
if ($debug) { if ($debug) {
echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n"; echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
echo "<!-- Site was cached = " . $cache . " -->\n"; echo "<!-- Site was cached = " . $cache . " -->\n";
if (isset($seconds_cache_redis)) { if (isset($seconds_cache_redis)) {
echo "<!-- wp-redis-cache-seconds = " . $seconds_cache_redis . " -->\n"; echo "<!-- wp-redis-cache-seconds = " . $seconds_cache_redis . " -->\n";
} }
echo "<!-- wp-redis-cache-secret = " . $secret_string . "-->\n"; echo "<!-- wp-redis-cache-secret = " . $secret_string . "-->\n";
echo "<!-- wp-redis-cache-ip = " . $websiteIp . "-->\n"; echo "<!-- wp-redis-cache-ip = " . $websiteIp . "-->\n";
if (isset($unlimited)) { if (isset($unlimited)) {
echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n"; echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n";
} }
echo "<!-- wp-redis-cache-debug = " . $debug . "-->\n"; echo "<!-- wp-redis-cache-debug = " . $debug . "-->\n";
} }
...@@ -99,18 +99,18 @@ We do this because WordPress is no longer in charge of displaying our posts. Re ...@@ -99,18 +99,18 @@ We do this because WordPress is no longer in charge of displaying our posts. Re
/* Copyright 2013 Benjamin Adams (email : ben@dudelol.com) /* Copyright 2013 Benjamin Adams (email : ben@dudelol.com)
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation. published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
...@@ -119,37 +119,37 @@ We do this because WordPress is no longer in charge of displaying our posts. Re ...@@ -119,37 +119,37 @@ We do this because WordPress is no longer in charge of displaying our posts. Re
add_action('admin_menu', 'add_redis_interface'); add_action('admin_menu', 'add_redis_interface');
function add_redis_interface() { function add_redis_interface() {
add_options_page('WP Redis Cache', 'WP Redis Cache', '8', 'functions', 'edit_redis_options'); add_options_page('WP Redis Cache', 'WP Redis Cache', '8', 'functions', 'edit_redis_options');
} }
function edit_redis_options() { function edit_redis_options() {
?> ?>
<div class='wrap'> <div class='wrap'>
<h2>WP-Redis Options</h2> <h2>WP-Redis Options</h2>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php wp_nonce_field('update-options') ?> <?php wp_nonce_field('update-options') ?>
<p>This plugin does not work out of the box and requires additional steps.<br /> <p>This plugin does not work out of the box and requires additional steps.<br />
Please follow these install instructions: <a target='_blank' href='https://github.com/BenjaminAdams/wp-redis-cache'>https://github.com/BenjaminAdams/wp-redis-cache</a></p> Please follow these install instructions: <a target='_blank' href='https://github.com/BenjaminAdams/wp-redis-cache'>https://github.com/BenjaminAdams/wp-redis-cache</a></p>
<p>If you do not have Redis installed on your machine this will NOT work! </p> <p>If you do not have Redis installed on your machine this will NOT work! </p>
<p><strong>Seconds of Caching:</strong><br /> <p><strong>Seconds of Caching:</strong><br />
How many seconds would you like to cache? *Recommended 12 hours or 43200 seconds <br /> How many seconds would you like to cache? *Recommended 12 hours or 43200 seconds <br />
<input type="text" name="wp-redis-cache-seconds" size="45" value="<?php echo get_option('wp-redis-cache-seconds'); ?>" /></p> <input type="text" name="wp-redis-cache-seconds" size="45" value="<?php echo get_option('wp-redis-cache-seconds'); ?>" /></p>
<p><strong>Cache unlimeted:</strong><br /> <p><strong>Cache unlimeted:</strong><br />
If this options set the cache never expire. This option overiedes the setting "Seconds of Caching"<br /> If this options set the cache never expire. This option overiedes the setting "Seconds of Caching"<br />
<input type="checkbox" name="wp-redis-cache-unlimited" size="45" value="true" <?php checked('true', get_option('wp-redis-cache-unlimited')); ?>/></p> <input type="checkbox" name="wp-redis-cache-unlimited" size="45" value="true" <?php checked('true', get_option('wp-redis-cache-unlimited')); ?>/></p>
<p><input type="submit" name="Submit" value="Update Options" /></p> <p><input type="submit" name="Submit" value="Update Options" /></p>
<p><input type="button" id="WPRedisClearCache" name="WPRedisClearCache" value="Clear Cache"></p> <p><input type="button" id="WPRedisClearCache" name="WPRedisClearCache" value="Clear Cache"></p>
<input type="hidden" name="action" value="update" /> <input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="wp-redis-cache-seconds,wp-redis-cache-unlimited" /> <input type="hidden" name="page_options" value="wp-redis-cache-seconds,wp-redis-cache-unlimited" />
</form> </form>
</div> </div>
<?php <?php
} }
include('cache.php'); include('cache.php');
\ No newline at end of file
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment