- move template object cache into static variables

This commit is contained in:
uwetews
2016-11-23 17:12:38 +01:00
parent 04a2327c75
commit 27928ece9c
4 changed files with 49 additions and 24 deletions

View File

@@ -1,5 +1,8 @@
===== 3.1.31-dev ===== (xx.xx.xx)
19.11.2016
===== 3.1.31 ===== (23.11.2016)
23.11.2016
- move template object cache into static variables
19.11.2016
- bugfix inheritance root child templates containing nested {block}{/block} could call sub-bock content from parent
template https://github.com/smarty-php/smarty/issues/317
- change version checking

View File

@@ -108,7 +108,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.31-dev/46';
const SMARTY_VERSION = '3.1.31-dev/47';
/**
* define variable scopes
@@ -746,6 +746,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public function __construct()
{
$this->_clearTemplateCache();
parent::__construct();
if (is_callable('mb_internal_encoding')) {
mb_internal_encoding(Smarty::$_CHARSET);
@@ -1111,13 +1112,13 @@ class Smarty extends Smarty_Internal_TemplateBase
}
$_templateId = $this->_getTemplateId($template, $cache_id, $compile_id);
$tpl = null;
if ($this->caching && isset($this->_cache[ 'isCached' ][ $_templateId ])) {
$tpl = $do_clone ? clone $this->_cache[ 'isCached' ][ $_templateId ] :
$this->_cache[ 'isCached' ][ $_templateId ];
if ($this->caching && isset(Smarty_Internal_Template::$isCacheTplObj[ $_templateId ])) {
$tpl = $do_clone ? clone Smarty_Internal_Template::$isCacheTplObj[ $_templateId ] :
Smarty_Internal_Template::$isCacheTplObj[ $_templateId ];
$tpl->inheritance = null;
$tpl->tpl_vars = $tpl->config_vars = array();
} else if (!$do_clone && isset($this->_cache[ 'tplObjects' ][ $_templateId ])) {
$tpl = clone $this->_cache[ 'tplObjects' ][ $_templateId ];
} else if (!$do_clone && isset(Smarty_Internal_Template::$tplObjCache[ $_templateId ])) {
$tpl = clone Smarty_Internal_Template::$tplObjCache[ $_templateId ];
$tpl->inheritance = null;
$tpl->tpl_vars = $tpl->config_vars = array();
} else {
@@ -1248,8 +1249,8 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public function _clearTemplateCache()
{
$this->_cache[ 'isCached' ] = array();
$this->_cache[ 'tplObjects' ] = array();
Smarty_Internal_Template::$isCacheTplObj = array();
Smarty_Internal_Template::$tplObjCache = array();
}
/**

View File

@@ -102,6 +102,27 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public $endRenderCallbacks = array();
/**
* Template object cache
*
* @var Smarty_Internal_Template[]
*/
public static $tplObjCache = array();
/**
* Template object cache for Smarty::isCached() == true
*
* @var Smarty_Internal_Template[]
*/
public static $isCacheTplObj = array();
/**
* Subtemplate Info Cache
*
* @var string[]int[]
*/
public static $subTplInfo = array();
/**
* Create template data object
* Some of the global Smarty settings copied to template scope
@@ -259,9 +280,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
// recursive call ?
if (isset($tpl->templateId) ? $tpl->templateId : $tpl->_getTemplateId() != $_templateId) {
// already in template cache?
if (isset($smarty->_cache[ 'tplObjects' ][ $_templateId ])) {
if (isset(self::$tplObjCache[ $_templateId ])) {
// copy data from cached object
$cachedTpl = &$smarty->_cache[ 'tplObjects' ][ $_templateId ];
$cachedTpl = &self::$tplObjCache[ $_templateId ];
$tpl->templateId = $cachedTpl->templateId;
$tpl->template_resource = $cachedTpl->template_resource;
$tpl->cache_id = $cachedTpl->cache_id;
@@ -306,13 +327,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$tpl->cache_lifetime = $cache_lifetime;
// set template scope
$tpl->scope = $scope;
if (!isset($smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
if (!isset(self::$tplObjCache[ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
// check if template object should be cached
if ($forceTplCache || (isset($smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
$smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) ||
($tpl->_isSubTpl() && isset($smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]))
if ($forceTplCache || (isset(self::$subTplInfo[ $tpl->template_resource ]) &&
self::$subTplInfo[ $tpl->template_resource ] > 1) ||
($tpl->_isSubTpl() && isset(self::$tplObjCache[ $tpl->parent->templateId ]))
) {
$smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl;
self::$tplObjCache[ $tpl->templateId ] = $tpl;
}
}
@@ -360,10 +381,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
public function _subTemplateRegister()
{
foreach ($this->compiled->includes as $name => $count) {
if (isset($this->smarty->_cache[ 'subTplInfo' ][ $name ])) {
$this->smarty->_cache[ 'subTplInfo' ][ $name ] += $count;
if (isset(self::$subTplInfo[ $name ])) {
self::$subTplInfo[ $name ] += $count;
} else {
$this->smarty->_cache[ 'subTplInfo' ][ $name ] = $count;
self::$subTplInfo[ $name ] = $count;
}
}
}
@@ -430,7 +451,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
}
$is_valid = true;
if (!empty($properties[ 'file_dependency' ]) &&
((!$cache && $tpl->smarty->compile_check) || $tpl->smarty->compile_check == 1)
((!$cache && $tpl->smarty->compile_check) || $tpl->smarty->compile_check == 1)
) {
// check file dependencies at compiled code
foreach ($properties[ 'file_dependency' ] as $_file_to_check) {

View File

@@ -199,7 +199,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
$template->loadCached();
}
$result = $template->cached->isCached($template);
$template->smarty->_cache[ 'isCached' ][ $template->_getTemplateId() ] = $template;
Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
} else {
return false;
}
@@ -219,10 +219,10 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
$template->tpl_vars = $savedTplVars;
$template->config_vars = $savedConfigVars;
} else {
if (!$function && !isset($smarty->_cache[ 'tplObjects' ][ $template->templateId ])) {
if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
$template->parent = null;
$template->tpl_vars = $template->config_vars = array();
$smarty->_cache[ 'tplObjects' ][ $template->templateId ] = $template;
Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
}
}
}