Created
March 27, 2013 08:19
-
-
Save highruned/5252614 to your computer and use it in GitHub Desktop.
LS cache WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$Phpr_InitOnly = true; | |
require_once('boot.php'); | |
$cache = Core_CacheBase::create(); | |
// change vars here | |
$debug = 1; // set to 1 if you wish to see execution time and cache actions | |
$display_stats = 1; // set to 1 if you want to display a powered by redis message with execution time, see below | |
$start = microtime(); // start timing page exec | |
// if cloudflare is enabled | |
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) | |
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; | |
// init vars | |
$domain = $_SERVER['HTTP_HOST']; | |
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
$url = str_replace('?r=y', '', $url); | |
$url = str_replace('?c=y', '', $url); | |
$dkey = md5($domain); | |
$ukey = md5($url); | |
// check if page isn't a comment submission | |
((isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0); | |
// check if logged in to wp | |
$cookie = var_export($_COOKIE, true); | |
$loggedin = preg_match("/eCommerce/", $cookie); | |
$cached = 0; | |
// check if a cache of the page exists | |
if (!$loggedin && !$submit && $cache->get($dkey . $ukey)) { | |
echo $cache->get($dkey . $ukey); | |
$cached = 1; | |
$msg = 'this is a cache'; | |
// if a comment was submitted or clear page cache request was made delete cache of page | |
} | |
else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') { | |
$cache->del($dkey . $ukey); | |
$msg = 'cache of page deleted'; | |
// delete entire cache, works only if logged in | |
} | |
else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') { | |
Phpr::$response->open( Phpr::$request->getCurrentUri(true) ); | |
if ($redis->exists($dkey)) { | |
$redis->del($dkey); | |
$msg = 'domain cache flushed'; | |
} | |
else { | |
$msg = 'no cache to flush'; | |
} | |
} | |
else if ($loggedin) { // if logged in don't cache anything | |
$Phpr_InitOnly = false; | |
require_once('boot.php'); | |
$msg = 'not cached'; | |
} | |
else { // cache the page | |
// turn on output buffering | |
ob_start(); | |
Phpr::$response->open( Phpr::$request->getCurrentUri(true) ); | |
// get contents of output buffer | |
$html = ob_get_contents(); | |
// clean output buffer | |
ob_end_clean(); | |
//echo $html; | |
// store html contents to redis cache | |
$cache->set($dkey . $ukey, $html, 5 * 60); // set to expire in 5 mins | |
$msg = 'cache is set'; | |
} | |
$end = microtime(); // get end execution time | |
// show messages if debug is enabled | |
if ($debug) { | |
echo "DEBUG "; | |
echo $msg . ': '; | |
echo t_exec($start, $end); | |
} | |
if ($cached && $display_stats) { | |
echo "<div>Page generated in<br/> ".t_exec($start, $end)." sec</div>"; | |
} | |
// time diff | |
function t_exec($start, $end) { | |
$t = (getmicrotime($end) - getmicrotime($start)); | |
return round($t,5); | |
} | |
// get time | |
function getmicrotime($t) { | |
list($usec, $sec) = explode(" ",$t); | |
return ((float)$usec + (float)$sec); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment