- rework clear cache methods

This commit is contained in:
Uwe Tews
2015-08-03 05:40:34 +02:00
parent 147ba329b5
commit f2585a035c
7 changed files with 47 additions and 83 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
*/

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
}
/**

View File

@@ -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;
}
}