clear_cache() and content_cache
| From: | Nichlas Löfdahl | Date: | Wed, 22 Jun 2005 18:30:50 +0000 |
| Subject: | clear_cache() and content_cache | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-2727@lists.php.net to get a copy of this message | ||
Greetings!
There's a small bug with caching in smarty.
If you do the following:
1. Checks the cache with $smarty->is_cached($template)
2. You find out that the cache should be cleared because the timestamp of the
cachefile is older than some recently changed data.
3 Clears cache with $smarty->clear_cache($template);
4. Assign the new data and regenerate cache with
$smarty->display($template).
5. Smarty shows the old, cached template instead. <--- BUG
Right now smarty temporarily stores cache-content in a static variable
inside smarty_core_read_cache_file() and it is not cleared when you call
$smarty->clear_cache.
I've attached a patch which transforms the static variable in
smarty_core_read_cache_file() to a memberfunction of the Smarty-class
instead. This way clear_cache can correctly reset the state of the cache-content.
/Nichlas
Index: Smarty.class.php =================================================================== RCS file: /repository/smarty/libs/Smarty.class.php,v retrieving revision 1.515 diff -u -r1.515 Smarty.class.php --- Smarty.class.php 31 Mar 2005 14:57:18 -0000 1.515 +++ Smarty.class.php 22 Jun 2005 18:28:37 -0000 @@ -502,6 +502,13 @@ var $_cache_info = array(); /** + * contains recently fetched cache-content + * + * @var array + */ + var $_content_cache = array(); + + /** * default file permissions * * @var integer @@ -958,6 +965,8 @@ return smarty_core_rm_auto($_params, $this); } + $hash = $tpl_file.','.$cache_id.','.$compile_id; + if(isset($smarty->_content_cache[$hash])) unset($smarty->_content_cache[$hash]); } @@ -1112,7 +1121,7 @@ function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) { static $_cache_info = array(); - + $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting) ? $this->error_reporting : error_reporting() & ~E_NOTICE); Index: core.read_cache_file.php =================================================================== RCS file: /repository/smarty/libs/internals/core.read_cache_file.php,v retrieving revision 1.2 diff -u -r1.2 core.read_cache_file.php --- core.read_cache_file.php 1 Feb 2005 10:19:08 -0000 1.2 +++ core.read_cache_file.php 22 Jun 2005 18:26:15 -0000 @@ -20,15 +20,16 @@ function smarty_core_read_cache_file(&$params, &$smarty) { - static $content_cache = array(); - if ($smarty->force_compile) { // force compile enabled, always regenerate return false; } - if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) { - list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']]; + $hash = $params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']; + + if (isset($smart->_content_cache[$hash])) { + + list($params['results'], $smarty->_cache_info) = $smarty->_content_cache[$hash]; return true; } @@ -36,6 +37,7 @@ // use cache_handler function call_user_func_array($smarty->cache_handler_func, array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null)); + } else { // use local cache file $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']); @@ -100,7 +102,7 @@ return false; } } - $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info); + $smarty->_content_cache[$hash] = array($params['results'], $_cache_info); $smarty->_cache_info = $_cache_info; return true;
Index: Smarty.class.php =================================================================== RCS file: /repository/smarty/libs/Smarty.class.php,v retrieving revision 1.515 diff -u -r1.515 Smarty.class.php --- Smarty.class.php 31 Mar 2005 14:57:18 -0000 1.515 +++ Smarty.class.php 22 Jun 2005 18:28:37 -0000 @@ -502,6 +502,13 @@ var $_cache_info = array(); /** + * contains recently fetched cache-content + * + * @var array + */ + var $_content_cache = array(); + + /** * default file permissions * * @var integer @@ -958,6 +965,8 @@ return smarty_core_rm_auto($_params, $this); } + $hash = $tpl_file.','.$cache_id.','.$compile_id; + if(isset($smarty->_content_cache[$hash])) unset($smarty->_content_cache[$hash]); } @@ -1112,7 +1121,7 @@ function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) { static $_cache_info = array(); - + $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting) ? $this->error_reporting : error_reporting() & ~E_NOTICE); Index: core.read_cache_file.php =================================================================== RCS file: /repository/smarty/libs/internals/core.read_cache_file.php,v retrieving revision 1.2 diff -u -r1.2 core.read_cache_file.php --- core.read_cache_file.php 1 Feb 2005 10:19:08 -0000 1.2 +++ core.read_cache_file.php 22 Jun 2005 18:26:15 -0000 @@ -20,15 +20,16 @@ function smarty_core_read_cache_file(&$params, &$smarty) { - static $content_cache = array(); - if ($smarty->force_compile) { // force compile enabled, always regenerate return false; } - if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) { - list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']]; + $hash = $params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']; + + if (isset($smart->_content_cache[$hash])) { + + list($params['results'], $smarty->_cache_info) = $smarty->_content_cache[$hash]; return true; } @@ -36,6 +37,7 @@ // use cache_handler function call_user_func_array($smarty->cache_handler_func, array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null)); + } else { // use local cache file $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']); @@ -100,7 +102,7 @@ return false; } } - $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info); + $smarty->_content_cache[$hash] = array($params['results'], $_cache_info); $smarty->_cache_info = $_cache_info; return true;