diff options
author | emkael <emkael@tlen.pl> | 2018-10-18 02:39:34 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2018-10-18 02:39:42 +0200 |
commit | ab5d8d4e07bb3c8230d0285ef8902ef1979fce51 (patch) | |
tree | 0b955e585cb2fdbc7207392a5f2c97d610b6a5bc /lib/smarty/sysplugins/smarty_cacheresource_custom.php | |
parent | c055ce2ab60c6582bad3e5babcb1d00384fde78a (diff) |
Updating Smarty
Diffstat (limited to 'lib/smarty/sysplugins/smarty_cacheresource_custom.php')
-rw-r--r-- | lib/smarty/sysplugins/smarty_cacheresource_custom.php | 189 |
1 files changed, 124 insertions, 65 deletions
diff --git a/lib/smarty/sysplugins/smarty_cacheresource_custom.php b/lib/smarty/sysplugins/smarty_cacheresource_custom.php index 16fe72e..68ad112 100644 --- a/lib/smarty/sysplugins/smarty_cacheresource_custom.php +++ b/lib/smarty/sysplugins/smarty_cacheresource_custom.php @@ -2,35 +2,35 @@ /** * Smarty Internal Plugin * - * @package Smarty + * @package Smarty * @subpackage Cacher */ /** * Cache Handler API * - * @package Smarty + * @package Smarty * @subpackage Cacher - * @author Rodney Rehm + * @author Rodney Rehm */ -abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { - +abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource +{ /** * fetch cached content and its modification time from data source * - * @param string $id unique cache content identifier - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id - * @param string $content cached content - * @param integer $mtime cache modification timestamp (epoch) + * @param string $id unique cache content identifier + * @param string $name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * @param string $content cached content + * @param integer $mtime cache modification timestamp (epoch) + * * @return void */ - protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime); + abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime); /** * Fetch cached content's modification timestamp from data source - * * {@internal implementing this method is optional. * Only implement it if modification times can be accessed faster than loading the complete cached content.}} * @@ -38,11 +38,12 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { * @param string $name template name * @param string $cache_id cache id * @param string $compile_id compile id + * * @return integer|boolean timestamp (epoch) the template was modified, or false if not found */ protected function fetchTimestamp($id, $name, $cache_id, $compile_id) { - return null; + return false; } /** @@ -53,54 +54,69 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { * @param string $cache_id cache id * @param string $compile_id compile id * @param integer|null $exp_time seconds till expiration or null - * @param string $content content to cache - * @return boolean success + * @param string $content content to cache + * + * @return boolean success */ - protected abstract function save($id, $name, $cache_id, $compile_id, $exp_time, $content); + abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content); /** * Delete content from cache * - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id + * @param string|null $name template name + * @param string|null $cache_id cache id + * @param string|null $compile_id compile id * @param integer|null $exp_time seconds till expiration time in seconds or null - * @return integer number of deleted caches + * + * @return integer number of deleted caches */ - protected abstract function delete($name, $cache_id, $compile_id, $exp_time); + abstract protected function delete($name, $cache_id, $compile_id, $exp_time); /** * populate Cached Object with meta data from Resource * * @param Smarty_Template_Cached $cached cached object * @param Smarty_Internal_Template $_template template object + * * @return void */ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template) { $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null; - $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null; - - $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id); + $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null; + $path = $cached->source->uid . $_cache_id . $_compile_id; + $cached->filepath = sha1($path); + if ($_template->smarty->cache_locking) { + $cached->lock_id = sha1('lock.' . $path); + } $this->populateTimestamp($cached); } /** * populate Cached Object with timestamp and exists from Resource * - * @param Smarty_Template_Cached $source cached object + * @param Smarty_Template_Cached $cached + * * @return void */ public function populateTimestamp(Smarty_Template_Cached $cached) { - $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id); + $mtime = + $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id); if ($mtime !== null) { $cached->timestamp = $mtime; $cached->exists = !!$cached->timestamp; return; } $timestamp = null; - $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp); + $this->fetch( + $cached->filepath, + $cached->source->name, + $cached->cache_id, + $cached->compile_id, + $cached->content, + $timestamp + ); $cached->timestamp = isset($timestamp) ? $timestamp : false; $cached->exists = !!$cached->timestamp; } @@ -108,30 +124,35 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { /** * Read the cached template and process the header * - * @param Smarty_Internal_Template $_template template object - * @param Smarty_Template_Cached $cached cached object - * @return booelan true or false if the cached content does not exist + * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template + * @param Smarty_Template_Cached $cached cached object + * @param boolean $update flag if called because cache update + * + * @return boolean true or false if the cached content does not exist */ - public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null) - { + public function process( + Smarty_Internal_Template $_smarty_tpl, + Smarty_Template_Cached $cached = null, + $update = false + ) { if (!$cached) { - $cached = $_template->cached; + $cached = $_smarty_tpl->cached; } $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, + $_smarty_tpl->cached->filepath, + $_smarty_tpl->source->name, + $_smarty_tpl->cache_id, + $_smarty_tpl->compile_id, $content, $timestamp ); } if (isset($content)) { - $_smarty_tpl = $_template; - eval("?>" . $content); + eval('?>' . $content); + $cached->content = null; return true; } return false; @@ -142,7 +163,8 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { * * @param Smarty_Internal_Template $_template template object * @param string $content content to cache - * @return boolean success + * + * @return boolean success */ public function writeCachedContent(Smarty_Internal_Template $_template, $content) { @@ -151,21 +173,49 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { $_template->source->name, $_template->cache_id, $_template->compile_id, - $_template->properties['cache_lifetime'], + $_template->cache_lifetime, $content ); } /** + * Read cached template from cache + * + * @param Smarty_Internal_Template $_template template object + * + * @return string|boolean content + */ + public function readCachedContent(Smarty_Internal_Template $_template) + { + $content = $_template->cached->content ? $_template->cached->content : null; + $timestamp = null; + if ($content === null) { + $timestamp = null; + $this->fetch( + $_template->cached->filepath, + $_template->source->name, + $_template->cache_id, + $_template->compile_id, + $content, + $timestamp + ); + } + if (isset($content)) { + return $content; + } + return false; + } + + /** * Empty cache * * @param Smarty $smarty Smarty object * @param integer $exp_time expiration time (number of seconds, not timestamp) + * * @return integer number of cache files deleted */ - public function clearAll(Smarty $smarty, $exp_time=null) + public function clearAll(Smarty $smarty, $exp_time = null) { - $this->cache = array(); return $this->delete(null, null, null, $exp_time); } @@ -177,62 +227,71 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource { * @param string $cache_id cache id * @param string $compile_id compile id * @param integer $exp_time expiration time (number of seconds, not timestamp) - * @return integer number of cache files deleted + * + * @return int number of cache files deleted + * @throws \SmartyException */ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) { - $this->cache = array(); - return $this->delete($resource_name, $cache_id, $compile_id, $exp_time); + $cache_name = null; + if (isset($resource_name)) { + $source = Smarty_Template_Source::load(null, $smarty, $resource_name); + if ($source->exists) { + $cache_name = $source->name; + } else { + return 0; + } + } + return $this->delete($cache_name, $cache_id, $compile_id, $exp_time); } - + /** * Check is cache is locked for this template * - * @param Smarty $smarty Smarty object + * @param Smarty $smarty Smarty object * @param Smarty_Template_Cached $cached cached object - * @return booelan true or false if cache is locked + * + * @return boolean true or false if cache is locked */ public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) { - $id = $cached->filepath; + $id = $cached->lock_id; $name = $cached->source->name . '.lock'; - - $mtime = $this->fetchTimestamp($id, $name, null, null); + $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id); if ($mtime === null) { - $this->fetch($id, $name, null, null, $content, $mtime); + $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime); } - - return $mtime && time() - $mtime < $smarty->locking_timeout; + return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout; } /** * Lock cache for this template * - * @param Smarty $smarty Smarty object + * @param Smarty $smarty Smarty object * @param Smarty_Template_Cached $cached cached object + * + * @return bool|void */ public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) { $cached->is_locked = true; - - $id = $cached->filepath; + $id = $cached->lock_id; $name = $cached->source->name . '.lock'; - $this->save($id, $name, null, null, $smarty->locking_timeout, ''); + $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, ''); } /** * Unlock cache for this template * - * @param Smarty $smarty Smarty object + * @param Smarty $smarty Smarty object * @param Smarty_Template_Cached $cached cached object + * + * @return bool|void */ public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) { $cached->is_locked = false; - - $id = $cached->filepath; $name = $cached->source->name . '.lock'; - $this->delete($name, null, null, null); + $this->delete($name, $cached->cache_id, $cached->compile_id, null); } } -?>
\ No newline at end of file |