From 189b2af115c69ea0507c093180c44b783290f4a1 Mon Sep 17 00:00:00 2001 From: uwetews Date: Sun, 23 Aug 2015 05:27:16 +0200 Subject: [PATCH] - optimize invalidation of internal caches --- libs/sysplugins/smarty_cacheresource.php | 11 +++++----- .../smarty_cacheresource_custom.php | 18 ++++++++--------- .../smarty_cacheresource_keyvaluestore.php | 20 ++++++++++++------- .../smarty_internal_extension_clear.php | 8 +++++--- .../smarty_internal_method_clearallcache.php | 4 +++- .../smarty_internal_method_clearcache.php | 4 +++- ..._internal_method_clearcompiledtemplate.php | 12 ++++++----- 7 files changed, 45 insertions(+), 32 deletions(-) diff --git a/libs/sysplugins/smarty_cacheresource.php b/libs/sysplugins/smarty_cacheresource.php index 46891ae9..8f76a5b1 100644 --- a/libs/sysplugins/smarty_cacheresource.php +++ b/libs/sysplugins/smarty_cacheresource.php @@ -216,12 +216,13 @@ abstract class Smarty_CacheResource * * @param Smarty $smarty Smarty object */ - public static function invalidLoadedCache(Smarty $smarty) + public function invalidLoadedCache(Smarty $smarty) { - foreach ($smarty->_cache['template_objects'] as $tpl) { - if (isset($tpl->cached)) { - $tpl->cached->valid = false; - $tpl->cached->processed = false; + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $tpl) { + if (isset($tpl->cached)) { + unset ($smarty->_cache['template_objects'][$key]); + } } } } diff --git a/libs/sysplugins/smarty_cacheresource_custom.php b/libs/sysplugins/smarty_cacheresource_custom.php index a7134689..4e9606ef 100644 --- a/libs/sysplugins/smarty_cacheresource_custom.php +++ b/libs/sysplugins/smarty_cacheresource_custom.php @@ -209,20 +209,18 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource $cache_name = null; if (isset($resource_name)) { - $_save_stat = $smarty->caching; - $smarty->caching = true; - $tpl = new $smarty->template_class($resource_name, $smarty); - $smarty->caching = $_save_stat; - - if ($tpl->source->exists) { - $cache_name = $tpl->source->name; + $source = Smarty_Template_Source::load(null, $smarty, $resource_name); + if ($source->exists) { + $cache_name = $source->name; } else { return 0; } // remove from template cache - foreach ($smarty->_cache['template_objects'] as $key => $_tpl) { - if (isset($_tpl->cached) && $_tpl->source->uid == $tpl->source->uid) { - unset($smarty->_cache['template_objects'][$key]); + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $_tpl) { + if (isset($_tpl->cached) && $_tpl->source->uid == $source->uid) { + unset($smarty->_cache['template_objects'][$key]); + } } } } diff --git a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php index 428fe2dc..ee4021a1 100644 --- a/libs/sysplugins/smarty_cacheresource_keyvaluestore.php +++ b/libs/sysplugins/smarty_cacheresource_keyvaluestore.php @@ -165,9 +165,11 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource $this->invalidate(null); } // remove from template cache - foreach ($smarty->_cache['template_objects'] as $key => $tpl) { - if (isset($tpl->cached)) { - unset($smarty->_cache['template_objects'][$key]); + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $tpl) { + if (isset($tpl->cached)) { + unset($smarty->_cache['template_objects'][$key]); + } } } return - 1; @@ -196,9 +198,13 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource $this->delete(array($cid)); $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid); // remove from template cache - foreach ($smarty->_cache['template_objects'] as $key => $tpl) { - if ($tpl->source->uid == $uid && isset($tpl->cached)) { - unset($smarty->_cache['template_objects'][$key]); + if (isset($resource_name) && isset($smarty->_cache['template_objects'])) { + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $tpl) { + if ($tpl->source->uid == $uid && isset($tpl->cached)) { + unset($smarty->_cache['template_objects'][$key]); + } + } } } return - 1; @@ -217,7 +223,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource protected function getTemplateUid(Smarty $smarty, $resource_name) { if (isset($resource_name)) { - $source = new Smarty_Template_Source(null, $smarty, $resource_name); + $source = Smarty_Template_Source::load(null, $smarty, $resource_name); if ($source->exists) { return $source->uid; } diff --git a/libs/sysplugins/smarty_internal_extension_clear.php b/libs/sysplugins/smarty_internal_extension_clear.php index 26192016..0d40d487 100644 --- a/libs/sysplugins/smarty_internal_extension_clear.php +++ b/libs/sysplugins/smarty_internal_extension_clear.php @@ -112,9 +112,11 @@ class Smarty_Internal_Extension_Clear } } // remove from template cache - foreach ($smarty->_cache['template_objects'] as $key => $tpl) { - if (isset($tpl->cached) && $tpl->cached->filepath == $_file) { - unset($smarty->_cache['template_objects'][$key]); + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $tpl) { + if (isset($tpl->cached) && $tpl->cached->filepath == (string)$_file) { + unset($smarty->_cache['template_objects'][$key]); + } } } $_count += @unlink((string) $_file) ? 1 : 0; diff --git a/libs/sysplugins/smarty_internal_method_clearallcache.php b/libs/sysplugins/smarty_internal_method_clearallcache.php index 24ceb0ed..1fe37892 100644 --- a/libs/sysplugins/smarty_internal_method_clearallcache.php +++ b/libs/sysplugins/smarty_internal_method_clearallcache.php @@ -34,7 +34,9 @@ class Smarty_Internal_Method_ClearAllCache { // load cache resource and call clearAll $_cache_resource = Smarty_CacheResource::load($smarty, $type); - Smarty_CacheResource::invalidLoadedCache($smarty); + if ($smarty->caching_type != 'file') { + $_cache_resource->invalidLoadedCache($smarty); + } return $_cache_resource->clearAll($smarty, $exp_time); } diff --git a/libs/sysplugins/smarty_internal_method_clearcache.php b/libs/sysplugins/smarty_internal_method_clearcache.php index 7db0a145..a923b3ae 100644 --- a/libs/sysplugins/smarty_internal_method_clearcache.php +++ b/libs/sysplugins/smarty_internal_method_clearcache.php @@ -37,7 +37,9 @@ class Smarty_Internal_Method_ClearCache { // load cache resource and call clear $_cache_resource = Smarty_CacheResource::load($smarty, $type); - Smarty_CacheResource::invalidLoadedCache($smarty); + if ($smarty->caching_type != 'file' && !isset($template_name)) { + $_cache_resource->invalidLoadedCache($smarty); + } return $_cache_resource->clear($smarty, $template_name, $cache_id, $compile_id, $exp_time); } diff --git a/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php b/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php index ed579165..9942c982 100644 --- a/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php +++ b/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php @@ -49,11 +49,6 @@ class Smarty_Internal_Method_ClearCompiledTemplate if ($tpl->source->exists) { // remove from compileds cache $tpl->source->compileds = array(); - // remove from template cache - $_templateId = $tpl->smarty->_getTemplateId($resource_name); - if (isset($smarty->_cache['template_objects'][$_templateId])) { - unset($smarty->_cache['template_objects'][$_templateId]); - } $_resource_part_1 = basename(str_replace('^', DS, $tpl->compiled->filepath)); $_resource_part_1_length = strlen($_resource_part_1); } else { @@ -111,6 +106,13 @@ class Smarty_Internal_Method_ClearCompiledTemplate } if ($unlink && @unlink($_filepath)) { + if (isset($smarty->_cache['template_objects'])) { + foreach ($smarty->_cache['template_objects'] as $key => $tpl) { + if (isset($tpl->compiled) && $tpl->compiled->filepath == $_filepath) { + unset($smarty->_cache['template_objects'][$key]); + } + } + } $_count ++; } }