3 * Smarty Internal Plugin
16 abstract class Smarty_CacheResource
19 * cache for Smarty_CacheResource instances
23 public static $resources = array();
26 * resource types provided by the core
30 protected static $sysplugins = array(
35 * populate Cached Object with meta data from Resource
37 * @param Smarty_Template_Cached $cached cached object
38 * @param Smarty_Internal_Template $_template template object
42 abstract public function populate(Smarty_Template_Cached
$cached, Smarty_Internal_Template
$_template);
45 * populate Cached Object with timestamp and exists from Resource
47 * @param Smarty_Template_Cached $cached
51 abstract public function populateTimestamp(Smarty_Template_Cached
$cached);
54 * Read the cached template and process header
56 * @param Smarty_Internal_Template $_template template object
57 * @param Smarty_Template_Cached $cached cached object
59 * @return boolean true or false if the cached content does not exist
61 abstract public function process(Smarty_Internal_Template
$_template, Smarty_Template_Cached
$cached = null);
64 * Write the rendered template output to cache
66 * @param Smarty_Internal_Template $_template template object
67 * @param string $content content to cache
69 * @return boolean success
71 abstract public function writeCachedContent(Smarty_Internal_Template
$_template, $content);
74 * Return cached content
76 * @param Smarty_Internal_Template $_template template object
80 public function getCachedContent(Smarty_Internal_Template
$_template)
82 if ($_template->cached
->handler
->process($_template)) {
84 $_template->properties
['unifunc']($_template);
86 return ob_get_clean();
95 * @param Smarty $smarty Smarty object
96 * @param integer $exp_time expiration time (number of seconds, not timestamp)
98 * @return integer number of cache files deleted
100 abstract public function clearAll(Smarty
$smarty, $exp_time = null);
103 * Empty cache for a specific template
105 * @param Smarty $smarty Smarty object
106 * @param string $resource_name template name
107 * @param string $cache_id cache id
108 * @param string $compile_id compile id
109 * @param integer $exp_time expiration time (number of seconds, not timestamp)
111 * @return integer number of cache files deleted
113 abstract public function clear(Smarty
$smarty, $resource_name, $cache_id, $compile_id, $exp_time);
116 * @param Smarty $smarty
117 * @param Smarty_Template_Cached $cached
121 public function locked(Smarty
$smarty, Smarty_Template_Cached
$cached)
123 // theoretically locking_timeout should be checked against time_limit (max_execution_time)
124 $start = microtime(true);
126 while ($this->hasLock($smarty, $cached)) {
128 if (microtime(true) - $start > $smarty->locking_timeout
) {
129 // abort waiting for lock release
139 * Check is cache is locked for this template
141 * @param Smarty $smarty
142 * @param Smarty_Template_Cached $cached
146 public function hasLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
148 // check if lock exists
153 * Lock cache for this template
155 * @param Smarty $smarty
156 * @param Smarty_Template_Cached $cached
160 public function acquireLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
167 * Unlock cache for this template
169 * @param Smarty $smarty
170 * @param Smarty_Template_Cached $cached
174 public function releaseLock(Smarty
$smarty, Smarty_Template_Cached
$cached)
181 * Load Cache Resource Handler
183 * @param Smarty $smarty Smarty object
184 * @param string $type name of the cache resource
186 * @throws SmartyException
187 * @return Smarty_CacheResource Cache Resource Handler
189 public static function load(Smarty
$smarty, $type = null)
192 $type = $smarty->caching_type
;
195 // try smarty's cache
196 if (isset($smarty->_cacheresource_handlers
[$type])) {
197 return $smarty->_cacheresource_handlers
[$type];
200 // try registered resource
201 if (isset($smarty->registered_cache_resources
[$type])) {
202 // do not cache these instances as they may vary from instance to instance
203 return $smarty->_cacheresource_handlers
[$type] = $smarty->registered_cache_resources
[$type];
205 // try sysplugins dir
206 if (isset(self
::$sysplugins[$type])) {
207 if (!isset(self
::$resources[$type])) {
208 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
209 self
::$resources[$type] = new $cache_resource_class();
212 return $smarty->_cacheresource_handlers
[$type] = self
::$resources[$type];
215 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
216 if ($smarty->loadPlugin($cache_resource_class)) {
217 if (!isset(self
::$resources[$type])) {
218 self
::$resources[$type] = new $cache_resource_class();
221 return $smarty->_cacheresource_handlers
[$type] = self
::$resources[$type];
224 throw new SmartyException("Unable to load cache resource '{$type}'");
228 * Invalid Loaded Cache Files
230 * @param Smarty $smarty Smarty object
232 public static function invalidLoadedCache(Smarty
$smarty)
234 foreach ($smarty->template_objects
as $tpl) {
235 if (isset($tpl->cached
)) {
236 $tpl->cached
->valid
= false;
237 $tpl->cached
->processed
= false;
244 * Smarty Resource Data Object
245 * Cache Data Container for Template Files
248 * @subpackage TemplateResources
249 * @author Rodney Rehm
251 class Smarty_Template_Cached
258 public $filepath = false;
265 public $content = null;
272 public $timestamp = false;
279 public $exists = false;
286 public $valid = false;
289 * Cache was processed
293 public $processed = false;
296 * CacheResource Handler
298 * @var Smarty_CacheResource
300 public $handler = null;
303 * Template Compile Id (Smarty_Internal_Template::$compile_id)
307 public $compile_id = null;
310 * Template Cache Id (Smarty_Internal_Template::$cache_id)
314 public $cache_id = null;
317 * Id for cache locking
321 public $lock_id = null;
324 * flag that cache is locked by this instance
328 public $is_locked = false;
333 * @var Smarty_Template_Source
335 public $source = null;
338 * create Cached Object container
340 * @param Smarty_Internal_Template $_template template object
342 public function __construct(Smarty_Internal_Template
$_template)
344 $this->compile_id
= $_template->compile_id
;
345 $this->cache_id
= $_template->cache_id
;
346 $this->source
= $_template->source
;
347 $_template->cached
= $this;
348 $smarty = $_template->smarty
;
351 // load resource handler
353 $this->handler
= $handler = Smarty_CacheResource
::load($smarty); // Note: prone to circular references
356 // check if cache is valid
358 if (!($_template->caching
== Smarty
::CACHING_LIFETIME_CURRENT ||
$_template->caching
== Smarty
::CACHING_LIFETIME_SAVED
) ||
$_template->source
->recompiled
) {
359 $handler->populate($this, $_template);
365 $handler->populate($this, $_template);
366 if ($this->timestamp
=== false ||
$smarty->force_compile ||
$smarty->force_cache
) {
367 $this->valid
= false;
371 if ($this->valid
&& $_template->caching
== Smarty
::CACHING_LIFETIME_CURRENT
&& $_template->cache_lifetime
>= 0 && time() > ($this->timestamp +
$_template->cache_lifetime
)) {
373 $this->valid
= false;
375 if ($this->valid ||
!$_template->smarty
->cache_locking
) {
378 if (!$this->handler
->locked($_template->smarty
, $this)) {
379 $this->handler
->acquireLock($_template->smarty
, $this);
384 if (!$_template->smarty
->cache_locking ||
$this->handler
->locked($_template->smarty
, $this) === null) {
385 // load cache file for the following checks
386 if ($smarty->debugging
) {
387 Smarty_Internal_Debug
::start_cache($_template);
389 if ($handler->process($_template, $this) === false) {
390 $this->valid
= false;
392 $this->processed
= true;
394 if ($smarty->debugging
) {
395 Smarty_Internal_Debug
::end_cache($_template);
403 if ($this->valid
&& $_template->caching
=== Smarty
::CACHING_LIFETIME_SAVED
&& $_template->properties
['cache_lifetime'] >= 0 && (time() > ($_template->cached
->timestamp +
$_template->properties
['cache_lifetime']))) {
404 $this->valid
= false;
406 if (!$this->valid
&& $_template->smarty
->cache_locking
) {
407 $this->handler
->acquireLock($_template->smarty
, $this);
417 * Write this cache object to handler
419 * @param Smarty_Internal_Template $_template template object
420 * @param string $content content to cache
422 * @return boolean success
424 public function write(Smarty_Internal_Template
$_template, $content)
426 if (!$_template->source
->recompiled
) {
427 if ($this->handler
->writeCachedContent($_template, $content)) {
428 $this->content
= null;
429 $this->timestamp
= time();
430 $this->exists
= true;
432 if ($_template->smarty
->cache_locking
) {
433 $this->handler
->releaseLock($_template->smarty
, $this);