Created
February 2, 2012 19:33
-
-
Save tistaharahap/1725281 to your computer and use it in GitHub Desktop.
Hacking CodeIgniter for Persistent Timestamped Cache with Memcache
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 | |
$ch_cache = empty($_COOKIE['URB-CC']) || (!empty($_COOKIE['URB-CC']) && $_COOKIE['URB-CC'] !== 'SUPERtWAKU'); | |
if($ch_cache) | |
setcookie('URB-CC', 'mtLJQdHqpef4nlwhA7hv', $_SERVER['REQUEST_TIME'] + 60*60*24*7, '/', '.urbanesia.com'); | |
// Check for cached pages | |
function is_logged_in() { | |
$check1 = !empty($_COOKIE['URB-CC']) && $_COOKIE['URB-CC'] === 'SUPERtWAKU'; | |
return $check1; | |
} | |
if(!is_logged_in() && class_exists('Memcache')) { | |
$_start = microtime(TRUE); | |
$url = $_SERVER['HTTP_HOST']; | |
$url .= $_SERVER['REQUEST_URI']; | |
$tmp = explode('/',$url); | |
$blacklist = array( | |
'img', | |
'auth', | |
'ajax' | |
); | |
if(!empty($tmp[1]) && in_array($tmp[1], $blacklist)) | |
return; | |
$hs_host = '127.0.0.1'; | |
$hs_port = 11211; | |
try { | |
$hs = new Memcache; | |
$hs->addServer('127.0.0.1', '11211', 1); | |
} catch(Exception $e) { | |
$hs = NULL; | |
} | |
$ns = "outputcachememc#".$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
$q = $hs->get($ns); | |
$check1 = !empty($q); | |
if($check1) { | |
$q = _defalsify($q); | |
$q .= '<!-- SERVED FROM HSOCK CACHE -->'; | |
$_end = microtime(TRUE); | |
$ts = round($_end - $_start, 6); | |
$q = str_replace('{elapsed_time_cc}', $ts, $q); | |
header('Content-Type: text/html'); | |
echo $q; exit; | |
} else { | |
unset($hs); | |
unset($url); | |
unset($q); | |
unset($hs_host); | |
unset($hs_port); | |
} | |
} | |
function _defalsify($value = '') { | |
if($value == '<-FALSE_ARRAY->') { | |
return array(); | |
} else if($value == '<-FALSE_OBJECT->') { | |
return new stdclass; | |
} else if($value == '<-FALSE_BOOLEAN->') { | |
return false; | |
} else if($value == '<-FALSE_NULL->') { | |
return null; | |
} else if($value == '<-FALSE_INTEGER->') { | |
return 0; | |
} else { | |
return $value; | |
} | |
} |
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 | |
// Check for cached pages | |
require_once '_cache.php'; | |
/* | |
|--------------------------------------------------------------- | |
| PHP ERROR REPORTING LEVEL | |
|--------------------------------------------------------------- | |
| | |
| By default CI runs with error reporting set to ALL. For security | |
| reasons you are encouraged to change this when your site goes live. | |
| For more info visit: http://www.php.net/error_reporting | |
| | |
*/ | |
error_reporting(E_ALL); | |
/* | |
|--------------------------------------------------------------- | |
| SYSTEM FOLDER NAME | |
|--------------------------------------------------------------- | |
| | |
| This variable must contain the name of your "system" folder. | |
| Include the path if the folder is not in the same directory | |
| as this file. | |
| | |
| NO TRAILING SLASH! | |
| | |
*/ | |
$system_folder = "../system"; | |
/* | |
|--------------------------------------------------------------- | |
| APPLICATION FOLDER NAME | |
|--------------------------------------------------------------- | |
| | |
| If you want this front controller to use a different "application" | |
| folder then the default one you can set its name here. The folder | |
| can also be renamed or relocated anywhere on your server. | |
| For more info please see the user guide: | |
| http://codeigniter.com/user_guide/general/managing_apps.html | |
| | |
| | |
| NO TRAILING SLASH! | |
| | |
*/ | |
$application_folder = "application"; | |
/* | |
|=============================================================== | |
| END OF USER CONFIGURABLE SETTINGS | |
|=============================================================== | |
*/ | |
/* | |
|--------------------------------------------------------------- | |
| SET THE SERVER PATH | |
|--------------------------------------------------------------- | |
| | |
| Let's attempt to determine the full-server path to the "system" | |
| folder in order to reduce the possibility of path problems. | |
| Note: We only attempt this if the user hasn't specified a | |
| full server path. | |
| | |
*/ | |
if (strpos($system_folder, '/') === FALSE) | |
{ | |
if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE) | |
{ | |
$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder; | |
} | |
} | |
else | |
{ | |
// Swap directory separators to Unix style for consistency | |
$system_folder = str_replace("\\", "/", $system_folder); | |
} | |
/* | |
|--------------------------------------------------------------- | |
| DEFINE APPLICATION CONSTANTS | |
|--------------------------------------------------------------- | |
| | |
| EXT - The file extension. Typically ".php" | |
| SELF - The name of THIS file (typically "index.php") | |
| FCPATH - The full server path to THIS file | |
| BASEPATH - The full server path to the "system" folder | |
| APPPATH - The full server path to the "application" folder | |
| | |
*/ | |
define('EXT', '.php'); | |
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); | |
define('FCPATH', str_replace(SELF, '', __FILE__)); | |
define('BASEPATH', $system_folder.'/'); | |
if (is_dir($application_folder)) | |
{ | |
define('APPPATH', $application_folder.'/'); | |
} | |
else | |
{ | |
if ($application_folder == '') | |
{ | |
$application_folder = 'application'; | |
} | |
define('APPPATH', BASEPATH.$application_folder.'/'); | |
} | |
/* | |
|--------------------------------------------------------------- | |
| LOAD THE FRONT CONTROLLER | |
|--------------------------------------------------------------- | |
| | |
| And away we go... | |
| | |
*/ | |
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT; | |
/* End of file index.php */ | |
/* Location: ./index.php */ |
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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class MY_Output extends CI_Output { | |
function _display($output = '') { | |
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end'); | |
$output = str_replace('{elapsed_time}', $elapsed, $output); | |
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; | |
$output = str_replace('{memory_usage}', $memory, $output); | |
// Grab the super object. We'll need it in a moment... | |
$CI =& get_instance(); | |
// TRAP OUTPUT | |
if(!empty($output) && ! $CI->access->logged_in() && class_exists('Memcache')) { | |
$output .= '<!-- DYNAMICALLY GENERATED -->'; | |
$ns = "outputcachememc#".$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
$m = new Memcache; | |
$m->addServer('127.0.0.1', '11211', 1); | |
$m->set($ns, $output, MEMCACHE_COMPRESSED, 60*5); | |
$output = str_replace('{elapsed_time_cc}', $elapsed, $output); | |
} | |
$output = str_replace('{elapsed_time_cc}', $elapsed, $output); | |
if (method_exists($CI, '_output')) { | |
$CI->_output($output); | |
} else { | |
echo $output; | |
} | |
log_message('debug', "Final output sent to browser"); | |
log_message('debug', "Total execution time: ".$elapsed); | |
} | |
} | |
/* End of file Output.php */ | |
/* Location: ./system/libraries/Output.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment