3 * Smarty Internal Plugin
16 abstract class Smarty_CacheResource
19 * resource types provided by the core
23 protected static $sysplugins = array('file' => 'smarty_internal_cacheresource_file.php',);
26 * populate Cached Object with meta data from Resource
28 * @param Smarty_Template_Cached $cached cached object
29 * @param Smarty_Internal_Template $_template template object
33 abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
36 * populate Cached Object with timestamp and exists from Resource
38 * @param Smarty_Template_Cached $cached
42 abstract public function populateTimestamp(Smarty_Template_Cached $cached);
45 * Read the cached template and process header
47 * @param Smarty_Internal_Template $_template template object
48 * @param Smarty_Template_Cached $cached cached object
49 * @param boolean $update flag if called because cache update
51 * @return boolean true or false if the cached content does not exist
53 abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null,
57 * Write the rendered template output to cache
59 * @param Smarty_Internal_Template $_template template object
60 * @param string $content content to cache
62 * @return boolean success
64 abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content);
67 * Read cached template from cache
69 * @param Smarty_Internal_Template $_template template object
71 * @return string content
73 abstract function readCachedContent(Smarty_Internal_Template $_template);
76 * Return cached content
78 * @param Smarty_Internal_Template $_template template object
82 public function getCachedContent(Smarty_Internal_Template $_template)
84 if ($_template->cached->handler->process($_template)) {
86 $unifunc = $_template->cached->unifunc;
88 return ob_get_clean();
97 * @param Smarty $smarty Smarty object
98 * @param integer $exp_time expiration time (number of seconds, not timestamp)
100 * @return integer number of cache files deleted
102 abstract public function clearAll(Smarty $smarty, $exp_time = null);
105 * Empty cache for a specific template
107 * @param Smarty $smarty Smarty object
108 * @param string $resource_name template name
109 * @param string $cache_id cache id
110 * @param string $compile_id compile id
111 * @param integer $exp_time expiration time (number of seconds, not timestamp)
113 * @return integer number of cache files deleted
115 abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
118 * @param Smarty $smarty
119 * @param Smarty_Template_Cached $cached
123 public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
125 // theoretically locking_timeout should be checked against time_limit (max_execution_time)
126 $start = microtime(true);
128 while ($this->hasLock($smarty, $cached)) {
130 if (microtime(true) - $start > $smarty->locking_timeout) {
131 // abort waiting for lock release
141 * Check is cache is locked for this template
143 * @param Smarty $smarty
144 * @param Smarty_Template_Cached $cached
148 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
150 // check if lock exists
155 * Lock cache for this template
157 * @param Smarty $smarty
158 * @param Smarty_Template_Cached $cached
162 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
169 * Unlock cache for this template
171 * @param Smarty $smarty
172 * @param Smarty_Template_Cached $cached
176 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
183 * Load Cache Resource Handler
185 * @param Smarty $smarty Smarty object
186 * @param string $type name of the cache resource
188 * @throws SmartyException
189 * @return Smarty_CacheResource Cache Resource Handler
191 public static function load(Smarty $smarty, $type = null)
194 $type = $smarty->caching_type;
197 // try smarty's cache
198 if (isset($smarty->_cache[ 'cacheresource_handlers' ][ $type ])) {
199 return $smarty->_cache[ 'cacheresource_handlers' ][ $type ];
202 // try registered resource
203 if (isset($smarty->registered_cache_resources[ $type ])) {
204 // do not cache these instances as they may vary from instance to instance
205 return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = $smarty->registered_cache_resources[ $type ];
207 // try sysplugins dir
208 if (isset(self::$sysplugins[ $type ])) {
209 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
210 return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class();
213 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
214 if ($smarty->loadPlugin($cache_resource_class)) {
215 return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class();
218 throw new SmartyException("Unable to load cache resource '{$type}'");