From 969f1e23c68a46d214aa96c49a52f45662d5f3cd Mon Sep 17 00:00:00 2001 From: Makis Tracend Date: Sun, 27 Sep 2015 09:00:33 -0700 Subject: [PATCH] :star: new method for page caching --- app/helpers/mvc.php | 20 ++++++++++++++++++++ app/helpers/template.php | 17 +++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/helpers/mvc.php b/app/helpers/mvc.php index cccfc62..008468f 100644 --- a/app/helpers/mvc.php +++ b/app/helpers/mvc.php @@ -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 @@ -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; + } } //=============================================================== diff --git a/app/helpers/template.php b/app/helpers/template.php index 7d5ad12..d162346 100644 --- a/app/helpers/template.php +++ b/app/helpers/template.php @@ -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();