Core.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. class Zend_Cache_Core
  27. {
  28. /**
  29. * Messages
  30. */
  31. const BACKEND_NOT_SUPPORTS_TAG = 'tags are not supported by the current backend';
  32. const BACKEND_NOT_IMPLEMENTS_EXTENDED_IF = 'Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available';
  33. /**
  34. * Backend Object
  35. *
  36. * @var object $_backend
  37. */
  38. protected $_backend = null;
  39. /**
  40. * Available options
  41. *
  42. * ====> (boolean) write_control :
  43. * - Enable / disable write control (the cache is read just after writing to detect corrupt entries)
  44. * - Enable write control will lightly slow the cache writing but not the cache reading
  45. * Write control can detect some corrupt cache files but maybe it's not a perfect control
  46. *
  47. * ====> (boolean) caching :
  48. * - Enable / disable caching
  49. * (can be very useful for the debug of cached scripts)
  50. *
  51. * =====> (string) cache_id_prefix :
  52. * - prefix for cache ids (namespace)
  53. *
  54. * ====> (boolean) automatic_serialization :
  55. * - Enable / disable automatic serialization
  56. * - It can be used to save directly datas which aren't strings (but it's slower)
  57. *
  58. * ====> (int) automatic_cleaning_factor :
  59. * - Disable / Tune the automatic cleaning process
  60. * - The automatic cleaning process destroy too old (for the given life time)
  61. * cache files when a new cache file is written :
  62. * 0 => no automatic cache cleaning
  63. * 1 => systematic cache cleaning
  64. * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
  65. *
  66. * ====> (int) lifetime :
  67. * - Cache lifetime (in seconds)
  68. * - If null, the cache is valid forever.
  69. *
  70. * ====> (boolean) logging :
  71. * - If set to true, logging is activated (but the system is slower)
  72. *
  73. * ====> (boolean) ignore_user_abort
  74. * - If set to true, the core will set the ignore_user_abort PHP flag inside the
  75. * save() method to avoid cache corruptions in some cases (default false)
  76. *
  77. * @var array $_options available options
  78. */
  79. protected $_options = array(
  80. 'write_control' => true,
  81. 'caching' => true,
  82. 'cache_id_prefix' => null,
  83. 'automatic_serialization' => false,
  84. 'automatic_cleaning_factor' => 10,
  85. 'lifetime' => 3600,
  86. 'logging' => false,
  87. 'logger' => null,
  88. 'ignore_user_abort' => false
  89. );
  90. /**
  91. * Array of options which have to be transfered to backend
  92. *
  93. * @var array $_directivesList
  94. */
  95. protected static $_directivesList = array('lifetime', 'logging', 'logger');
  96. /**
  97. * Not used for the core, just a sort a hint to get a common setOption() method (for the core and for frontends)
  98. *
  99. * @var array $_specificOptions
  100. */
  101. protected $_specificOptions = array();
  102. /**
  103. * Last used cache id
  104. *
  105. * @var string $_lastId
  106. */
  107. private $_lastId = null;
  108. /**
  109. * True if the backend implements Zend_Cache_Backend_ExtendedInterface
  110. *
  111. * @var boolean $_extendedBackend
  112. */
  113. protected $_extendedBackend = false;
  114. /**
  115. * Array of capabilities of the backend (only if it implements Zend_Cache_Backend_ExtendedInterface)
  116. *
  117. * @var array
  118. */
  119. protected $_backendCapabilities = array();
  120. /**
  121. * Constructor
  122. *
  123. * @param array|Zend_Config $options Associative array of options or Zend_Config instance
  124. * @throws Zend_Cache_Exception
  125. * @return void
  126. */
  127. public function __construct($options = array())
  128. {
  129. if ($options instanceof Zend_Config) {
  130. $options = $options->toArray();
  131. }
  132. if (!is_array($options)) {
  133. Zend_Cache::throwException("Options passed were not an array"
  134. . " or Zend_Config instance.");
  135. }
  136. while (list($name, $value) = each($options)) {
  137. $this->setOption($name, $value);
  138. }
  139. $this->_loggerSanity();
  140. }
  141. /**
  142. * Set options using an instance of type Zend_Config
  143. *
  144. * @param Zend_Config $config
  145. * @return Zend_Cache_Core
  146. */
  147. public function setConfig(Zend_Config $config)
  148. {
  149. $options = $config->toArray();
  150. while (list($name, $value) = each($options)) {
  151. $this->setOption($name, $value);
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Set the backend
  157. *
  158. * @param object $backendObject
  159. * @throws Zend_Cache_Exception
  160. * @return void
  161. */
  162. public function setBackend(Zend_Cache_Backend $backendObject)
  163. {
  164. $this->_backend= $backendObject;
  165. // some options (listed in $_directivesList) have to be given
  166. // to the backend too (even if they are not "backend specific")
  167. $directives = array();
  168. foreach (Zend_Cache_Core::$_directivesList as $directive) {
  169. $directives[$directive] = $this->_options[$directive];
  170. }
  171. $this->_backend->setDirectives($directives);
  172. if (in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_backend))) {
  173. $this->_extendedBackend = true;
  174. $this->_backendCapabilities = $this->_backend->getCapabilities();
  175. }
  176. }
  177. /**
  178. * Returns the backend
  179. *
  180. * @return object backend object
  181. */
  182. public function getBackend()
  183. {
  184. return $this->_backend;
  185. }
  186. /**
  187. * Public frontend to set an option
  188. *
  189. * There is an additional validation (relatively to the protected _setOption method)
  190. *
  191. * @param string $name Name of the option
  192. * @param mixed $value Value of the option
  193. * @throws Zend_Cache_Exception
  194. * @return void
  195. */
  196. public function setOption($name, $value)
  197. {
  198. if (!is_string($name)) {
  199. Zend_Cache::throwException("Incorrect option name : $name");
  200. }
  201. $name = strtolower($name);
  202. if (array_key_exists($name, $this->_options)) {
  203. // This is a Core option
  204. $this->_setOption($name, $value);
  205. return;
  206. }
  207. if (array_key_exists($name, $this->_specificOptions)) {
  208. // This a specic option of this frontend
  209. $this->_specificOptions[$name] = $value;
  210. return;
  211. }
  212. }
  213. /**
  214. * Public frontend to get an option value
  215. *
  216. * @param string $name Name of the option
  217. * @throws Zend_Cache_Exception
  218. * @return mixed option value
  219. */
  220. public function getOption($name)
  221. {
  222. if (is_string($name)) {
  223. $name = strtolower($name);
  224. if (array_key_exists($name, $this->_options)) {
  225. // This is a Core option
  226. return $this->_options[$name];
  227. }
  228. if (array_key_exists($name, $this->_specificOptions)) {
  229. // This a specic option of this frontend
  230. return $this->_specificOptions[$name];
  231. }
  232. }
  233. Zend_Cache::throwException("Incorrect option name : $name");
  234. }
  235. /**
  236. * Set an option
  237. *
  238. * @param string $name Name of the option
  239. * @param mixed $value Value of the option
  240. * @throws Zend_Cache_Exception
  241. * @return void
  242. */
  243. private function _setOption($name, $value)
  244. {
  245. if (!is_string($name) || !array_key_exists($name, $this->_options)) {
  246. Zend_Cache::throwException("Incorrect option name : $name");
  247. }
  248. $this->_options[$name] = $value;
  249. }
  250. /**
  251. * Force a new lifetime
  252. *
  253. * The new value is set for the core/frontend but for the backend too (directive)
  254. *
  255. * @param int $newLifetime New lifetime (in seconds)
  256. * @return void
  257. */
  258. public function setLifetime($newLifetime)
  259. {
  260. $this->_options['lifetime'] = $newLifetime;
  261. $this->_backend->setDirectives(array(
  262. 'lifetime' => $newLifetime
  263. ));
  264. }
  265. /**
  266. * Test if a cache is available for the given id and (if yes) return it (false else)
  267. *
  268. * @param string $id Cache id
  269. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  270. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  271. * @return mixed|false Cached datas
  272. */
  273. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  274. {
  275. if (!$this->_options['caching']) {
  276. return false;
  277. }
  278. $id = $this->_id($id); // cache id may need prefix
  279. $this->_lastId = $id;
  280. self::_validateIdOrTag($id);
  281. $data = $this->_backend->load($id, $doNotTestCacheValidity);
  282. if ($data===false) {
  283. // no cache available
  284. return false;
  285. }
  286. if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
  287. // we need to unserialize before sending the result
  288. return unserialize($data);
  289. }
  290. return $data;
  291. }
  292. /**
  293. * Test if a cache is available for the given id
  294. *
  295. * @param string $id Cache id
  296. * @return int|false Last modified time of cache entry if it is available, false otherwise
  297. */
  298. public function test($id)
  299. {
  300. if (!$this->_options['caching']) {
  301. return false;
  302. }
  303. $id = $this->_id($id); // cache id may need prefix
  304. self::_validateIdOrTag($id);
  305. $this->_lastId = $id;
  306. return $this->_backend->test($id);
  307. }
  308. /**
  309. * Save some data in a cache
  310. *
  311. * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
  312. * @param string $id Cache id (if not set, the last cache id will be used)
  313. * @param array $tags Cache tags
  314. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  315. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  316. * @throws Zend_Cache_Exception
  317. * @return boolean True if no problem
  318. */
  319. public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
  320. {
  321. if (!$this->_options['caching']) {
  322. return true;
  323. }
  324. if ($id === null) {
  325. $id = $this->_lastId;
  326. } else {
  327. $id = $this->_id($id);
  328. }
  329. self::_validateIdOrTag($id);
  330. self::_validateTagsArray($tags);
  331. if ($this->_options['automatic_serialization']) {
  332. // we need to serialize datas before storing them
  333. $data = serialize($data);
  334. } else {
  335. if (!is_string($data)) {
  336. Zend_Cache::throwException("Datas must be string or set automatic_serialization = true");
  337. }
  338. }
  339. // automatic cleaning
  340. if ($this->_options['automatic_cleaning_factor'] > 0) {
  341. $rand = rand(1, $this->_options['automatic_cleaning_factor']);
  342. if ($rand==1) {
  343. if ($this->_extendedBackend) {
  344. // New way
  345. if ($this->_backendCapabilities['automatic_cleaning']) {
  346. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  347. } else {
  348. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  349. }
  350. } else {
  351. // Deprecated way (will be removed in next major version)
  352. if (method_exists($this->_backend, 'isAutomaticCleaningAvailable') && ($this->_backend->isAutomaticCleaningAvailable())) {
  353. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  354. } else {
  355. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  356. }
  357. }
  358. }
  359. }
  360. if ($this->_options['ignore_user_abort']) {
  361. $abort = ignore_user_abort(true);
  362. }
  363. if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) {
  364. $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority);
  365. } else {
  366. $result = $this->_backend->save($data, $id, $tags, $specificLifetime);
  367. }
  368. if ($this->_options['ignore_user_abort']) {
  369. ignore_user_abort($abort);
  370. }
  371. if (!$result) {
  372. // maybe the cache is corrupted, so we remove it !
  373. if ($this->_options['logging']) {
  374. $this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)");
  375. }
  376. $this->remove($id);
  377. return false;
  378. }
  379. if ($this->_options['write_control']) {
  380. $data2 = $this->_backend->load($id, true);
  381. if ($data!=$data2) {
  382. $this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match');
  383. $this->_backend->remove($id);
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389. /**
  390. * Remove a cache
  391. *
  392. * @param string $id Cache id to remove
  393. * @return boolean True if ok
  394. */
  395. public function remove($id)
  396. {
  397. if (!$this->_options['caching']) {
  398. return true;
  399. }
  400. $id = $this->_id($id); // cache id may need prefix
  401. self::_validateIdOrTag($id);
  402. return $this->_backend->remove($id);
  403. }
  404. /**
  405. * Clean cache entries
  406. *
  407. * Available modes are :
  408. * 'all' (default) => remove all cache entries ($tags is not used)
  409. * 'old' => remove too old cache entries ($tags is not used)
  410. * 'matchingTag' => remove cache entries matching all given tags
  411. * ($tags can be an array of strings or a single string)
  412. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  413. * ($tags can be an array of strings or a single string)
  414. * 'matchingAnyTag' => remove cache entries matching any given tags
  415. * ($tags can be an array of strings or a single string)
  416. *
  417. * @param string $mode
  418. * @param array|string $tags
  419. * @throws Zend_Cache_Exception
  420. * @return boolean True if ok
  421. */
  422. public function clean($mode = 'all', $tags = array())
  423. {
  424. if (!$this->_options['caching']) {
  425. return true;
  426. }
  427. if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
  428. Zend_Cache::CLEANING_MODE_OLD,
  429. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  430. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  431. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
  432. Zend_Cache::throwException('Invalid cleaning mode');
  433. }
  434. self::_validateTagsArray($tags);
  435. return $this->_backend->clean($mode, $tags);
  436. }
  437. /**
  438. * Return an array of stored cache ids which match given tags
  439. *
  440. * In case of multiple tags, a logical AND is made between tags
  441. *
  442. * @param array $tags array of tags
  443. * @return array array of matching cache ids (string)
  444. */
  445. public function getIdsMatchingTags($tags = array())
  446. {
  447. if (!$this->_extendedBackend) {
  448. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  449. }
  450. if (!($this->_backendCapabilities['tags'])) {
  451. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  452. }
  453. return $this->_backend->getIdsMatchingTags($tags);
  454. }
  455. /**
  456. * Return an array of stored cache ids which don't match given tags
  457. *
  458. * In case of multiple tags, a logical OR is made between tags
  459. *
  460. * @param array $tags array of tags
  461. * @return array array of not matching cache ids (string)
  462. */
  463. public function getIdsNotMatchingTags($tags = array())
  464. {
  465. if (!$this->_extendedBackend) {
  466. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  467. }
  468. if (!($this->_backendCapabilities['tags'])) {
  469. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  470. }
  471. return $this->_backend->getIdsNotMatchingTags($tags);
  472. }
  473. /**
  474. * Return an array of stored cache ids which match any given tags
  475. *
  476. * In case of multiple tags, a logical OR is made between tags
  477. *
  478. * @param array $tags array of tags
  479. * @return array array of matching any cache ids (string)
  480. */
  481. public function getIdsMatchingAnyTags($tags = array())
  482. {
  483. if (!$this->_extendedBackend) {
  484. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  485. }
  486. if (!($this->_backendCapabilities['tags'])) {
  487. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  488. }
  489. return $this->_backend->getIdsMatchingAnyTags($tags);
  490. }
  491. /**
  492. * Return an array of stored cache ids
  493. *
  494. * @return array array of stored cache ids (string)
  495. */
  496. public function getIds()
  497. {
  498. if (!$this->_extendedBackend) {
  499. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  500. }
  501. $array = $this->_backend->getIds();
  502. if ((!isset($this->_options['cache_id_prefix'])) || ($this->_options['cache_id_prefix'] == '')) return $array;
  503. // we need to remove cache_id_prefix from ids (see #ZF-6178)
  504. $res = array();
  505. while (list(,$id) = each($array)) {
  506. if (strpos($id, $this->_options['cache_id_prefix']) === 0) {
  507. $res[] = preg_replace("~^{$this->_options['cache_id_prefix']}~", '', $id);
  508. } else {
  509. $res[] = $id;
  510. }
  511. }
  512. return $res;
  513. }
  514. /**
  515. * Return an array of stored tags
  516. *
  517. * @return array array of stored tags (string)
  518. */
  519. public function getTags()
  520. {
  521. if (!$this->_extendedBackend) {
  522. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  523. }
  524. if (!($this->_backendCapabilities['tags'])) {
  525. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  526. }
  527. return $this->_backend->getTags();
  528. }
  529. /**
  530. * Return the filling percentage of the backend storage
  531. *
  532. * @return int integer between 0 and 100
  533. */
  534. public function getFillingPercentage()
  535. {
  536. if (!$this->_extendedBackend) {
  537. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  538. }
  539. return $this->_backend->getFillingPercentage();
  540. }
  541. /**
  542. * Return an array of metadatas for the given cache id
  543. *
  544. * The array will include these keys :
  545. * - expire : the expire timestamp
  546. * - tags : a string array of tags
  547. * - mtime : timestamp of last modification time
  548. *
  549. * @param string $id cache id
  550. * @return array array of metadatas (false if the cache id is not found)
  551. */
  552. public function getMetadatas($id)
  553. {
  554. if (!$this->_extendedBackend) {
  555. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  556. }
  557. $id = $this->_id($id); // cache id may need prefix
  558. return $this->_backend->getMetadatas($id);
  559. }
  560. /**
  561. * Give (if possible) an extra lifetime to the given cache id
  562. *
  563. * @param string $id cache id
  564. * @param int $extraLifetime
  565. * @return boolean true if ok
  566. */
  567. public function touch($id, $extraLifetime)
  568. {
  569. if (!$this->_extendedBackend) {
  570. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  571. }
  572. $id = $this->_id($id); // cache id may need prefix
  573. return $this->_backend->touch($id, $extraLifetime);
  574. }
  575. /**
  576. * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
  577. *
  578. * Throw an exception if a problem is found
  579. *
  580. * @param string $string Cache id or tag
  581. * @throws Zend_Cache_Exception
  582. * @return void
  583. */
  584. protected static function _validateIdOrTag($string)
  585. {
  586. if (!is_string($string)) {
  587. Zend_Cache::throwException('Invalid id or tag : must be a string');
  588. }
  589. if (substr($string, 0, 9) == 'internal-') {
  590. Zend_Cache::throwException('"internal-*" ids or tags are reserved');
  591. }
  592. if (!preg_match('~^[\w]+$~D', $string)) {
  593. Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
  594. }
  595. }
  596. /**
  597. * Validate a tags array (security, reliable filenames, reserved prefixes...)
  598. *
  599. * Throw an exception if a problem is found
  600. *
  601. * @param array $tags Array of tags
  602. * @throws Zend_Cache_Exception
  603. * @return void
  604. */
  605. protected static function _validateTagsArray($tags)
  606. {
  607. if (!is_array($tags)) {
  608. Zend_Cache::throwException('Invalid tags array : must be an array');
  609. }
  610. foreach($tags as $tag) {
  611. self::_validateIdOrTag($tag);
  612. }
  613. reset($tags);
  614. }
  615. /**
  616. * Make sure if we enable logging that the Zend_Log class
  617. * is available.
  618. * Create a default log object if none is set.
  619. *
  620. * @throws Zend_Cache_Exception
  621. * @return void
  622. */
  623. protected function _loggerSanity()
  624. {
  625. if (!isset($this->_options['logging']) || !$this->_options['logging']) {
  626. return;
  627. }
  628. if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
  629. return;
  630. }
  631. // Create a default logger to the standard output stream
  632. require_once 'Zend/Log/Writer/Stream.php';
  633. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  634. $this->_options['logger'] = $logger;
  635. }
  636. /**
  637. * Log a message at the WARN (4) priority.
  638. *
  639. * @param string $message
  640. * @throws Zend_Cache_Exception
  641. * @return void
  642. */
  643. protected function _log($message, $priority = 4)
  644. {
  645. if (!$this->_options['logging']) {
  646. return;
  647. }
  648. if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
  649. Zend_Cache::throwException('Logging is enabled but logger is not set');
  650. }
  651. $logger = $this->_options['logger'];
  652. $logger->log($message, $priority);
  653. }
  654. /**
  655. * Make and return a cache id
  656. *
  657. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  658. *
  659. * @param string $id Cache id
  660. * @return string Cache id (with or without prefix)
  661. */
  662. protected function _id($id)
  663. {
  664. if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
  665. return $this->_options['cache_id_prefix'] . $id; // return with prefix
  666. }
  667. return $id; // no prefix, just return the $id passed
  668. }
  669. }