2
0

Core.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. if ($name == 'lifetime' && empty($value)) {
  249. $value = null;
  250. }
  251. $this->_options[$name] = $value;
  252. }
  253. /**
  254. * Force a new lifetime
  255. *
  256. * The new value is set for the core/frontend but for the backend too (directive)
  257. *
  258. * @param int $newLifetime New lifetime (in seconds)
  259. * @return void
  260. */
  261. public function setLifetime($newLifetime)
  262. {
  263. $this->_options['lifetime'] = $newLifetime;
  264. $this->_backend->setDirectives(array(
  265. 'lifetime' => $newLifetime
  266. ));
  267. }
  268. /**
  269. * Test if a cache is available for the given id and (if yes) return it (false else)
  270. *
  271. * @param string $id Cache id
  272. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  273. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  274. * @return mixed|false Cached datas
  275. */
  276. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  277. {
  278. if (!$this->_options['caching']) {
  279. return false;
  280. }
  281. $id = $this->_id($id); // cache id may need prefix
  282. $this->_lastId = $id;
  283. self::_validateIdOrTag($id);
  284. $data = $this->_backend->load($id, $doNotTestCacheValidity);
  285. if ($data===false) {
  286. // no cache available
  287. return false;
  288. }
  289. if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
  290. // we need to unserialize before sending the result
  291. return unserialize($data);
  292. }
  293. return $data;
  294. }
  295. /**
  296. * Test if a cache is available for the given id
  297. *
  298. * @param string $id Cache id
  299. * @return int|false Last modified time of cache entry if it is available, false otherwise
  300. */
  301. public function test($id)
  302. {
  303. if (!$this->_options['caching']) {
  304. return false;
  305. }
  306. $id = $this->_id($id); // cache id may need prefix
  307. self::_validateIdOrTag($id);
  308. $this->_lastId = $id;
  309. return $this->_backend->test($id);
  310. }
  311. /**
  312. * Save some data in a cache
  313. *
  314. * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
  315. * @param string $id Cache id (if not set, the last cache id will be used)
  316. * @param array $tags Cache tags
  317. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  318. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  319. * @throws Zend_Cache_Exception
  320. * @return boolean True if no problem
  321. */
  322. public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
  323. {
  324. if (!$this->_options['caching']) {
  325. return true;
  326. }
  327. if ($id === null) {
  328. $id = $this->_lastId;
  329. } else {
  330. $id = $this->_id($id);
  331. }
  332. self::_validateIdOrTag($id);
  333. self::_validateTagsArray($tags);
  334. if ($this->_options['automatic_serialization']) {
  335. // we need to serialize datas before storing them
  336. $data = serialize($data);
  337. } else {
  338. if (!is_string($data)) {
  339. Zend_Cache::throwException("Datas must be string or set automatic_serialization = true");
  340. }
  341. }
  342. // automatic cleaning
  343. if ($this->_options['automatic_cleaning_factor'] > 0) {
  344. $rand = rand(1, $this->_options['automatic_cleaning_factor']);
  345. if ($rand==1) {
  346. if ($this->_extendedBackend) {
  347. // New way
  348. if ($this->_backendCapabilities['automatic_cleaning']) {
  349. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  350. } else {
  351. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  352. }
  353. } else {
  354. // Deprecated way (will be removed in next major version)
  355. if (method_exists($this->_backend, 'isAutomaticCleaningAvailable') && ($this->_backend->isAutomaticCleaningAvailable())) {
  356. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  357. } else {
  358. $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
  359. }
  360. }
  361. }
  362. }
  363. if ($this->_options['ignore_user_abort']) {
  364. $abort = ignore_user_abort(true);
  365. }
  366. if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) {
  367. $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority);
  368. } else {
  369. $result = $this->_backend->save($data, $id, $tags, $specificLifetime);
  370. }
  371. if ($this->_options['ignore_user_abort']) {
  372. ignore_user_abort($abort);
  373. }
  374. if (!$result) {
  375. // maybe the cache is corrupted, so we remove it !
  376. if ($this->_options['logging']) {
  377. $this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)");
  378. }
  379. $this->remove($id);
  380. return false;
  381. }
  382. if ($this->_options['write_control']) {
  383. $data2 = $this->_backend->load($id, true);
  384. if ($data!=$data2) {
  385. $this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match');
  386. $this->_backend->remove($id);
  387. return false;
  388. }
  389. }
  390. return true;
  391. }
  392. /**
  393. * Remove a cache
  394. *
  395. * @param string $id Cache id to remove
  396. * @return boolean True if ok
  397. */
  398. public function remove($id)
  399. {
  400. if (!$this->_options['caching']) {
  401. return true;
  402. }
  403. $id = $this->_id($id); // cache id may need prefix
  404. self::_validateIdOrTag($id);
  405. return $this->_backend->remove($id);
  406. }
  407. /**
  408. * Clean cache entries
  409. *
  410. * Available modes are :
  411. * 'all' (default) => remove all cache entries ($tags is not used)
  412. * 'old' => remove too old cache entries ($tags is not used)
  413. * 'matchingTag' => remove cache entries matching all given tags
  414. * ($tags can be an array of strings or a single string)
  415. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  416. * ($tags can be an array of strings or a single string)
  417. * 'matchingAnyTag' => remove cache entries matching any given tags
  418. * ($tags can be an array of strings or a single string)
  419. *
  420. * @param string $mode
  421. * @param array|string $tags
  422. * @throws Zend_Cache_Exception
  423. * @return boolean True if ok
  424. */
  425. public function clean($mode = 'all', $tags = array())
  426. {
  427. if (!$this->_options['caching']) {
  428. return true;
  429. }
  430. if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
  431. Zend_Cache::CLEANING_MODE_OLD,
  432. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  433. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  434. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
  435. Zend_Cache::throwException('Invalid cleaning mode');
  436. }
  437. self::_validateTagsArray($tags);
  438. return $this->_backend->clean($mode, $tags);
  439. }
  440. /**
  441. * Return an array of stored cache ids which match given tags
  442. *
  443. * In case of multiple tags, a logical AND is made between tags
  444. *
  445. * @param array $tags array of tags
  446. * @return array array of matching cache ids (string)
  447. */
  448. public function getIdsMatchingTags($tags = array())
  449. {
  450. if (!$this->_extendedBackend) {
  451. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  452. }
  453. if (!($this->_backendCapabilities['tags'])) {
  454. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  455. }
  456. return $this->_backend->getIdsMatchingTags($tags);
  457. }
  458. /**
  459. * Return an array of stored cache ids which don't match given tags
  460. *
  461. * In case of multiple tags, a logical OR is made between tags
  462. *
  463. * @param array $tags array of tags
  464. * @return array array of not matching cache ids (string)
  465. */
  466. public function getIdsNotMatchingTags($tags = array())
  467. {
  468. if (!$this->_extendedBackend) {
  469. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  470. }
  471. if (!($this->_backendCapabilities['tags'])) {
  472. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  473. }
  474. return $this->_backend->getIdsNotMatchingTags($tags);
  475. }
  476. /**
  477. * Return an array of stored cache ids which match any given tags
  478. *
  479. * In case of multiple tags, a logical OR is made between tags
  480. *
  481. * @param array $tags array of tags
  482. * @return array array of matching any cache ids (string)
  483. */
  484. public function getIdsMatchingAnyTags($tags = array())
  485. {
  486. if (!$this->_extendedBackend) {
  487. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  488. }
  489. if (!($this->_backendCapabilities['tags'])) {
  490. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  491. }
  492. return $this->_backend->getIdsMatchingAnyTags($tags);
  493. }
  494. /**
  495. * Return an array of stored cache ids
  496. *
  497. * @return array array of stored cache ids (string)
  498. */
  499. public function getIds()
  500. {
  501. if (!$this->_extendedBackend) {
  502. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  503. }
  504. $array = $this->_backend->getIds();
  505. if ((!isset($this->_options['cache_id_prefix'])) || ($this->_options['cache_id_prefix'] == '')) return $array;
  506. // we need to remove cache_id_prefix from ids (see #ZF-6178)
  507. $res = array();
  508. while (list(,$id) = each($array)) {
  509. if (strpos($id, $this->_options['cache_id_prefix']) === 0) {
  510. $res[] = preg_replace("~^{$this->_options['cache_id_prefix']}~", '', $id);
  511. } else {
  512. $res[] = $id;
  513. }
  514. }
  515. return $res;
  516. }
  517. /**
  518. * Return an array of stored tags
  519. *
  520. * @return array array of stored tags (string)
  521. */
  522. public function getTags()
  523. {
  524. if (!$this->_extendedBackend) {
  525. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  526. }
  527. if (!($this->_backendCapabilities['tags'])) {
  528. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  529. }
  530. return $this->_backend->getTags();
  531. }
  532. /**
  533. * Return the filling percentage of the backend storage
  534. *
  535. * @return int integer between 0 and 100
  536. */
  537. public function getFillingPercentage()
  538. {
  539. if (!$this->_extendedBackend) {
  540. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  541. }
  542. return $this->_backend->getFillingPercentage();
  543. }
  544. /**
  545. * Return an array of metadatas for the given cache id
  546. *
  547. * The array will include these keys :
  548. * - expire : the expire timestamp
  549. * - tags : a string array of tags
  550. * - mtime : timestamp of last modification time
  551. *
  552. * @param string $id cache id
  553. * @return array array of metadatas (false if the cache id is not found)
  554. */
  555. public function getMetadatas($id)
  556. {
  557. if (!$this->_extendedBackend) {
  558. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  559. }
  560. $id = $this->_id($id); // cache id may need prefix
  561. return $this->_backend->getMetadatas($id);
  562. }
  563. /**
  564. * Give (if possible) an extra lifetime to the given cache id
  565. *
  566. * @param string $id cache id
  567. * @param int $extraLifetime
  568. * @return boolean true if ok
  569. */
  570. public function touch($id, $extraLifetime)
  571. {
  572. if (!$this->_extendedBackend) {
  573. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  574. }
  575. $id = $this->_id($id); // cache id may need prefix
  576. return $this->_backend->touch($id, $extraLifetime);
  577. }
  578. /**
  579. * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
  580. *
  581. * Throw an exception if a problem is found
  582. *
  583. * @param string $string Cache id or tag
  584. * @throws Zend_Cache_Exception
  585. * @return void
  586. */
  587. protected static function _validateIdOrTag($string)
  588. {
  589. if (!is_string($string)) {
  590. Zend_Cache::throwException('Invalid id or tag : must be a string');
  591. }
  592. if (substr($string, 0, 9) == 'internal-') {
  593. Zend_Cache::throwException('"internal-*" ids or tags are reserved');
  594. }
  595. if (!preg_match('~^[\w]+$~D', $string)) {
  596. Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
  597. }
  598. }
  599. /**
  600. * Validate a tags array (security, reliable filenames, reserved prefixes...)
  601. *
  602. * Throw an exception if a problem is found
  603. *
  604. * @param array $tags Array of tags
  605. * @throws Zend_Cache_Exception
  606. * @return void
  607. */
  608. protected static function _validateTagsArray($tags)
  609. {
  610. if (!is_array($tags)) {
  611. Zend_Cache::throwException('Invalid tags array : must be an array');
  612. }
  613. foreach($tags as $tag) {
  614. self::_validateIdOrTag($tag);
  615. }
  616. reset($tags);
  617. }
  618. /**
  619. * Make sure if we enable logging that the Zend_Log class
  620. * is available.
  621. * Create a default log object if none is set.
  622. *
  623. * @throws Zend_Cache_Exception
  624. * @return void
  625. */
  626. protected function _loggerSanity()
  627. {
  628. if (!isset($this->_options['logging']) || !$this->_options['logging']) {
  629. return;
  630. }
  631. if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
  632. return;
  633. }
  634. // Create a default logger to the standard output stream
  635. require_once 'Zend/Log/Writer/Stream.php';
  636. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  637. $this->_options['logger'] = $logger;
  638. }
  639. /**
  640. * Log a message at the WARN (4) priority.
  641. *
  642. * @param string $message
  643. * @throws Zend_Cache_Exception
  644. * @return void
  645. */
  646. protected function _log($message, $priority = 4)
  647. {
  648. if (!$this->_options['logging']) {
  649. return;
  650. }
  651. if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
  652. Zend_Cache::throwException('Logging is enabled but logger is not set');
  653. }
  654. $logger = $this->_options['logger'];
  655. $logger->log($message, $priority);
  656. }
  657. /**
  658. * Make and return a cache id
  659. *
  660. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  661. *
  662. * @param string $id Cache id
  663. * @return string Cache id (with or without prefix)
  664. */
  665. protected function _id($id)
  666. {
  667. if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
  668. return $this->_options['cache_id_prefix'] . $id; // return with prefix
  669. }
  670. return $id; // no prefix, just return the $id passed
  671. }
  672. }