3 * Smarty Internal Plugin
16 abstract class Smarty_CacheResource {
18 * cache for Smarty_CacheResource instances
21 public static $resources = array();
24 * resource types provided by the core
27 protected static $sysplugins = array(
32 * populate Cached Object with meta data from Resource
34 * @param Smarty_Template_Cached $cached cached object
35 * @param Smarty_Internal_Template $_template template object
38 public abstract function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
41 * populate Cached Object with timestamp and exists from Resource
43 * @param Smarty_Template_Cached $source cached object
46 public abstract function populateTimestamp(Smarty_Template_Cached $cached);
49 * Read the cached template and process header
51 * @param Smarty_Internal_Template $_template template object
52 * @param Smarty_Template_Cached $cached cached object
53 * @return booelan true or false if the cached content does not exist
55 public abstract function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null);
58 * Write the rendered template output to cache
60 * @param Smarty_Internal_Template $_template template object
61 * @param string $content content to cache
62 * @return boolean success
64 public abstract function writeCachedContent(Smarty_Internal_Template $_template, $content);
67 * Return cached content
69 * @param Smarty_Internal_Template $_template template object
70 * @param string $content content of cache
72 public function getCachedContent(Smarty_Internal_Template $_template)
74 if ($_template->cached->handler->process($_template)) {
76 $_template->properties['unifunc']($_template);
77 return ob_get_clean();
85 * @param Smarty $smarty Smarty object
86 * @param integer $exp_time expiration time (number of seconds, not timestamp)
87 * @return integer number of cache files deleted
89 public abstract function clearAll(Smarty $smarty, $exp_time=null);
92 * Empty cache for a specific template
94 * @param Smarty $smarty Smarty object
95 * @param string $resource_name template name
96 * @param string $cache_id cache id
97 * @param string $compile_id compile id
98 * @param integer $exp_time expiration time (number of seconds, not timestamp)
99 * @return integer number of cache files deleted
101 public abstract function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
104 public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
106 // theoretically locking_timeout should be checked against time_limit (max_execution_time)
107 $start = microtime(true);
109 while ($this->hasLock($smarty, $cached)) {
111 if (microtime(true) - $start > $smarty->locking_timeout) {
112 // abort waiting for lock release
120 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
122 // check if lock exists
126 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
132 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
140 * Load Cache Resource Handler
142 * @param Smarty $smarty Smarty object
143 * @param string $type name of the cache resource
144 * @return Smarty_CacheResource Cache Resource Handler
146 public static function load(Smarty $smarty, $type = null)
149 $type = $smarty->caching_type;
152 // try smarty's cache
153 if (isset($smarty->_cacheresource_handlers[$type])) {
154 return $smarty->_cacheresource_handlers[$type];
157 // try registered resource
158 if (isset($smarty->registered_cache_resources[$type])) {
159 // do not cache these instances as they may vary from instance to instance
160 return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type];
162 // try sysplugins dir
163 if (isset(self::$sysplugins[$type])) {
164 if (!isset(self::$resources[$type])) {
165 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
166 self::$resources[$type] = new $cache_resource_class();
168 return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
171 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
172 if ($smarty->loadPlugin($cache_resource_class)) {
173 if (!isset(self::$resources[$type])) {
174 self::$resources[$type] = new $cache_resource_class();
176 return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
179 throw new SmartyException("Unable to load cache resource '{$type}'");
183 * Invalid Loaded Cache Files
185 * @param Smarty $smarty Smarty object
187 public static function invalidLoadedCache(Smarty $smarty)
189 foreach ($smarty->template_objects as $tpl) {
190 if (isset($tpl->cached)) {
191 $tpl->cached->valid = false;
192 $tpl->cached->processed = false;
199 * Smarty Resource Data Object
201 * Cache Data Container for Template Files
204 * @subpackage TemplateResources
205 * @author Rodney Rehm
207 class Smarty_Template_Cached {
212 public $filepath = false;
218 public $content = null;
224 public $timestamp = false;
230 public $exists = false;
236 public $valid = false;
239 * Cache was processed
242 public $processed = false;
245 * CacheResource Handler
246 * @var Smarty_CacheResource
248 public $handler = null;
251 * Template Compile Id (Smarty_Internal_Template::$compile_id)
254 public $compile_id = null;
257 * Template Cache Id (Smarty_Internal_Template::$cache_id)
260 public $cache_id = null;
263 * Id for cache locking
266 public $lock_id = null;
269 * flag that cache is locked by this instance
272 public $is_locked = false;
276 * @var Smarty_Template_Source
278 public $source = null;
281 * create Cached Object container
283 * @param Smarty_Internal_Template $_template template object
285 public function __construct(Smarty_Internal_Template $_template)
287 $this->compile_id = $_template->compile_id;
288 $this->cache_id = $_template->cache_id;
289 $this->source = $_template->source;
290 $_template->cached = $this;
291 $smarty = $_template->smarty;
294 // load resource handler
296 $this->handler = $handler = Smarty_CacheResource::load($smarty); // Note: prone to circular references
299 // check if cache is valid
301 if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->recompiled) {
302 $handler->populate($this, $_template);
307 $handler->populate($this, $_template);
308 if ($this->timestamp === false || $smarty->force_compile || $smarty->force_cache) {
309 $this->valid = false;
313 if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
315 $this->valid = false;
317 if ($this->valid || !$_template->smarty->cache_locking) {
320 if (!$this->handler->locked($_template->smarty, $this)) {
321 $this->handler->acquireLock($_template->smarty, $this);
326 if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
327 // load cache file for the following checks
328 if ($smarty->debugging) {
329 Smarty_Internal_Debug::start_cache($_template);
331 if($handler->process($_template, $this) === false) {
332 $this->valid = false;
334 $this->processed = true;
336 if ($smarty->debugging) {
337 Smarty_Internal_Debug::end_cache($_template);
345 if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
346 $this->valid = false;
348 if (!$this->valid && $_template->smarty->cache_locking) {
349 $this->handler->acquireLock($_template->smarty, $this);
358 * Write this cache object to handler
360 * @param Smarty_Internal_Template $_template template object
361 * @param string $content content to cache
362 * @return boolean success
364 public function write(Smarty_Internal_Template $_template, $content)
366 if (!$_template->source->recompiled) {
367 if ($this->handler->writeCachedContent($_template, $content)) {
368 $this->timestamp = time();
369 $this->exists = true;
371 if ($_template->smarty->cache_locking) {
372 $this->handler->releaseLock($_template->smarty, $this);