You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_cacheresource_custom.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Smarty Internal Plugin
  4. *
  5. * @package Smarty
  6. * @subpackage Cacher
  7. */
  8. /**
  9. * Cache Handler API
  10. *
  11. * @package Smarty
  12. * @subpackage Cacher
  13. * @author Rodney Rehm
  14. */
  15. abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
  16. {
  17. /**
  18. * fetch cached content and its modification time from data source
  19. *
  20. * @param string $id unique cache content identifier
  21. * @param string $name template name
  22. * @param string $cache_id cache id
  23. * @param string $compile_id compile id
  24. * @param string $content cached content
  25. * @param integer $mtime cache modification timestamp (epoch)
  26. *
  27. * @return void
  28. */
  29. abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
  30. /**
  31. * Fetch cached content's modification timestamp from data source
  32. * {@internal implementing this method is optional.
  33. * Only implement it if modification times can be accessed faster than loading the complete cached content.}}
  34. *
  35. * @param string $id unique cache content identifier
  36. * @param string $name template name
  37. * @param string $cache_id cache id
  38. * @param string $compile_id compile id
  39. *
  40. * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
  41. */
  42. protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
  43. {
  44. return false;
  45. }
  46. /**
  47. * Save content to cache
  48. *
  49. * @param string $id unique cache content identifier
  50. * @param string $name template name
  51. * @param string $cache_id cache id
  52. * @param string $compile_id compile id
  53. * @param integer|null $exp_time seconds till expiration or null
  54. * @param string $content content to cache
  55. *
  56. * @return boolean success
  57. */
  58. abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
  59. /**
  60. * Delete content from cache
  61. *
  62. * @param string|null $name template name
  63. * @param string|null $cache_id cache id
  64. * @param string|null $compile_id compile id
  65. * @param integer|null $exp_time seconds till expiration time in seconds or null
  66. *
  67. * @return integer number of deleted caches
  68. */
  69. abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
  70. /**
  71. * populate Cached Object with meta data from Resource
  72. *
  73. * @param Smarty_Template_Cached $cached cached object
  74. * @param Smarty_Internal_Template $_template template object
  75. *
  76. * @return void
  77. */
  78. public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
  79. {
  80. $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
  81. $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
  82. $path = $cached->source->uid . $_cache_id . $_compile_id;
  83. $cached->filepath = sha1($path);
  84. if ($_template->smarty->cache_locking) {
  85. $cached->lock_id = sha1('lock.' . $path);
  86. }
  87. $this->populateTimestamp($cached);
  88. }
  89. /**
  90. * populate Cached Object with timestamp and exists from Resource
  91. *
  92. * @param Smarty_Template_Cached $cached
  93. *
  94. * @return void
  95. */
  96. public function populateTimestamp(Smarty_Template_Cached $cached)
  97. {
  98. $mtime =
  99. $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
  100. if ($mtime !== null) {
  101. $cached->timestamp = $mtime;
  102. $cached->exists = !!$cached->timestamp;
  103. return;
  104. }
  105. $timestamp = null;
  106. $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content,
  107. $timestamp);
  108. $cached->timestamp = isset($timestamp) ? $timestamp : false;
  109. $cached->exists = !!$cached->timestamp;
  110. }
  111. /**
  112. * Read the cached template and process the header
  113. *
  114. * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
  115. * @param Smarty_Template_Cached $cached cached object
  116. * @param boolean $update flag if called because cache update
  117. *
  118. * @return boolean true or false if the cached content does not exist
  119. */
  120. public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
  121. $update = false)
  122. {
  123. if (!$cached) {
  124. $cached = $_smarty_tpl->cached;
  125. }
  126. $content = $cached->content ? $cached->content : null;
  127. $timestamp = $cached->timestamp ? $cached->timestamp : null;
  128. if ($content === null || !$timestamp) {
  129. $this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id,
  130. $_smarty_tpl->compile_id, $content, $timestamp);
  131. }
  132. if (isset($content)) {
  133. eval('?>' . $content);
  134. $cached->content = null;
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Write the rendered template output to cache
  141. *
  142. * @param Smarty_Internal_Template $_template template object
  143. * @param string $content content to cache
  144. *
  145. * @return boolean success
  146. */
  147. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  148. {
  149. return $this->save($_template->cached->filepath, $_template->source->name, $_template->cache_id,
  150. $_template->compile_id, $_template->cache_lifetime, $content);
  151. }
  152. /**
  153. * Read cached template from cache
  154. *
  155. * @param Smarty_Internal_Template $_template template object
  156. *
  157. * @return string|boolean content
  158. */
  159. public function readCachedContent(Smarty_Internal_Template $_template)
  160. {
  161. $content = $_template->cached->content ? $_template->cached->content : null;
  162. $timestamp = null;
  163. if ($content === null) {
  164. $timestamp = null;
  165. $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id,
  166. $_template->compile_id, $content, $timestamp);
  167. }
  168. if (isset($content)) {
  169. return $content;
  170. }
  171. return false;
  172. }
  173. /**
  174. * Empty cache
  175. *
  176. * @param Smarty $smarty Smarty object
  177. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  178. *
  179. * @return integer number of cache files deleted
  180. */
  181. public function clearAll(Smarty $smarty, $exp_time = null)
  182. {
  183. return $this->delete(null, null, null, $exp_time);
  184. }
  185. /**
  186. * Empty cache for a specific template
  187. *
  188. * @param Smarty $smarty Smarty object
  189. * @param string $resource_name template name
  190. * @param string $cache_id cache id
  191. * @param string $compile_id compile id
  192. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  193. *
  194. * @return int number of cache files deleted
  195. * @throws \SmartyException
  196. */
  197. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  198. {
  199. $cache_name = null;
  200. if (isset($resource_name)) {
  201. $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
  202. if ($source->exists) {
  203. $cache_name = $source->name;
  204. } else {
  205. return 0;
  206. }
  207. }
  208. return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
  209. }
  210. /**
  211. * Check is cache is locked for this template
  212. *
  213. * @param Smarty $smarty Smarty object
  214. * @param Smarty_Template_Cached $cached cached object
  215. *
  216. * @return boolean true or false if cache is locked
  217. */
  218. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  219. {
  220. $id = $cached->lock_id;
  221. $name = $cached->source->name . '.lock';
  222. $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
  223. if ($mtime === null) {
  224. $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
  225. }
  226. return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
  227. }
  228. /**
  229. * Lock cache for this template
  230. *
  231. * @param Smarty $smarty Smarty object
  232. * @param Smarty_Template_Cached $cached cached object
  233. *
  234. * @return bool|void
  235. */
  236. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  237. {
  238. $cached->is_locked = true;
  239. $id = $cached->lock_id;
  240. $name = $cached->source->name . '.lock';
  241. $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
  242. }
  243. /**
  244. * Unlock cache for this template
  245. *
  246. * @param Smarty $smarty Smarty object
  247. * @param Smarty_Template_Cached $cached cached object
  248. *
  249. * @return bool|void
  250. */
  251. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  252. {
  253. $cached->is_locked = false;
  254. $name = $cached->source->name . '.lock';
  255. $this->delete($name, $cached->cache_id, $cached->compile_id, null);
  256. }
  257. }