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)
29 protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
32 * 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
41 * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
43 protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
49 * Save content to cache
51 * @param string $id unique cache content identifier
52 * @param string $name template name
53 * @param string $cache_id cache id
54 * @param string $compile_id compile id
55 * @param integer|null $exp_time seconds till expiration or null
56 * @param string $content content to cache
57 * @return boolean success
59 protected abstract function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
62 * Delete content from cache
64 * @param string $name template name
65 * @param string $cache_id cache id
66 * @param string $compile_id compile id
67 * @param integer|null $exp_time seconds till expiration time in seconds or null
68 * @return integer number of deleted caches
70 protected abstract function delete($name, $cache_id, $compile_id, $exp_time);
73 * populate Cached Object with meta data from Resource
75 * @param Smarty_Template_Cached $cached cached object
76 * @param Smarty_Internal_Template $_template template object
79 public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
81 $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
82 $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
84 $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
85 $this->populateTimestamp($cached);
89 * populate Cached Object with timestamp and exists from Resource
91 * @param Smarty_Template_Cached $source cached object
94 public function populateTimestamp(Smarty_Template_Cached $cached)
96 $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
97 if ($mtime !== null) {
98 $cached->timestamp = $mtime;
99 $cached->exists = !!$cached->timestamp;
103 $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
104 $cached->timestamp = isset($timestamp) ? $timestamp : false;
105 $cached->exists = !!$cached->timestamp;
109 * Read the cached template and process the header
111 * @param Smarty_Internal_Template $_template template object
112 * @param Smarty_Template_Cached $cached cached object
113 * @return booelan true or false if the cached content does not exist
115 public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
118 $cached = $_template->cached;
120 $content = $cached->content ? $cached->content : null;
121 $timestamp = $cached->timestamp ? $cached->timestamp : null;
122 if ($content === null || !$timestamp) {
124 $_template->cached->filepath,
125 $_template->source->name,
126 $_template->cache_id,
127 $_template->compile_id,
132 if (isset($content)) {
133 $_smarty_tpl = $_template;
134 eval("?>" . $content);
141 * Write the rendered template output to cache
143 * @param Smarty_Internal_Template $_template template object
144 * @param string $content content to cache
145 * @return boolean success
147 public function writeCachedContent(Smarty_Internal_Template $_template, $content)
150 $_template->cached->filepath,
151 $_template->source->name,
152 $_template->cache_id,
153 $_template->compile_id,
154 $_template->properties['cache_lifetime'],
162 * @param Smarty $smarty Smarty object
163 * @param integer $exp_time expiration time (number of seconds, not timestamp)
164 * @return integer number of cache files deleted
166 public function clearAll(Smarty $smarty, $exp_time=null)
168 $this->cache = array();
169 return $this->delete(null, null, null, $exp_time);
173 * Empty cache for a specific template
175 * @param Smarty $smarty Smarty object
176 * @param string $resource_name template name
177 * @param string $cache_id cache id
178 * @param string $compile_id compile id
179 * @param integer $exp_time expiration time (number of seconds, not timestamp)
180 * @return integer number of cache files deleted
182 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
184 $this->cache = array();
185 return $this->delete($resource_name, $cache_id, $compile_id, $exp_time);
189 * Check is cache is locked for this template
191 * @param Smarty $smarty Smarty object
192 * @param Smarty_Template_Cached $cached cached object
193 * @return booelan true or false if cache is locked
195 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
197 $id = $cached->filepath;
198 $name = $cached->source->name . '.lock';
200 $mtime = $this->fetchTimestamp($id, $name, null, null);
201 if ($mtime === null) {
202 $this->fetch($id, $name, null, null, $content, $mtime);
205 return $mtime && time() - $mtime < $smarty->locking_timeout;
209 * Lock cache for this template
211 * @param Smarty $smarty Smarty object
212 * @param Smarty_Template_Cached $cached cached object
214 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
216 $cached->is_locked = true;
218 $id = $cached->filepath;
219 $name = $cached->source->name . '.lock';
220 $this->save($id, $name, null, null, $smarty->locking_timeout, '');
224 * Unlock cache for this template
226 * @param Smarty $smarty Smarty object
227 * @param Smarty_Template_Cached $cached cached object
229 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
231 $cached->is_locked = false;
233 $name = $cached->source->name . '.lock';
234 $this->delete($name, null, null, null);