TwoLevels.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage Zend_Cache_Backend
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Cache_Backend_ExtendedInterface
  23. */
  24. require_once 'Zend/Cache/Backend/ExtendedInterface.php';
  25. /**
  26. * @see Zend_Cache_Backend
  27. */
  28. require_once 'Zend/Cache/Backend.php';
  29. /**
  30. * @package Zend_Cache
  31. * @subpackage Zend_Cache_Backend
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
  36. {
  37. /**
  38. * Available options
  39. *
  40. * =====> (string) slow_backend :
  41. * - Slow backend name
  42. * - Must implement the Zend_Cache_Backend_ExtendedInterface
  43. * - Should provide a big storage
  44. *
  45. * =====> (string) fast_backend :
  46. * - Flow backend name
  47. * - Must implement the Zend_Cache_Backend_ExtendedInterface
  48. * - Must be much faster than slow_backend
  49. *
  50. * =====> (array) slow_backend_options :
  51. * - Slow backend options (see corresponding backend)
  52. *
  53. * =====> (array) fast_backend_options :
  54. * - Fast backend options (see corresponding backend)
  55. *
  56. * =====> (int) stats_update_factor :
  57. * - Disable / Tune the computation of the fast backend filling percentage
  58. * - When saving a record into cache :
  59. * 1 => systematic computation of the fast backend filling percentage
  60. * x (integer) > 1 => computation of the fast backend filling percentage randomly 1 times on x cache write
  61. *
  62. * =====> (boolean) slow_backend_custom_naming :
  63. * =====> (boolean) fast_backend_custom_naming :
  64. * =====> (boolean) slow_backend_autoload :
  65. * =====> (boolean) fast_backend_autoload :
  66. * - See Zend_Cache::factory() method
  67. *
  68. * =====> (boolean) auto_refresh_fast_cache
  69. * - If true, auto refresh the fast cache when a cache record is hit
  70. *
  71. * @var array available options
  72. */
  73. protected $_options = array(
  74. 'slow_backend' => 'File',
  75. 'fast_backend' => 'Apc',
  76. 'slow_backend_options' => array(),
  77. 'fast_backend_options' => array(),
  78. 'stats_update_factor' => 10,
  79. 'slow_backend_custom_naming' => false,
  80. 'fast_backend_custom_naming' => false,
  81. 'slow_backend_autoload' => false,
  82. 'fast_backend_autoload' => false,
  83. 'auto_refresh_fast_cache' => true
  84. );
  85. /**
  86. * Slow Backend
  87. *
  88. * @var Zend_Cache_Backend
  89. */
  90. private $_slowBackend;
  91. /**
  92. * Fast Backend
  93. *
  94. * @var Zend_Cache_Backend
  95. */
  96. private $_fastBackend;
  97. /**
  98. * Cache for the fast backend filling percentage
  99. *
  100. * @var int
  101. */
  102. private $_fastBackendFillingPercentage = null;
  103. /**
  104. * Constructor
  105. *
  106. * @param array $options Associative array of options
  107. * @throws Zend_Cache_Exception
  108. * @return void
  109. */
  110. public function __construct(array $options = array())
  111. {
  112. parent::__construct($options);
  113. if ($this->_options['slow_backend'] === null) {
  114. Zend_Cache::throwException('slow_backend option has to set');
  115. }
  116. if ($this->_options['fast_backend'] === null) {
  117. Zend_Cache::throwException('fast_backend option has to set');
  118. }
  119. $this->_slowBackend = Zend_Cache::_makeBackend($this->_options['slow_backend'], $this->_options['slow_backend_options'], $this->_options['slow_backend_custom_naming'], $this->_options['slow_backend_autoload']);
  120. $this->_fastBackend = Zend_Cache::_makeBackend($this->_options['fast_backend'], $this->_options['fast_backend_options'], $this->_options['fast_backend_custom_naming'], $this->_options['fast_backend_autoload']);
  121. if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_slowBackend))) {
  122. Zend_Cache::throwException('slow_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
  123. }
  124. if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_fastBackend))) {
  125. Zend_Cache::throwException('fast_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
  126. }
  127. $this->_slowBackend->setDirectives($this->_directives);
  128. $this->_fastBackend->setDirectives($this->_directives);
  129. }
  130. /**
  131. * Test if a cache is available or not (for the given id)
  132. *
  133. * @param string $id cache id
  134. * @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  135. */
  136. public function test($id)
  137. {
  138. $fastTest = $this->_fastBackend->test($id);
  139. if ($fastTest) {
  140. return $fastTest;
  141. } else {
  142. return $this->_slowBackend->test($id);
  143. }
  144. }
  145. /**
  146. * Save some string datas into a cache record
  147. *
  148. * Note : $data is always "string" (serialization is done by the
  149. * core not by the backend)
  150. *
  151. * @param string $data Datas to cache
  152. * @param string $id Cache id
  153. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  154. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  155. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  156. * @return boolean true if no problem
  157. */
  158. public function save($data, $id, $tags = array(), $specificLifetime = false, $priority = 8)
  159. {
  160. $usage = $this->_getFastFillingPercentage('saving');
  161. $boolFast = true;
  162. $lifetime = $this->getLifetime($specificLifetime);
  163. $preparedData = $this->_prepareData($data, $lifetime, $priority);
  164. if (($priority > 0) && (10 * $priority >= $usage)) {
  165. $fastLifetime = $this->_getFastLifetime($lifetime, $priority);
  166. $boolFast = $this->_fastBackend->save($preparedData, $id, array(), $fastLifetime);
  167. }
  168. $boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
  169. return ($boolFast && $boolSlow);
  170. }
  171. /**
  172. * Test if a cache is available for the given id and (if yes) return it (false else)
  173. *
  174. * Note : return value is always "string" (unserialization is done by the core not by the backend)
  175. *
  176. * @param string $id Cache id
  177. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  178. * @return string|false cached datas
  179. */
  180. public function load($id, $doNotTestCacheValidity = false)
  181. {
  182. $res = $this->_fastBackend->load($id, $doNotTestCacheValidity);
  183. if ($res === false) {
  184. $res = $this->_slowBackend->load($id, $doNotTestCacheValidity);
  185. if ($res === false) {
  186. // there is no cache at all for this id
  187. return false;
  188. }
  189. }
  190. $array = unserialize($res);
  191. // maybe, we have to refresh the fast cache ?
  192. if ($this->_options['auto_refresh_fast_cache']) {
  193. if ($array['priority'] == 10) {
  194. // no need to refresh the fast cache with priority = 10
  195. return $array['data'];
  196. }
  197. $newFastLifetime = $this->_getFastLifetime($array['lifetime'], $array['priority'], time() - $array['expire']);
  198. // we have the time to refresh the fast cache
  199. $usage = $this->_getFastFillingPercentage('loading');
  200. if (($array['priority'] > 0) && (10 * $array['priority'] >= $usage)) {
  201. // we can refresh the fast cache
  202. $preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
  203. $this->_fastBackend->save($preparedData, $id, array(), $newFastLifetime);
  204. }
  205. }
  206. return $array['data'];
  207. }
  208. /**
  209. * Remove a cache record
  210. *
  211. * @param string $id Cache id
  212. * @return boolean True if no problem
  213. */
  214. public function remove($id)
  215. {
  216. $this->_fastBackend->remove($id);
  217. return $this->_slowBackend->remove($id);
  218. }
  219. /**
  220. * Clean some cache records
  221. *
  222. * Available modes are :
  223. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  224. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  225. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  226. * ($tags can be an array of strings or a single string)
  227. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  228. * ($tags can be an array of strings or a single string)
  229. * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
  230. * ($tags can be an array of strings or a single string)
  231. *
  232. * @param string $mode Clean mode
  233. * @param array $tags Array of tags
  234. * @throws Zend_Cache_Exception
  235. * @return boolean true if no problem
  236. */
  237. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  238. {
  239. switch($mode) {
  240. case Zend_Cache::CLEANING_MODE_ALL:
  241. $boolFast = $this->_fastBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
  242. $boolSlow = $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_ALL);
  243. return $boolFast && $boolSlow;
  244. break;
  245. case Zend_Cache::CLEANING_MODE_OLD:
  246. return $this->_slowBackend->clean(Zend_Cache::CLEANING_MODE_OLD);
  247. case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  248. $ids = $this->_slowBackend->getIdsMatchingTags($tags);
  249. $res = true;
  250. foreach ($ids as $id) {
  251. $res = $res && $this->_slowBackend->remove($id) && $this->_fastBackend->remove($id);
  252. }
  253. return $res;
  254. break;
  255. case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  256. $ids = $this->_slowBackend->getIdsNotMatchingTags($tags);
  257. $res = true;
  258. foreach ($ids as $id) {
  259. $res = $res && $this->_slowBackend->remove($id) && $this->_fastBackend->remove($id);
  260. }
  261. return $res;
  262. break;
  263. case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  264. $ids = $this->_slowBackend->getIdsMatchingAnyTags($tags);
  265. $res = true;
  266. foreach ($ids as $id) {
  267. $res = $res && $this->_slowBackend->remove($id) && $this->_fastBackend->remove($id);
  268. }
  269. return $res;
  270. break;
  271. default:
  272. Zend_Cache::throwException('Invalid mode for clean() method');
  273. break;
  274. }
  275. }
  276. /**
  277. * Return an array of stored cache ids
  278. *
  279. * @return array array of stored cache ids (string)
  280. */
  281. public function getIds()
  282. {
  283. return $this->_slowBackend->getIds();
  284. }
  285. /**
  286. * Return an array of stored tags
  287. *
  288. * @return array array of stored tags (string)
  289. */
  290. public function getTags()
  291. {
  292. return $this->_slowBackend->getTags();
  293. }
  294. /**
  295. * Return an array of stored cache ids which match given tags
  296. *
  297. * In case of multiple tags, a logical AND is made between tags
  298. *
  299. * @param array $tags array of tags
  300. * @return array array of matching cache ids (string)
  301. */
  302. public function getIdsMatchingTags($tags = array())
  303. {
  304. return $this->_slowBackend->getIdsMatchingTags($tags);
  305. }
  306. /**
  307. * Return an array of stored cache ids which don't match given tags
  308. *
  309. * In case of multiple tags, a logical OR is made between tags
  310. *
  311. * @param array $tags array of tags
  312. * @return array array of not matching cache ids (string)
  313. */
  314. public function getIdsNotMatchingTags($tags = array())
  315. {
  316. return $this->_slowBackend->getIdsNotMatchingTags($tags);
  317. }
  318. /**
  319. * Return an array of stored cache ids which match any given tags
  320. *
  321. * In case of multiple tags, a logical AND is made between tags
  322. *
  323. * @param array $tags array of tags
  324. * @return array array of any matching cache ids (string)
  325. */
  326. public function getIdsMatchingAnyTags($tags = array())
  327. {
  328. return $this->_slowBackend->getIdsMatchingAnyTags($tags);
  329. }
  330. /**
  331. * Return the filling percentage of the backend storage
  332. *
  333. * @return int integer between 0 and 100
  334. */
  335. public function getFillingPercentage()
  336. {
  337. return $this->_slowBackend->getFillingPercentage();
  338. }
  339. /**
  340. * Return an array of metadatas for the given cache id
  341. *
  342. * The array must include these keys :
  343. * - expire : the expire timestamp
  344. * - tags : a string array of tags
  345. * - mtime : timestamp of last modification time
  346. *
  347. * @param string $id cache id
  348. * @return array array of metadatas (false if the cache id is not found)
  349. */
  350. public function getMetadatas($id)
  351. {
  352. return $this->_slowBackend->getMetadatas($id);
  353. }
  354. /**
  355. * Give (if possible) an extra lifetime to the given cache id
  356. *
  357. * @param string $id cache id
  358. * @param int $extraLifetime
  359. * @return boolean true if ok
  360. */
  361. public function touch($id, $extraLifetime)
  362. {
  363. return $this->_slowBackend->touch($id, $extraLifetime);
  364. }
  365. /**
  366. * Return an associative array of capabilities (booleans) of the backend
  367. *
  368. * The array must include these keys :
  369. * - automatic_cleaning (is automating cleaning necessary)
  370. * - tags (are tags supported)
  371. * - expired_read (is it possible to read expired cache records
  372. * (for doNotTestCacheValidity option for example))
  373. * - priority does the backend deal with priority when saving
  374. * - infinite_lifetime (is infinite lifetime can work with this backend)
  375. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  376. *
  377. * @return array associative of with capabilities
  378. */
  379. public function getCapabilities()
  380. {
  381. $slowBackendCapabilities = $this->_slowBackend->getCapabilities();
  382. return array(
  383. 'automatic_cleaning' => $slowBackendCapabilities['automatic_cleaning'],
  384. 'tags' => $slowBackendCapabilities['tags'],
  385. 'expired_read' => $slowBackendCapabilities['expired_read'],
  386. 'priority' => $slowBackendCapabilities['priority'],
  387. 'infinite_lifetime' => $slowBackendCapabilities['infinite_lifetime'],
  388. 'get_list' => $slowBackendCapabilities['get_list']
  389. );
  390. }
  391. /**
  392. * Prepare a serialized array to store datas and metadatas informations
  393. *
  394. * @param string $data data to store
  395. * @param int $lifetime original lifetime
  396. * @param int $priority priority
  397. * @return string serialize array to store into cache
  398. */
  399. private function _prepareData($data, $lifetime, $priority)
  400. {
  401. $lt = $lifetime;
  402. if ($lt === null) {
  403. $lt = 9999999999;
  404. }
  405. return serialize(array(
  406. 'data' => $data,
  407. 'lifetime' => $lifetime,
  408. 'expire' => time() + $lt,
  409. 'priority' => $priority
  410. ));
  411. }
  412. /**
  413. * Compute and return the lifetime for the fast backend
  414. *
  415. * @param int $lifetime original lifetime
  416. * @param int $priority priority
  417. * @param int $maxLifetime maximum lifetime
  418. * @return int lifetime for the fast backend
  419. */
  420. private function _getFastLifetime($lifetime, $priority, $maxLifetime = null)
  421. {
  422. if ($lifetime === null) {
  423. // if lifetime is null, we have an infinite lifetime
  424. // we need to use arbitrary lifetimes
  425. $fastLifetime = (int) (2592000 / (11 - $priority));
  426. } else {
  427. $fastLifetime = (int) ($lifetime / (11 - $priority));
  428. }
  429. if (($maxLifetime !== null) && ($maxLifetime >= 0)) {
  430. if ($fastLifetime > $maxLifetime) {
  431. return $maxLifetime;
  432. }
  433. }
  434. return $fastLifetime;
  435. }
  436. /**
  437. * PUBLIC METHOD FOR UNIT TESTING ONLY !
  438. *
  439. * Force a cache record to expire
  440. *
  441. * @param string $id cache id
  442. */
  443. public function ___expire($id)
  444. {
  445. $this->_fastBackend->remove($id);
  446. $this->_slowBackend->___expire($id);
  447. }
  448. private function _getFastFillingPercentage($mode)
  449. {
  450. if ($mode == 'saving') {
  451. // mode saving
  452. if ($this->_fastBackendFillingPercentage === null) {
  453. $this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
  454. } else {
  455. $rand = rand(1, $this->_options['stats_update_factor']);
  456. if ($rand == 1) {
  457. // we force a refresh
  458. $this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
  459. }
  460. }
  461. } else {
  462. // mode loading
  463. // we compute the percentage only if it's not available in cache
  464. if ($this->_fastBackendFillingPercentage === null) {
  465. $this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
  466. }
  467. }
  468. return $this->_fastBackendFillingPercentage;
  469. }
  470. }