- move caching to Smarty::_cache

This commit is contained in:
uwetews
2015-08-23 01:38:42 +02:00
parent 6a26393099
commit 87985d1243
11 changed files with 43 additions and 69 deletions

View File

@@ -8,6 +8,7 @@
- optimize nocache hash processing - optimize nocache hash processing
- remove not really needed properties - remove not really needed properties
- optimize rendering - optimize rendering
- move caching to Smarty::_cache
06.08.2015 06.08.2015
- avoid possible circular object references caused by parser/lexer objects - avoid possible circular object references caused by parser/lexer objects

View File

@@ -570,20 +570,6 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public $default_config_type = 'file'; public $default_config_type = 'file';
/**
* cached template objects
*
* @var array
*/
public $source_objects = array();
/**
* cached template objects
*
* @var array
*/
public $template_objects = array();
/** /**
* enable resource caching * enable resource caching
* *
@@ -633,13 +619,6 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public $registered_resources = array(); public $registered_resources = array();
/**
* resource handler cache
*
* @var array
*/
public $_resource_handlers = array();
/** /**
* registered cache resources * registered cache resources
* *
@@ -647,13 +626,6 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public $registered_cache_resources = array(); public $registered_cache_resources = array();
/**
* cache resource handler cache
*
* @var array
*/
public $_cacheresource_handlers = array();
/** /**
* autoload filter * autoload filter
* *
@@ -766,11 +738,11 @@ class Smarty extends Smarty_Internal_TemplateBase
public function templateExists($resource_name) public function templateExists($resource_name)
{ {
// create template object // create template object
$save = $this->template_objects; $save = $this->_cache['template_objects'];
$tpl = new $this->template_class($resource_name, $this); $tpl = new $this->template_class($resource_name, $this);
// check if it does exists // check if it does exists
$result = $tpl->source->exists; $result = $tpl->source->exists;
$this->template_objects = $save; $this->_cache['template_objects'] = $save;
return $result; return $result;
} }

View File

@@ -185,14 +185,14 @@ abstract class Smarty_CacheResource
} }
// try smarty's cache // try smarty's cache
if (isset($smarty->_cacheresource_handlers[$type])) { if (isset($smarty->_cache['cacheresource_handlers'][$type])) {
return $smarty->_cacheresource_handlers[$type]; return $smarty->_cache['cacheresource_handlers'][$type];
} }
// try registered resource // try registered resource
if (isset($smarty->registered_cache_resources[$type])) { if (isset($smarty->registered_cache_resources[$type])) {
// do not cache these instances as they may vary from instance to instance // do not cache these instances as they may vary from instance to instance
return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type]; return $smarty->_cache['cacheresource_handlers'][$type] = $smarty->registered_cache_resources[$type];
} }
// try sysplugins dir // try sysplugins dir
if (isset(self::$sysplugins[$type])) { if (isset(self::$sysplugins[$type])) {
@@ -200,12 +200,12 @@ abstract class Smarty_CacheResource
if (!class_exists($cache_resource_class, false)) { if (!class_exists($cache_resource_class, false)) {
require SMARTY_SYSPLUGINS_DIR . self::$sysplugins[$type]; require SMARTY_SYSPLUGINS_DIR . self::$sysplugins[$type];
} }
return $smarty->_cacheresource_handlers[$type] = new $cache_resource_class(); return $smarty->_cache['cacheresource_handlers'][$type] = new $cache_resource_class();
} }
// try plugins dir // try plugins dir
$cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
if ($smarty->loadPlugin($cache_resource_class)) { if ($smarty->loadPlugin($cache_resource_class)) {
return $smarty->_cacheresource_handlers[$type] = new $cache_resource_class(); return $smarty->_cache['cacheresource_handlers'][$type] = new $cache_resource_class();
} }
// give up // give up
throw new SmartyException("Unable to load cache resource '{$type}'"); throw new SmartyException("Unable to load cache resource '{$type}'");
@@ -218,7 +218,7 @@ abstract class Smarty_CacheResource
*/ */
public static function invalidLoadedCache(Smarty $smarty) public static function invalidLoadedCache(Smarty $smarty)
{ {
foreach ($smarty->template_objects as $tpl) { foreach ($smarty->_cache['template_objects'] as $tpl) {
if (isset($tpl->cached)) { if (isset($tpl->cached)) {
$tpl->cached->valid = false; $tpl->cached->valid = false;
$tpl->cached->processed = false; $tpl->cached->processed = false;

View File

@@ -220,9 +220,9 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
return 0; return 0;
} }
// remove from template cache // remove from template cache
foreach ($smarty->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 == $tpl->source->uid) {
unset($smarty->template_objects[$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
} }

View File

@@ -165,9 +165,9 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$this->invalidate(null); $this->invalidate(null);
} }
// remove from template cache // remove from template cache
foreach ($smarty->template_objects as $key => $tpl) { foreach ($smarty->_cache['template_objects'] as $key => $tpl) {
if (isset($tpl->cached)) { if (isset($tpl->cached)) {
unset($smarty->template_objects[$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
return - 1; return - 1;
@@ -196,9 +196,9 @@ 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
foreach ($smarty->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->template_objects[$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
return - 1; return - 1;
@@ -217,7 +217,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 = Smarty_Template_Source::load(null, $smarty, $resource_name); $source = new Smarty_Template_Source(null, $smarty, $resource_name);
if ($source->exists) { if ($source->exists) {
return $source->uid; return $source->uid;
} }

View File

@@ -112,9 +112,9 @@ class Smarty_Internal_Extension_Clear
} }
} }
// remove from template cache // remove from template cache
foreach ($smarty->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 == $_file) {
unset($smarty->template_objects[$key]); unset($smarty->_cache['template_objects'][$key]);
} }
} }
$_count += @unlink((string) $_file) ? 1 : 0; $_count += @unlink((string) $_file) ? 1 : 0;

View File

@@ -50,9 +50,9 @@ class Smarty_Internal_Method_ClearCompiledTemplate
// remove from compileds cache // remove from compileds cache
$tpl->source->compileds = array(); $tpl->source->compileds = array();
// remove from template cache // remove from template cache
$_templateId = $tpl->getTemplateId($resource_name); $_templateId = $tpl->smarty->_getTemplateId($resource_name);
if (isset($smarty->template_objects[$_templateId])) { if (isset($smarty->_cache['template_objects'][$_templateId])) {
unset($smarty->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);
@@ -116,8 +116,8 @@ class Smarty_Internal_Method_ClearCompiledTemplate
} }
} }
// clear compiled cache // clear compiled cache
if (!isset($resource_name)) { if (!isset($resource_name) && isset($smarty->_cache['source_objects'])) {
foreach ($smarty->source_objects as $source) { foreach ($smarty->_cache['source_objects'] as $source) {
$source->compileds = array(); $source->compileds = array();
} }
} }

View File

@@ -98,7 +98,7 @@ class Smarty_Internal_Method_CompileAllTemplates
} }
// free memory // free memory
unset($_tpl); unset($_tpl);
$_smarty->template_objects = array(); $_smarty->_cache['template_objects'] = array();
if ($max_errors !== null && $_error_count == $max_errors) { if ($max_errors !== null && $_error_count == $max_errors) {
echo "\n<br><br>too many errors\n"; echo "\n<br><br>too many errors\n";
exit(); exit();

View File

@@ -205,11 +205,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') { if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') {
$this->smarty->_debug->debugUrl($this); $this->smarty->_debug->debugUrl($this);
} }
if ($this->source->uncompiled) { if ($this->source->handler->uncompiled) {
$this->source->render($this); $this->source->render($this);
} else { } else {
// disable caching for evaluated code // disable caching for evaluated code
if ($this->source->recompiled) { if ($this->source->handler->recompiled) {
$this->caching = false; $this->caching = false;
} }
// read from cache or render // read from cache or render
@@ -491,11 +491,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/ */
public function cacheTpl($cache_tpl_obj) public function cacheTpl($cache_tpl_obj)
{ {
if (!$this->source->handler->recompiled && (isset($this->smarty->template_objects[$this->parent->templateId]) || if (!$this->source->handler->recompiled && (isset($this->smarty->_cache['template_objects'][$this->parent->templateId]) ||
($cache_tpl_obj && $this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) || ($cache_tpl_obj && $this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
$this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) $this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) { ) {
$this->smarty->->template_objects[$tpl->templateId] = $this; $this->smarty->_cache['template_objects'][$this->templateId] = $this;
} }
} }

View File

@@ -154,7 +154,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->smarty->compile_check = $compileCheck; $_template->smarty->compile_check = $compileCheck;
} }
} }
if (!$_template->source->isConfig && !isset($_template->smarty->template_objects[$_template->templateId]) && if (!isset($_template->smarty->_cache['template_objects'][$_template->templateId]) &&
$_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC && $_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC &&
$_template->parent instanceof Smarty_Internal_Template && isset($_template->parent->compiled) $_template->parent instanceof Smarty_Internal_Template && isset($_template->parent->compiled)
) { ) {
@@ -163,7 +163,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$count : $count; $count : $count;
} }
if (!in_array($_template->source->type, array('eval', 'string')) && $_template->compiled->includes[$_template->source->type . ':' . $_template->source->name] > 1) { if (!in_array($_template->source->type, array('eval', 'string')) && $_template->compiled->includes[$_template->source->type . ':' . $_template->source->name] > 1) {
$_template->smarty->template_objects[$_template->templateId] = $_template; $_template->smarty->_cache['template_objects'][$_template->templateId] = $_template;
} }
} }
$this->processed = true; $this->processed = true;

View File

@@ -98,14 +98,14 @@ class Smarty_Template_Source
/** /**
* The Components an extended template is made of * The Components an extended template is made of
* *
* @var array * @var \Smarty_Resource
*/ */
public $components = null; public $components = null;
/** /**
* Resource Handler * Resource Handler
* *
* @var Smarty_Resource * @var \Smarty_Resource|\Smarty_Resource_Uncompiled|\Smarty_Resource_Recompiled
*/ */
public $handler = null; public $handler = null;
@@ -200,39 +200,40 @@ class Smarty_Template_Source
} }
// parse resource_name, load resource handler, identify unique resource name // parse resource_name, load resource handler, identify unique resource name
list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $smarty->default_resource_type); list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $smarty->default_resource_type);
$resource = Smarty_Resource::load($smarty, $type);
$handler = Smarty_Resource::load($smarty, $type);
// if resource is not recompiling and resource name is not dotted we can check the source cache // if resource is not recompiling and resource name is not dotted we can check the source cache
if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$resource->recompiled && if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$handler->recompiled &&
!(isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/')) !(isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'))
) { ) {
$unique_resource = $resource->buildUniqueResourceName($smarty, $name); $unique_resource = $handler->buildUniqueResourceName($smarty, $name);
if (isset($smarty->source_objects[$unique_resource])) { if (isset($smarty->_cache['source_objects'][$unique_resource])) {
return $smarty->source_objects[$unique_resource]; return $smarty->_cache['source_objects'][$unique_resource];
} }
} else { } else {
$unique_resource = null; $unique_resource = null;
} }
// create new source object // create new source object
$source = new Smarty_Template_Source($resource, $smarty, $template_resource, $type, $name); $source = new Smarty_Template_Source($handler, $smarty, $template_resource, $type, $name);
$resource->populate($source, $_template); $handler->populate($source, $_template);
if (!$source->exists && isset($_template->smarty->default_template_handler_func)) { if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source); Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
} }
// on recompiling resources we are done // on recompiling resources we are done
if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$resource->recompiled) { if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$handler->recompiled) {
// may by we have already $unique_resource // may by we have already $unique_resource
$is_relative = false; $is_relative = false;
if (!isset($unique_resource)) { if (!isset($unique_resource)) {
$is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') && $is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') &&
($type == 'file' || ($type == 'file' ||
(isset($_template->parent->source) && $_template->parent->source->type == 'extends')); (isset($_template->parent->source) && $_template->parent->source->type == 'extends'));
$unique_resource = $resource->buildUniqueResourceName($smarty, $is_relative ? $source->filepath . $unique_resource = $handler->buildUniqueResourceName($smarty, $is_relative ? $source->filepath .
$name : $name); $name : $name);
} }
$source->unique_resource = $unique_resource; $source->unique_resource = $unique_resource;
// save in runtime cache if not relative // save in runtime cache if not relative
if (!$is_relative) { if (!$is_relative) {
$smarty->source_objects[$unique_resource] = $source; $smarty->_cache['source_objects'][$unique_resource] = $source;
} }
} }
return $source; return $source;