3 * Smarty Internal Plugin
16 abstract class Smarty_CacheResource
19 * resource types provided by the core
23 protected static $sysplugins = array(
24 'file' => 'smarty_internal_cacheresource_file.php',
28 * populate Cached Object with meta data from Resource
30 * @param Smarty_Template_Cached $cached cached object
31 * @param Smarty_Internal_Template $_template template object
35 abstract public function populate(Smarty_Template_Cached
$cached, Smarty_Internal_Template
$_template);
38 * populate Cached Object with timestamp and exists from Resource
40 * @param Smarty_Template_Cached $cached
44 abstract public function populateTimestamp(Smarty_Template_Cached
$cached);
47 * Read the cached template and process header
49 * @param Smarty_Internal_Template $_template template object
50 * @param Smarty_Template_Cached $cached cached object
52 * @return boolean true or false if the cached content does not exist
54 abstract public function process(Smarty_Internal_Template
$_template, Smarty_Template_Cached
$cached = null);
57 * Write the rendered template output to cache
59 * @param Smarty_Internal_Template $_template template object
60 * @param string $content content to cache
62 * @return boolean success
64 abstract public function writeCachedContent(Smarty_Internal_Template
$_template, $content);
67 * Return cached content
69 * @param Smarty_Internal_Template $_template template object
73 public function getCachedContent(Smarty_Internal_Template
$_template)
75 if ($_template->cached
->handler
->process($_template)) {
77 $_template->properties
['unifunc']($_template);
79 return ob_get_clean();
88 * @param Smarty $smarty Smarty object
89 * @param integer $exp_time expiration time (number of seconds, not timestamp)
91 * @return integer number of cache files deleted
93 abstract public function clearAll(Smarty
$smarty, $exp_time = null);
96 * Empty cache for a specific template
98 * @param Smarty $smarty Smarty object
99 * @param string $resource_name template name
100 * @param string $cache_id cache id
101 * @param string $compile_id compile id
102 * @param integer $exp_time expiration time (number of seconds, not timestamp)
104 * @return integer number of cache files deleted
106 abstract public function clear(Smarty
$smarty, $resource_name, $cache_id, $compile_id, $exp_time);
109 * @param Smarty $smarty
110 * @param Smarty_Template_Cached $cached
114 public function locked(Smarty
$smarty, Smarty_Template_Cached
$cached)
116 // theoretically locking_timeout should be checked against time_limit (max_execution_time)
117 $start = microtime(true);
119 while ($this->hasLock($smarty, $cached)) {
121 if (microtime(true) - $start > $smarty->locking_timeout
) {
122 // abort waiting for lock release
132 * Check is cache is locked for this template
134 * @param Smarty $smarty
135 * @param Smarty_Template_Cached $cached
139 public function hasLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
141 // check if lock exists
146 * Lock cache for this template
148 * @param Smarty $smarty
149 * @param Smarty_Template_Cached $cached
153 public function acquireLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
160 * Unlock cache for this template
162 * @param Smarty $smarty
163 * @param Smarty_Template_Cached $cached
167 public function releaseLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
174 * Load Cache Resource Handler
176 * @param Smarty $smarty Smarty object
177 * @param string $type name of the cache resource
179 * @throws SmartyException
180 * @return Smarty_CacheResource Cache Resource Handler
182 public static function load(Smarty
$smarty, $type = null)
185 $type = $smarty->caching_type
;
188 // try smarty's cache
189 if (isset($smarty->_cacheresource_handlers
[$type])) {
190 return $smarty->_cacheresource_handlers
[$type];
193 // try registered resource
194 if (isset($smarty->registered_cache_resources
[$type])) {
195 // do not cache these instances as they may vary from instance to instance
196 return $smarty->_cacheresource_handlers
[$type] = $smarty->registered_cache_resources
[$type];
198 // try sysplugins dir
199 if (isset(self
::$sysplugins[$type])) {
200 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
201 if (!class_exists($cache_resource_class, false)) {
202 require SMARTY_SYSPLUGINS_DIR
. self
::$sysplugins[$type];
204 return $smarty->_cacheresource_handlers
[$type] = new $cache_resource_class();
207 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
208 if ($smarty->loadPlugin($cache_resource_class)) {
209 return $smarty->_cacheresource_handlers
[$type] = new $cache_resource_class();
212 throw new SmartyException("Unable to load cache resource '{$type}'");
216 * Invalid Loaded Cache Files
218 * @param Smarty $smarty Smarty object
220 public static function invalidLoadedCache(Smarty
$smarty)
222 foreach ($smarty->template_objects
as $tpl) {
223 if (isset($tpl->cached
)) {
224 $tpl->cached
->valid
= false;
225 $tpl->cached
->processed
= false;