3 * Smarty Internal Plugin
16 abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
19 * fetch cached content and its modification time from data source
21 * @param string $id unique cache content identifier
22 * @param string $name template name
23 * @param string $cache_id cache id
24 * @param string $compile_id compile id
25 * @param string $content cached content
26 * @param integer $mtime cache modification timestamp (epoch)
30 abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
33 * Fetch cached content's modification timestamp from data source
34 * {@internal implementing this method is optional.
35 * Only implement it if modification times can be accessed faster than loading the complete cached content.}}
37 * @param string $id unique cache content identifier
38 * @param string $name template name
39 * @param string $cache_id cache id
40 * @param string $compile_id compile id
42 * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
44 protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
50 * Save content to cache
52 * @param string $id unique cache content identifier
53 * @param string $name template name
54 * @param string $cache_id cache id
55 * @param string $compile_id compile id
56 * @param integer|null $exp_time seconds till expiration or null
57 * @param string $content content to cache
59 * @return boolean success
61 abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
64 * Delete content from cache
66 * @param string|null $name template name
67 * @param string|null $cache_id cache id
68 * @param string|null $compile_id compile id
69 * @param integer|null $exp_time seconds till expiration time in seconds or null
71 * @return integer number of deleted caches
73 abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
76 * populate Cached Object with meta data from Resource
78 * @param Smarty_Template_Cached $cached cached object
79 * @param Smarty_Internal_Template $_template template object
83 public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
85 $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
86 $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
87 $path = $cached->source->uid . $_cache_id . $_compile_id;
88 $cached->filepath = sha1($path);
89 if ($_template->smarty->cache_locking) {
90 $cached->lock_id = sha1('lock.' . $path);
92 $this->populateTimestamp($cached);
96 * populate Cached Object with timestamp and exists from Resource
98 * @param Smarty_Template_Cached $cached
102 public function populateTimestamp(Smarty_Template_Cached $cached)
105 $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
106 if ($mtime !== null) {
107 $cached->timestamp = $mtime;
108 $cached->exists = !!$cached->timestamp;
113 $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content,
115 $cached->timestamp = isset($timestamp) ? $timestamp : false;
116 $cached->exists = !!$cached->timestamp;
120 * Read the cached template and process the header
122 * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
123 * @param Smarty_Template_Cached $cached cached object
124 * @param boolean $update flag if called because cache update
126 * @return boolean true or false if the cached content does not exist
128 public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
132 $cached = $_smarty_tpl->cached;
134 $content = $cached->content ? $cached->content : null;
135 $timestamp = $cached->timestamp ? $cached->timestamp : null;
136 if ($content === null || !$timestamp) {
137 $this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id,
138 $_smarty_tpl->compile_id, $content, $timestamp);
140 if (isset($content)) {
141 eval("?>" . $content);
142 $cached->content = null;
150 * Write the rendered template output to cache
152 * @param Smarty_Internal_Template $_template template object
153 * @param string $content content to cache
155 * @return boolean success
157 public function writeCachedContent(Smarty_Internal_Template $_template, $content)
159 return $this->save($_template->cached->filepath, $_template->source->name, $_template->cache_id,
160 $_template->compile_id, $_template->cache_lifetime, $content);
164 * Read cached template from cache
166 * @param Smarty_Internal_Template $_template template object
168 * @return string|boolean content
170 public function readCachedContent(Smarty_Internal_Template $_template)
172 $content = $_template->cached->content ? $_template->cached->content : null;
174 if ($content === null) {
176 $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id,
177 $_template->compile_id, $content, $timestamp);
179 if (isset($content)) {
188 * @param Smarty $smarty Smarty object
189 * @param integer $exp_time expiration time (number of seconds, not timestamp)
191 * @return integer number of cache files deleted
193 public function clearAll(Smarty $smarty, $exp_time = null)
195 return $this->delete(null, null, null, $exp_time);
199 * Empty cache for a specific template
201 * @param Smarty $smarty Smarty object
202 * @param string $resource_name template name
203 * @param string $cache_id cache id
204 * @param string $compile_id compile id
205 * @param integer $exp_time expiration time (number of seconds, not timestamp)
207 * @return integer number of cache files deleted
209 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
213 if (isset($resource_name)) {
214 $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
215 if ($source->exists) {
216 $cache_name = $source->name;
222 return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
226 * Check is cache is locked for this template
228 * @param Smarty $smarty Smarty object
229 * @param Smarty_Template_Cached $cached cached object
231 * @return boolean true or false if cache is locked
233 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
235 $id = $cached->lock_id;
236 $name = $cached->source->name . '.lock';
238 $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
239 if ($mtime === null) {
240 $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
242 return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
246 * Lock cache for this template
248 * @param Smarty $smarty Smarty object
249 * @param Smarty_Template_Cached $cached cached object
253 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
255 $cached->is_locked = true;
256 $id = $cached->lock_id;
257 $name = $cached->source->name . '.lock';
258 $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
262 * Unlock cache for this template
264 * @param Smarty $smarty Smarty object
265 * @param Smarty_Template_Cached $cached cached object
269 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
271 $cached->is_locked = false;
272 $name = $cached->source->name . '.lock';
273 $this->delete($name, $cached->cache_id, $cached->compile_id, null);