From f2585a035c1a5b27be10dd669d2a92b08564fac0 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Mon, 3 Aug 2015 05:40:34 +0200 Subject: [PATCH] - rework clear cache methods --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- libs/sysplugins/smarty_cacheresource.php | 6 +-- .../smarty_cacheresource_custom.php | 48 ++++------------- .../smarty_cacheresource_keyvaluestore.php | 54 +++++++++---------- .../smarty_internal_cacheresource_file.php | 2 +- .../smarty_internal_extension_clear.php | 15 +++--- 7 files changed, 47 insertions(+), 83 deletions(-) diff --git a/change_log.txt b/change_log.txt index 7b03504c..dff467d0 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.28-dev===== (xx.xx.2015) + 03.08.2015 + - rework clear cache methods + 02.08.2015 - optimization and code cleanup of {foreach} and {section} compiler - rework {capture} compiler diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 28548b11..650e92b6 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/38'; + const SMARTY_VERSION = '3.1.28-dev/39'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_cacheresource.php b/libs/sysplugins/smarty_cacheresource.php index 5918eb51..44b6e46d 100644 --- a/libs/sysplugins/smarty_cacheresource.php +++ b/libs/sysplugins/smarty_cacheresource.php @@ -20,9 +20,7 @@ abstract class Smarty_CacheResource * * @var array */ - protected static $sysplugins = array( - 'file' => 'smarty_internal_cacheresource_file.php', - ); + protected static $sysplugins = array('file' => 'smarty_internal_cacheresource_file.php',); /** * populate Cached Object with meta data from Resource @@ -48,7 +46,7 @@ abstract class Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param bool $update flag if called because cache update * * @return bool true or false if the cached content does not exist */ diff --git a/libs/sysplugins/smarty_cacheresource_custom.php b/libs/sysplugins/smarty_cacheresource_custom.php index d560fe2b..15539a90 100644 --- a/libs/sysplugins/smarty_cacheresource_custom.php +++ b/libs/sysplugins/smarty_cacheresource_custom.php @@ -119,7 +119,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param bool $update flag if called because cache update * * @return boolean true or false if the cached content does not exist */ @@ -131,14 +131,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource $content = $cached->content ? $cached->content : null; $timestamp = $cached->timestamp ? $cached->timestamp : null; if ($content === null || !$timestamp) { - $this->fetch( - $_template->cached->filepath, - $_template->source->name, - $_template->cache_id, - $_template->compile_id, - $content, - $timestamp - ); + $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp); } if (isset($content)) { /** @var Smarty_Internal_Template $_smarty_tpl @@ -163,14 +156,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource */ public function writeCachedContent(Smarty_Internal_Template $_template, $content) { - return $this->save( - $_template->cached->filepath, - $_template->source->name, - $_template->cache_id, - $_template->compile_id, - $_template->properties['cache_lifetime'], - $content - ); + return $this->save($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $_template->properties['cache_lifetime'], $content); } /** @@ -186,14 +172,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource $timestamp = null; if ($content === null) { $timestamp = null; - $this->fetch( - $_template->cached->filepath, - $_template->source->name, - $_template->cache_id, - $_template->compile_id, - $content, - $timestamp - ); + $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp); } if (isset($content)) { return $content; @@ -211,9 +190,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource */ public function clearAll(Smarty $smarty, $exp_time = null) { - $this->cache = array(); - - return $this->delete(null, null, null, $exp_time); + return $this->delete(null, null, null, $exp_time); } /** @@ -229,7 +206,6 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource */ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) { - $this->cache = array(); $cache_name = null; if (isset($resource_name)) { @@ -244,17 +220,11 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource return 0; } // remove from template cache - if ($smarty->allow_ambiguous_resources) { - $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; - } else { - $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; + foreach ($smarty->template_objects as $key => $_tpl) { + if (isset($_tpl->cached) && $_tpl->source->uid == $tpl->source->uid) { + unset($smarty->template_objects[$key]); + } } - if (isset($_templateId[150])) { - $_templateId = sha1($_templateId); - } - unset($smarty->template_objects[$_templateId]); - // template object no longer needed - unset($tpl); } return $this->delete($cache_name, $cache_id, $compile_id, $exp_time); diff --git a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php index 99df6f31..15f2277d 100644 --- a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php +++ b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php @@ -54,7 +54,8 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource */ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) { - $cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' . $this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id); + $cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' . + $this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id); $this->populateTimestamp($cached); } @@ -81,7 +82,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource * * @param Smarty_Internal_Template $_template template object * @param Smarty_Template_Cached $cached cached object - * @param bool $update flag if called because cache update + * @param bool $update flag if called because cache update * * @return boolean true or false if the cached content does not exist */ @@ -163,7 +164,12 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource if (!$this->purge()) { $this->invalidate(null); } - + // remove from template cache + foreach ($smarty->template_objects as $key => $tpl) { + if (isset($tpl->cached)) { + unset($smarty->template_objects[$key]); + } + } return - 1; } @@ -184,11 +190,17 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource */ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) { - $uid = $this->getTemplateUid($smarty, $resource_name, $cache_id, $compile_id); - $cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' . $this->sanitize($compile_id); + $uid = $this->getTemplateUid($smarty, $resource_name); + $cid = $uid . '#' . $this->sanitize($resource_name) . '#' . $this->sanitize($cache_id) . '#' . + $this->sanitize($compile_id); $this->delete(array($cid)); $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid); - + // remove from template cache + foreach ($smarty->template_objects as $key => $tpl) { + if ($tpl->source->uid == $uid && isset($tpl->cached)) { + unset($smarty->template_objects[$key]); + } + } return - 1; } @@ -197,33 +209,20 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource * * @param Smarty $smarty Smarty object * @param string $resource_name template name - * @param string $cache_id cache id - * @param string $compile_id compile id * * @return string filepath of cache file + * @throws \SmartyException + * */ - protected function getTemplateUid(Smarty $smarty, $resource_name, $cache_id, $compile_id) + protected function getTemplateUid(Smarty $smarty, $resource_name) { - $uid = ''; if (isset($resource_name)) { - $tpl = new $smarty->template_class($resource_name, $smarty); - if ($tpl->source->exists) { - $uid = $tpl->source->uid; + $source = Smarty_Template_Source::load(null, $smarty, $resource_name); + if ($source->exists) { + return $source->uid; } - - // remove from template cache - if ($smarty->allow_ambiguous_resources) { - $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; - } else { - $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; - } - if (isset($_templateId[150])) { - $_templateId = sha1($_templateId); - } - unset($smarty->template_objects[$_templateId]); } - - return $uid; + return ''; } /** @@ -235,12 +234,10 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource */ protected function sanitize($string) { - // some poeple smoke bad weed $string = trim($string, '|'); if (!$string) { return null; } - return preg_replace('#[^\w\|]+#S', '_', $string); } @@ -398,7 +395,6 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource $t[] = 'IVK#COMPILE' . $_compile; } $_name .= '#'; - // some poeple smoke bad weed $cid = trim($cache_id, '|'); if (!$cid) { return $t; diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index 528ea01c..ad0cd532 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -153,7 +153,7 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource */ public function clearAll(Smarty $smarty, $exp_time = null) { - return $this->clear($smarty, null, null, null, $exp_time); + return Smarty_Internal_Extension_Clear::clear($smarty, null, null, null, $exp_time); } /** diff --git a/libs/sysplugins/smarty_internal_extension_clear.php b/libs/sysplugins/smarty_internal_extension_clear.php index 00de781f..8e2f4af0 100644 --- a/libs/sysplugins/smarty_internal_extension_clear.php +++ b/libs/sysplugins/smarty_internal_extension_clear.php @@ -50,15 +50,6 @@ class Smarty_Internal_Extension_Clear // remove from template cache $tpl->source; // have the template registered before unset() - if ($smarty->allow_ambiguous_resources) { - $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; - } else { - $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; - } - if (isset($_templateId[150])) { - $_templateId = sha1($_templateId); - } - unset($smarty->template_objects[$_templateId]); if ($tpl->source->exists) { $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); @@ -120,6 +111,12 @@ class Smarty_Internal_Extension_Clear } } } + // remove from template cache + foreach ($smarty->template_objects as $key => $tpl) { + if (isset($tpl->cached) && $tpl->cached->filepath == $_file) { + unset($smarty->template_objects[$key]); + } + } $_count += @unlink((string) $_file) ? 1 : 0; } }