TwoLevels.php 20 KB

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