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

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