- optimize invalidation of internal caches

This commit is contained in:
uwetews
2015-08-23 05:27:16 +02:00
parent 1ae11c26c0
commit 189b2af115
7 changed files with 45 additions and 32 deletions

View File

@@ -216,12 +216,13 @@ abstract class Smarty_CacheResource
* *
* @param Smarty $smarty Smarty object * @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($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
if (isset($tpl->cached)) { if (isset($tpl->cached)) {
$tpl->cached->valid = false; unset ($smarty->_cache['template_objects'][$key]);
$tpl->cached->processed = false; }
} }
} }
} }

View File

@@ -209,23 +209,21 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
$cache_name = null; $cache_name = null;
if (isset($resource_name)) { if (isset($resource_name)) {
$_save_stat = $smarty->caching; $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
$smarty->caching = true; if ($source->exists) {
$tpl = new $smarty->template_class($resource_name, $smarty); $cache_name = $source->name;
$smarty->caching = $_save_stat;
if ($tpl->source->exists) {
$cache_name = $tpl->source->name;
} else { } else {
return 0; return 0;
} }
// remove from template cache // remove from template cache
if (isset($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $_tpl) { foreach ($smarty->_cache['template_objects'] as $key => $_tpl) {
if (isset($_tpl->cached) && $_tpl->source->uid == $tpl->source->uid) { if (isset($_tpl->cached) && $_tpl->source->uid == $source->uid) {
unset($smarty->_cache['template_objects'][$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
} }
}
return $this->delete($cache_name, $cache_id, $compile_id, $exp_time); return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
} }

View File

@@ -165,11 +165,13 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$this->invalidate(null); $this->invalidate(null);
} }
// remove from template cache // remove from template cache
if (isset($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $tpl) { foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
if (isset($tpl->cached)) { if (isset($tpl->cached)) {
unset($smarty->_cache['template_objects'][$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
}
return - 1; return - 1;
} }
@@ -196,11 +198,15 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$this->delete(array($cid)); $this->delete(array($cid));
$this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid); $this->invalidate($cid, $resource_name, $cache_id, $compile_id, $uid);
// remove from template cache // remove from template cache
if (isset($resource_name) && isset($smarty->_cache['template_objects'])) {
if (isset($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $tpl) { foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
if ($tpl->source->uid == $uid && isset($tpl->cached)) { if ($tpl->source->uid == $uid && isset($tpl->cached)) {
unset($smarty->_cache['template_objects'][$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
}
}
return - 1; return - 1;
} }
@@ -217,7 +223,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
protected function getTemplateUid(Smarty $smarty, $resource_name) protected function getTemplateUid(Smarty $smarty, $resource_name)
{ {
if (isset($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) { if ($source->exists) {
return $source->uid; return $source->uid;
} }

View File

@@ -112,11 +112,13 @@ class Smarty_Internal_Extension_Clear
} }
} }
// remove from template cache // remove from template cache
if (isset($smarty->_cache['template_objects'])) {
foreach ($smarty->_cache['template_objects'] as $key => $tpl) { foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
if (isset($tpl->cached) && $tpl->cached->filepath == $_file) { if (isset($tpl->cached) && $tpl->cached->filepath == (string)$_file) {
unset($smarty->_cache['template_objects'][$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
}
$_count += @unlink((string) $_file) ? 1 : 0; $_count += @unlink((string) $_file) ? 1 : 0;
} }
} }

View File

@@ -34,7 +34,9 @@ class Smarty_Internal_Method_ClearAllCache
{ {
// load cache resource and call clearAll // load cache resource and call clearAll
$_cache_resource = Smarty_CacheResource::load($smarty, $type); $_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); return $_cache_resource->clearAll($smarty, $exp_time);
} }

View File

@@ -37,7 +37,9 @@ class Smarty_Internal_Method_ClearCache
{ {
// load cache resource and call clear // load cache resource and call clear
$_cache_resource = Smarty_CacheResource::load($smarty, $type); $_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); return $_cache_resource->clear($smarty, $template_name, $cache_id, $compile_id, $exp_time);
} }

View File

@@ -49,11 +49,6 @@ class Smarty_Internal_Method_ClearCompiledTemplate
if ($tpl->source->exists) { if ($tpl->source->exists) {
// remove from compileds cache // remove from compileds cache
$tpl->source->compileds = array(); $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 = basename(str_replace('^', DS, $tpl->compiled->filepath));
$_resource_part_1_length = strlen($_resource_part_1); $_resource_part_1_length = strlen($_resource_part_1);
} else { } else {
@@ -111,6 +106,13 @@ class Smarty_Internal_Method_ClearCompiledTemplate
} }
if ($unlink && @unlink($_filepath)) { 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 ++; $_count ++;
} }
} }