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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. $path = $cached->source->filepath . $_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 = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
  99. if ($mtime !== null) {
  100. $cached->timestamp = $mtime;
  101. $cached->exists = !!$cached->timestamp;
  102. return;
  103. }
  104. $timestamp = null;
  105. $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
  106. $cached->timestamp = isset($timestamp) ? $timestamp : false;
  107. $cached->exists = !!$cached->timestamp;
  108. }
  109. /**
  110. * Read the cached template and process the header
  111. *
  112. * @param Smarty_Internal_Template $_template template object
  113. * @param Smarty_Template_Cached $cached cached object
  114. * @param bool $update flag if called because cache update
  115. *
  116. * @return boolean true or false if the cached content does not exist
  117. */
  118. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false)
  119. {
  120. if (!$cached) {
  121. $cached = $_template->cached;
  122. }
  123. $content = $cached->content ? $cached->content : null;
  124. $timestamp = $cached->timestamp ? $cached->timestamp : null;
  125. if ($content === null || !$timestamp) {
  126. $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp);
  127. }
  128. if (isset($content)) {
  129. /** @var Smarty_Internal_Template $_smarty_tpl
  130. * used in evaluated code
  131. */
  132. $_smarty_tpl = $_template;
  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, $_template->compile_id, $_template->cache_lifetime, $content);
  150. }
  151. /**
  152. * Read cached template from cache
  153. *
  154. * @param Smarty_Internal_Template $_template template object
  155. *
  156. * @return string content
  157. */
  158. public function readCachedContent(Smarty_Internal_Template $_template)
  159. {
  160. $content = $_template->cached->content ? $_template->cached->content : null;
  161. $timestamp = null;
  162. if ($content === null) {
  163. $timestamp = null;
  164. $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id, $_template->compile_id, $content, $timestamp);
  165. }
  166. if (isset($content)) {
  167. return $content;
  168. }
  169. return false;
  170. }
  171. /**
  172. * Empty cache
  173. *
  174. * @param Smarty $smarty Smarty object
  175. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  176. *
  177. * @return integer number of cache files deleted
  178. */
  179. public function clearAll(Smarty $smarty, $exp_time = null)
  180. {
  181. return $this->delete(null, null, null, $exp_time);
  182. }
  183. /**
  184. * Empty cache for a specific template
  185. *
  186. * @param Smarty $smarty Smarty object
  187. * @param string $resource_name template name
  188. * @param string $cache_id cache id
  189. * @param string $compile_id compile id
  190. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  191. *
  192. * @return integer number of cache files deleted
  193. */
  194. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  195. {
  196. $cache_name = null;
  197. if (isset($resource_name)) {
  198. $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
  199. if ($source->exists) {
  200. $cache_name = $source->name;
  201. } else {
  202. return 0;
  203. }
  204. // remove from template cache
  205. if (isset($smarty->_cache['template_objects'])) {
  206. foreach ($smarty->_cache['template_objects'] as $key => $_tpl) {
  207. if (isset($_tpl->cached) && $_tpl->source->uid == $source->uid) {
  208. unset($smarty->_cache['template_objects'][$key]);
  209. }
  210. }
  211. }
  212. }
  213. return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
  214. }
  215. /**
  216. * Check is cache is locked for this template
  217. *
  218. * @param Smarty $smarty Smarty object
  219. * @param Smarty_Template_Cached $cached cached object
  220. *
  221. * @return boolean true or false if cache is locked
  222. */
  223. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  224. {
  225. $id = $cached->lock_id;
  226. $name = $cached->source->name . '.lock';
  227. $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
  228. if ($mtime === null) {
  229. $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
  230. }
  231. return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
  232. }
  233. /**
  234. * Lock cache for this template
  235. *
  236. * @param Smarty $smarty Smarty object
  237. * @param Smarty_Template_Cached $cached cached object
  238. *
  239. * @return bool|void
  240. */
  241. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  242. {
  243. $cached->is_locked = true;
  244. $id = $cached->lock_id;
  245. $name = $cached->source->name . '.lock';
  246. $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
  247. }
  248. /**
  249. * Unlock cache for this template
  250. *
  251. * @param Smarty $smarty Smarty object
  252. * @param Smarty_Template_Cached $cached cached object
  253. *
  254. * @return bool|void
  255. */
  256. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  257. {
  258. $cached->is_locked = false;
  259. $name = $cached->source->name . '.lock';
  260. $this->delete($name, $cached->cache_id, $cached->compile_id, null);
  261. }
  262. }