From 7912d0b3d0d2064032c59ba25c2f52ab71f67e3e Mon Sep 17 00:00:00 2001 From: mohrt Date: Mon, 15 Apr 2002 17:28:50 +0000 Subject: [PATCH] update caching documentation --- NEWS | 2 ++ Smarty.class.php | 28 ++++++++++++++++++++++++---- docs/programmers.sgml | 36 ++++++++++++++++++++---------------- libs/Smarty.class.php | 28 ++++++++++++++++++++++++---- 4 files changed, 70 insertions(+), 24 deletions(-) diff --git a/NEWS b/NEWS index e3b606b5..f9249024 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ + - added caching logic for expire times per cache file + (Norbert Rocher, Monte) - fixed html_select_date when field separator is "/" (Roberto Berto, Monte) - added YYYY-MM-DD format support to html_select_date diff --git a/Smarty.class.php b/Smarty.class.php index f37e8867..df36f586 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -13,7 +13,7 @@ * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. @@ -92,7 +92,11 @@ class Smarty var $force_compile = false; // force templates to compile every time, // overrides cache settings. default false. - var $caching = false; // enable caching. true/false default false. + var $caching = 0; // enable caching. can be one of 0/1/2. + // 0 = no caching + // 1 = use class cache_lifetime value + // 2 = use cache_lifetime in cache file + // default = 0. var $cache_dir = 'cache'; // name of directory for template cache files var $cache_lifetime = 3600; // number of seconds cached content will persist. // 0 = always regenerate cache, @@ -1414,7 +1418,14 @@ function _run_insert_handler($args) { // put timestamp in cache header $this->_cache_info['timestamp'] = time(); - + if ($this->cache_lifetime > -1){ + // expiration set + $this->_cache_info['expires'] = $this->_cache_info['timestamp'] + $this->cache_lifetime; + } else { + // cache will never expire + $this->_cache_info['expires'] = -1; + } + // prepend the cache header info into cache file $results = serialize($this->_cache_info)."\n".$results; @@ -1479,9 +1490,18 @@ function _run_insert_handler($args) $this->_cache_info = unserialize($cache_header); - if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) { + if ($this->caching == 2 && isset ($this->_cache_info['expires'])){ + // caching by expiration time + if ($this->_cache_info['expires'] > -1 && (time() > $this->_cache_info['expires'])) { // cache expired, regenerate return false; + } + } elseif ($this->caching == 1) { + // caching by lifetime + if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) { + // cache expired, regenerate + return false; + } } if ($this->compile_check) { diff --git a/docs/programmers.sgml b/docs/programmers.sgml index 872682ca..001ae908 100644 --- a/docs/programmers.sgml +++ b/docs/programmers.sgml @@ -173,14 +173,17 @@ $caching - This tells Smarty whether or not to cache the output of the - templates. By default this is set to false. If your templates - generate redundant redundant content over and over again and - again repeatedly, it is advisable to turn on caching. This will - result in significant performance gains. You can also have - multiple caches for the same template. See is_cached for details. This was - added to Smarty 1.3.0. + This tells Smarty whether or not to cache the output of the templates. + By default this is set to 0, or disabled. If your templates generate + redundant redundant content, it is advisable to turn on caching. This + will result in significant performance gains. You can also have multiple + caches for the same template. A value of 1 or 2 enables caching. 1 tells + Smarty to use the current $cache_lifetime variable to determine if the + cache has expired. A value of 2 tells Smarty to use the cache_lifetime + value at the time the cache was generated. This way you can set the + cache_lifetime just before fetching the template to have granular + control over when that particular cache expires. See also is_cached. If $compile_check is enabled, the cached content will be @@ -217,14 +220,15 @@ $cache_lifetime - This is the length of time in seconds that a template cache is - valid. Once this time has expired, the cache will be - regenerated. $caching must be set to "true" for this setting to - work. You can also force the cache to expire with clear_all_cache. A setting - of -1 will force the cache to never expire. A setting of 0 will cause - the cache to always regenerate (which doesn't make much sense, you'd be - better off setting caching=false.) + This is the length of time in seconds that a template cache is valid. + Once this time has expired, the cache will be regenerated. $caching must + be set to "true" for this setting to work. You can also force the cache + to expire with clear_all_cache. A setting of -1 + will force the cache to never expire. A setting of 0 will cause the + cache to always regenerate (which doesn't make much sense, you'd be + better off setting caching=false.) If $caching = 2, then the cache + expiration time is saved along with the template. diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index f37e8867..df36f586 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -13,7 +13,7 @@ * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. @@ -92,7 +92,11 @@ class Smarty var $force_compile = false; // force templates to compile every time, // overrides cache settings. default false. - var $caching = false; // enable caching. true/false default false. + var $caching = 0; // enable caching. can be one of 0/1/2. + // 0 = no caching + // 1 = use class cache_lifetime value + // 2 = use cache_lifetime in cache file + // default = 0. var $cache_dir = 'cache'; // name of directory for template cache files var $cache_lifetime = 3600; // number of seconds cached content will persist. // 0 = always regenerate cache, @@ -1414,7 +1418,14 @@ function _run_insert_handler($args) { // put timestamp in cache header $this->_cache_info['timestamp'] = time(); - + if ($this->cache_lifetime > -1){ + // expiration set + $this->_cache_info['expires'] = $this->_cache_info['timestamp'] + $this->cache_lifetime; + } else { + // cache will never expire + $this->_cache_info['expires'] = -1; + } + // prepend the cache header info into cache file $results = serialize($this->_cache_info)."\n".$results; @@ -1479,9 +1490,18 @@ function _run_insert_handler($args) $this->_cache_info = unserialize($cache_header); - if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) { + if ($this->caching == 2 && isset ($this->_cache_info['expires'])){ + // caching by expiration time + if ($this->_cache_info['expires'] > -1 && (time() > $this->_cache_info['expires'])) { // cache expired, regenerate return false; + } + } elseif ($this->caching == 1) { + // caching by lifetime + if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) { + // cache expired, regenerate + return false; + } } if ($this->compile_check) {