Skip to content

Commit

Permalink
⭐ new method for page caching
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Sep 27, 2015
1 parent 6aabe7b commit 969f1e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
20 changes: 20 additions & 0 deletions app/helpers/mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ function __construct( $controller_path='controllers/', $web_folder=WEB_FOLDER, $
// generic redirection for secure connections (assuming that ssl is on port 443)
if( defined('SSL') && SSL && $_SERVER['SERVER_PORT'] != "443" ) header('Location: '. url( request_uri() ) );

// html cache on only in production
$cached = ( DEBUG ) ? false : $this->_pageCache();
if( !empty($cached) ){
echo $cached;
// exit now
exit;
//return;
}

// add the config in the data object
$this->data['config'] = $GLOBALS['config'];
// add admin flag
Expand Down Expand Up @@ -607,6 +616,17 @@ function cors() {
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");

}

// Internal
function _pageCache(){
// compile md5 from: session id + request data + request uri
$key = md5( session_id() . json_encode($_REQUEST) . $_SERVER['REQUEST_URI'] );
// if a file with that id exists and it's relatively new, use it...
$id = "html/". $_SERVER['HTTP_HOST'] ."_". $key;
$cache = Template::getCache( $id );
// return cache if any...
return $cache;
}
}

//===============================================================
Expand Down
17 changes: 9 additions & 8 deletions app/helpers/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,22 @@ function process( $html ){

// Helpers
function getHash( $prefix="", $vars=array() ){
// OLD
// the hash is an expression of the variables compiled to render the template
// note that constantly updated values (like timestamps) should be avoided to allow the hash to be reproduced...
$string = serialize( $vars );
// ALTERNATE method
//$string = serialize( $vars );

// NEW method
// the hash is a combination of :
// - the request url
// - the request parameters
// - the session id
// - the user id (if available)
//$string = $_SERVER['REQUEST_URI'];
//$string .= serialize( $_REQUEST );
//$string .= session_id();
//if( isset($_SESSION['user']['id']) ) $string .= $_SESSION['user']['id'];
//
// use serialize( $_REQUEST ) instead?
$key = session_id() . json_encode($_REQUEST) . $_SERVER['REQUEST_URI'];
// generate a hash form the string
return $prefix . hash("md5", $string);
return $prefix . hash("md5", $key);

}
static function getCache($file ){
$cache = new Minify_Cache_File();
Expand Down

0 comments on commit 969f1e2

Please sign in to comment.