smarty_cacheresource_custom.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 null;
  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 $name template name
  63. * @param string $cache_id cache id
  64. * @param string $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. $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
  83. $this->populateTimestamp($cached);
  84. }
  85. /**
  86. * populate Cached Object with timestamp and exists from Resource
  87. *
  88. * @param Smarty_Template_Cached $cached
  89. *
  90. * @return void
  91. */
  92. public function populateTimestamp(Smarty_Template_Cached $cached)
  93. {
  94. $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
  95. if ($mtime !== null) {
  96. $cached->timestamp = $mtime;
  97. $cached->exists = !!$cached->timestamp;
  98. return;
  99. }
  100. $timestamp = null;
  101. $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
  102. $cached->timestamp = isset($timestamp) ? $timestamp : false;
  103. $cached->exists = !!$cached->timestamp;
  104. }
  105. /**
  106. * Read the cached template and process the header
  107. *
  108. * @param Smarty_Internal_Template $_template template object
  109. * @param Smarty_Template_Cached $cached cached object
  110. *
  111. * @return boolean true or false if the cached content does not exist
  112. */
  113. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
  114. {
  115. if (!$cached) {
  116. $cached = $_template->cached;
  117. }
  118. $content = $cached->content ? $cached->content : null;
  119. $timestamp = $cached->timestamp ? $cached->timestamp : null;
  120. if ($content === null || !$timestamp) {
  121. $this->fetch(
  122. $_template->cached->filepath,
  123. $_template->source->name,
  124. $_template->cache_id,
  125. $_template->compile_id,
  126. $content,
  127. $timestamp
  128. );
  129. }
  130. if (isset($content)) {
  131. /** @var Smarty_Internal_Template $_smarty_tpl
  132. * used in evaluated code
  133. */
  134. $_smarty_tpl = $_template;
  135. eval("?>" . $content);
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Write the rendered template output to cache
  142. *
  143. * @param Smarty_Internal_Template $_template template object
  144. * @param string $content content to cache
  145. *
  146. * @return boolean success
  147. */
  148. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  149. {
  150. return $this->save(
  151. $_template->cached->filepath,
  152. $_template->source->name,
  153. $_template->cache_id,
  154. $_template->compile_id,
  155. $_template->properties['cache_lifetime'],
  156. $content
  157. );
  158. }
  159. /**
  160. * Empty cache
  161. *
  162. * @param Smarty $smarty Smarty object
  163. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  164. *
  165. * @return integer number of cache files deleted
  166. */
  167. public function clearAll(Smarty $smarty, $exp_time = null)
  168. {
  169. $this->cache = array();
  170. return $this->delete(null, null, null, $exp_time);
  171. }
  172. /**
  173. * Empty cache for a specific template
  174. *
  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. *
  181. * @return integer number of cache files deleted
  182. */
  183. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  184. {
  185. $this->cache = array();
  186. $cache_name = null;
  187. if (isset($resource_name)) {
  188. $_save_stat = $smarty->caching;
  189. $smarty->caching = true;
  190. $tpl = new $smarty->template_class($resource_name, $smarty);
  191. $smarty->caching = $_save_stat;
  192. if ($tpl->source->exists) {
  193. $cache_name = $tpl->source->name;
  194. } else {
  195. return 0;
  196. }
  197. // remove from template cache
  198. if ($smarty->allow_ambiguous_resources) {
  199. $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
  200. } else {
  201. $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
  202. }
  203. if (isset($_templateId[150])) {
  204. $_templateId = sha1($_templateId);
  205. }
  206. unset($smarty->template_objects[$_templateId]);
  207. // template object no longer needed
  208. unset($tpl);
  209. }
  210. return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
  211. }
  212. /**
  213. * Check is cache is locked for this template
  214. *
  215. * @param Smarty $smarty Smarty object
  216. * @param Smarty_Template_Cached $cached cached object
  217. *
  218. * @return boolean true or false if cache is locked
  219. */
  220. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  221. {
  222. $id = $cached->filepath;
  223. $name = $cached->source->name . '.lock';
  224. $mtime = $this->fetchTimestamp($id, $name, null, null);
  225. if ($mtime === null) {
  226. $this->fetch($id, $name, null, null, $content, $mtime);
  227. }
  228. return $mtime && time() - $mtime < $smarty->locking_timeout;
  229. }
  230. /**
  231. * Lock cache for this template
  232. *
  233. * @param Smarty $smarty Smarty object
  234. * @param Smarty_Template_Cached $cached cached object
  235. *
  236. * @return bool|void
  237. */
  238. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  239. {
  240. $cached->is_locked = true;
  241. $id = $cached->filepath;
  242. $name = $cached->source->name . '.lock';
  243. $this->save($id, $name, null, null, $smarty->locking_timeout, '');
  244. }
  245. /**
  246. * Unlock cache for this template
  247. *
  248. * @param Smarty $smarty Smarty object
  249. * @param Smarty_Template_Cached $cached cached object
  250. *
  251. * @return bool|void
  252. */
  253. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  254. {
  255. $cached->is_locked = false;
  256. $name = $cached->source->name . '.lock';
  257. $this->delete($name, null, null, null);
  258. }
  259. }