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.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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
  16. {
  17. /**
  18. * resource types provided by the core
  19. *
  20. * @var array
  21. */
  22. protected static $sysplugins = array('file' => 'smarty_internal_cacheresource_file.php',);
  23. /**
  24. * populate Cached Object with meta data from Resource
  25. *
  26. * @param Smarty_Template_Cached $cached cached object
  27. * @param Smarty_Internal_Template $_template template object
  28. *
  29. * @return void
  30. */
  31. abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
  32. /**
  33. * populate Cached Object with timestamp and exists from Resource
  34. *
  35. * @param Smarty_Template_Cached $cached
  36. *
  37. * @return void
  38. */
  39. abstract public function populateTimestamp(Smarty_Template_Cached $cached);
  40. /**
  41. * Read the cached template and process header
  42. *
  43. * @param Smarty_Internal_Template $_template template object
  44. * @param Smarty_Template_Cached $cached cached object
  45. * @param bool $update flag if called because cache update
  46. *
  47. * @return bool true or false if the cached content does not exist
  48. */
  49. abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null, $update = false);
  50. /**
  51. * Write the rendered template output to cache
  52. *
  53. * @param Smarty_Internal_Template $_template template object
  54. * @param string $content content to cache
  55. *
  56. * @return boolean success
  57. */
  58. abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content);
  59. /**
  60. * Return cached content
  61. *
  62. * @param Smarty_Internal_Template $_template template object
  63. *
  64. * @return null|string
  65. */
  66. public function getCachedContent(Smarty_Internal_Template $_template)
  67. {
  68. if ($_template->cached->handler->process($_template)) {
  69. ob_start();
  70. $unifunc = $_template->cached->unifunc;
  71. $unifunc($_template);
  72. return ob_get_clean();
  73. }
  74. return null;
  75. }
  76. /**
  77. * Empty cache
  78. *
  79. * @param Smarty $smarty Smarty object
  80. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  81. *
  82. * @return integer number of cache files deleted
  83. */
  84. abstract public function clearAll(Smarty $smarty, $exp_time = null);
  85. /**
  86. * Empty cache for a specific template
  87. *
  88. * @param Smarty $smarty Smarty object
  89. * @param string $resource_name template name
  90. * @param string $cache_id cache id
  91. * @param string $compile_id compile id
  92. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  93. *
  94. * @return integer number of cache files deleted
  95. */
  96. abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
  97. /**
  98. * @param Smarty $smarty
  99. * @param Smarty_Template_Cached $cached
  100. *
  101. * @return bool|null
  102. */
  103. public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
  104. {
  105. // theoretically locking_timeout should be checked against time_limit (max_execution_time)
  106. $start = microtime(true);
  107. $hadLock = null;
  108. while ($this->hasLock($smarty, $cached)) {
  109. $hadLock = true;
  110. if (microtime(true) - $start > $smarty->locking_timeout) {
  111. // abort waiting for lock release
  112. return false;
  113. }
  114. sleep(1);
  115. }
  116. return $hadLock;
  117. }
  118. /**
  119. * Check is cache is locked for this template
  120. *
  121. * @param Smarty $smarty
  122. * @param Smarty_Template_Cached $cached
  123. *
  124. * @return bool
  125. */
  126. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  127. {
  128. // check if lock exists
  129. return false;
  130. }
  131. /**
  132. * Lock cache for this template
  133. *
  134. * @param Smarty $smarty
  135. * @param Smarty_Template_Cached $cached
  136. *
  137. * @return bool
  138. */
  139. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  140. {
  141. // create lock
  142. return true;
  143. }
  144. /**
  145. * Unlock cache for this template
  146. *
  147. * @param Smarty $smarty
  148. * @param Smarty_Template_Cached $cached
  149. *
  150. * @return bool
  151. */
  152. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  153. {
  154. // release lock
  155. return true;
  156. }
  157. /**
  158. * Load Cache Resource Handler
  159. *
  160. * @param Smarty $smarty Smarty object
  161. * @param string $type name of the cache resource
  162. *
  163. * @throws SmartyException
  164. * @return Smarty_CacheResource Cache Resource Handler
  165. */
  166. public static function load(Smarty $smarty, $type = null)
  167. {
  168. if (!isset($type)) {
  169. $type = $smarty->caching_type;
  170. }
  171. // try smarty's cache
  172. if (isset($smarty->_cache['cacheresource_handlers'][$type])) {
  173. return $smarty->_cache['cacheresource_handlers'][$type];
  174. }
  175. // try registered resource
  176. if (isset($smarty->registered_cache_resources[$type])) {
  177. // do not cache these instances as they may vary from instance to instance
  178. return $smarty->_cache['cacheresource_handlers'][$type] = $smarty->registered_cache_resources[$type];
  179. }
  180. // try sysplugins dir
  181. if (isset(self::$sysplugins[$type])) {
  182. $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
  183. return $smarty->_cache['cacheresource_handlers'][$type] = new $cache_resource_class();
  184. }
  185. // try plugins dir
  186. $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
  187. if ($smarty->loadPlugin($cache_resource_class)) {
  188. return $smarty->_cache['cacheresource_handlers'][$type] = new $cache_resource_class();
  189. }
  190. // give up
  191. throw new SmartyException("Unable to load cache resource '{$type}'");
  192. }
  193. /**
  194. * Invalid Loaded Cache Files
  195. *
  196. * @param Smarty $smarty Smarty object
  197. */
  198. public function invalidLoadedCache(Smarty $smarty)
  199. {
  200. $smarty->_cache['isCached'] = array();
  201. if (isset($smarty->ext->_subtemplate)) {
  202. $smarty->ext->_subtemplate->tplObjects = array();
  203. }
  204. }
  205. }