smarty_cacheresource.php 6.5 KB

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