10 * Smarty Resource Data Object
11 * Cache Data Container for Template Files
14 * @subpackage TemplateResources
17 class Smarty_Template_Cached extends Smarty_Template_Resource_Base
27 * CacheResource Handler
29 * @var Smarty_CacheResource
31 public $handler = null;
34 * Template Cache Id (Smarty_Internal_Template::$cache_id)
38 public $cache_id = null;
41 * saved cache lifetime in seconds
45 public $cache_lifetime = 0;
48 * Id for cache locking
52 public $lock_id = null;
55 * flag that cache is locked by this instance
59 public $is_locked = false;
64 * @var Smarty_Template_Source
66 public $source = null;
69 * Nocache hash codes of processed compiled templates
73 public $hashes = array();
76 * Flag if this is a cache resource
80 public $isCache = true;
83 * create Cached Object container
85 * @param Smarty_Internal_Template $_template template object
87 public function __construct(Smarty_Internal_Template $_template)
89 $this->compile_id = $_template->compile_id;
90 $this->cache_id = $_template->cache_id;
91 $this->source = $_template->source;
92 if (!class_exists('Smarty_CacheResource', false)) {
93 require SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
95 $this->handler = Smarty_CacheResource::load($_template->smarty);
99 * @param Smarty_Internal_Template $_template
101 * @return Smarty_Template_Cached
103 static function load(Smarty_Internal_Template $_template)
105 $_template->cached = new Smarty_Template_Cached($_template);
106 $_template->cached->handler->populate($_template->cached, $_template);
108 if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
109 $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->handler->recompiled
111 $_template->cached->valid = false;
113 return $_template->cached;
117 * Render cache template
119 * @param \Smarty_Internal_Template $_template
120 * @param bool $no_output_filter
124 public function render(Smarty_Internal_Template $_template, $no_output_filter = true)
126 if ($this->isCached($_template)) {
127 if ($_template->smarty->debugging) {
128 if (!isset($_template->smarty->_debug)) {
129 $_template->smarty->_debug = new Smarty_Internal_Debug();
131 $_template->smarty->_debug->start_cache($_template);
133 if (!$this->processed) {
134 $this->process($_template);
136 $this->getRenderedTemplateCode($_template);
137 if ($_template->smarty->debugging) {
138 $_template->smarty->_debug->end_cache($_template);
142 $_template->smarty->ext->_updateCache->updateCache($this, $_template, $no_output_filter);
147 * Check if cache is valid, lock cache if required
149 * @param \Smarty_Internal_Template $_template
151 * @return bool flag true if cache is valid
153 public function isCached(Smarty_Internal_Template $_template)
155 if ($this->valid !== null) {
160 if ($this->exists === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
161 $this->valid = false;
165 if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT &&
166 $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)
169 $this->valid = false;
171 if ($this->valid && $_template->smarty->compile_check == 1 &&
172 $_template->source->getTimeStamp() > $this->timestamp
174 $this->valid = false;
176 if ($this->valid || !$_template->smarty->cache_locking) {
179 if (!$this->handler->locked($_template->smarty, $this)) {
180 $this->handler->acquireLock($_template->smarty, $this);
183 $this->handler->populate($this, $_template);
186 if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
187 // load cache file for the following checks
188 if ($_template->smarty->debugging) {
189 $_template->smarty->_debug->start_cache($_template);
191 if ($this->handler->process($_template, $this) === false) {
192 $this->valid = false;
194 $this->processed = true;
196 if ($_template->smarty->debugging) {
197 $_template->smarty->_debug->end_cache($_template);
200 $this->is_locked = true;
206 if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED &&
207 $_template->cached->cache_lifetime >= 0 &&
208 (time() > ($_template->cached->timestamp + $_template->cached->cache_lifetime))
210 $this->valid = false;
212 if ($_template->smarty->cache_locking) {
214 $this->handler->acquireLock($_template->smarty, $this);
215 } elseif ($this->is_locked) {
216 $this->handler->releaseLock($_template->smarty, $this);
225 * Process cached template
227 * @param Smarty_Internal_Template $_template template object
228 * @param bool $update flag if called because cache update
230 public function process(Smarty_Internal_Template $_template, $update = false)
232 if ($this->handler->process($_template, $this, $update) === false) {
233 $this->valid = false;
236 $this->processed = true;
238 $this->processed = false;
243 * Read cache content from handler
245 * @param Smarty_Internal_Template $_template template object
247 * @return string|false content
249 public function read(Smarty_Internal_Template $_template)
251 if (!$_template->source->handler->recompiled) {
252 return $this->handler->readCachedContent($_template);