/framework/vendor/zend/Zend/Cache/Core.php

http://zoop.googlecode.com/ · PHP · 756 lines · 385 code · 51 blank · 320 comment · 86 complexity · 39e088433166ffde15f548e495276cd5 MD5 · raw file

  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: Core.php 21293 2010-03-02 10:26:32Z mabe $
  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. $ids = $this->_backend->getIdsMatchingTags($tags);
  457. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  458. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  459. $prefix = & $this->_options['cache_id_prefix'];
  460. $prefixLen = strlen($prefix);
  461. foreach ($ids as &$id) {
  462. if (strpos($id, $prefix) === 0) {
  463. $id = substr($id, $prefixLen);
  464. }
  465. }
  466. }
  467. return $ids;
  468. }
  469. /**
  470. * Return an array of stored cache ids which don't match given tags
  471. *
  472. * In case of multiple tags, a logical OR is made between tags
  473. *
  474. * @param array $tags array of tags
  475. * @return array array of not matching cache ids (string)
  476. */
  477. public function getIdsNotMatchingTags($tags = array())
  478. {
  479. if (!$this->_extendedBackend) {
  480. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  481. }
  482. if (!($this->_backendCapabilities['tags'])) {
  483. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  484. }
  485. $ids = $this->_backend->getIdsNotMatchingTags($tags);
  486. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  487. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  488. $prefix = & $this->_options['cache_id_prefix'];
  489. $prefixLen = strlen($prefix);
  490. foreach ($ids as &$id) {
  491. if (strpos($id, $prefix) === 0) {
  492. $id = substr($id, $prefixLen);
  493. }
  494. }
  495. }
  496. return $ids;
  497. }
  498. /**
  499. * Return an array of stored cache ids which match any given tags
  500. *
  501. * In case of multiple tags, a logical OR is made between tags
  502. *
  503. * @param array $tags array of tags
  504. * @return array array of matching any cache ids (string)
  505. */
  506. public function getIdsMatchingAnyTags($tags = array())
  507. {
  508. if (!$this->_extendedBackend) {
  509. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  510. }
  511. if (!($this->_backendCapabilities['tags'])) {
  512. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  513. }
  514. $ids = $this->_backend->getIdsMatchingAnyTags($tags);
  515. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  516. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  517. $prefix = & $this->_options['cache_id_prefix'];
  518. $prefixLen = strlen($prefix);
  519. foreach ($ids as &$id) {
  520. if (strpos($id, $prefix) === 0) {
  521. $id = substr($id, $prefixLen);
  522. }
  523. }
  524. }
  525. return $ids;
  526. }
  527. /**
  528. * Return an array of stored cache ids
  529. *
  530. * @return array array of stored cache ids (string)
  531. */
  532. public function getIds()
  533. {
  534. if (!$this->_extendedBackend) {
  535. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  536. }
  537. $ids = $this->_backend->getIds();
  538. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  539. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  540. $prefix = & $this->_options['cache_id_prefix'];
  541. $prefixLen = strlen($prefix);
  542. foreach ($ids as &$id) {
  543. if (strpos($id, $prefix) === 0) {
  544. $id = substr($id, $prefixLen);
  545. }
  546. }
  547. }
  548. return $ids;
  549. }
  550. /**
  551. * Return an array of stored tags
  552. *
  553. * @return array array of stored tags (string)
  554. */
  555. public function getTags()
  556. {
  557. if (!$this->_extendedBackend) {
  558. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  559. }
  560. if (!($this->_backendCapabilities['tags'])) {
  561. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORT_TAG);
  562. }
  563. return $this->_backend->getTags();
  564. }
  565. /**
  566. * Return the filling percentage of the backend storage
  567. *
  568. * @return int integer between 0 and 100
  569. */
  570. public function getFillingPercentage()
  571. {
  572. if (!$this->_extendedBackend) {
  573. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  574. }
  575. return $this->_backend->getFillingPercentage();
  576. }
  577. /**
  578. * Return an array of metadatas for the given cache id
  579. *
  580. * The array will include these keys :
  581. * - expire : the expire timestamp
  582. * - tags : a string array of tags
  583. * - mtime : timestamp of last modification time
  584. *
  585. * @param string $id cache id
  586. * @return array array of metadatas (false if the cache id is not found)
  587. */
  588. public function getMetadatas($id)
  589. {
  590. if (!$this->_extendedBackend) {
  591. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  592. }
  593. $id = $this->_id($id); // cache id may need prefix
  594. return $this->_backend->getMetadatas($id);
  595. }
  596. /**
  597. * Give (if possible) an extra lifetime to the given cache id
  598. *
  599. * @param string $id cache id
  600. * @param int $extraLifetime
  601. * @return boolean true if ok
  602. */
  603. public function touch($id, $extraLifetime)
  604. {
  605. if (!$this->_extendedBackend) {
  606. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  607. }
  608. $id = $this->_id($id); // cache id may need prefix
  609. return $this->_backend->touch($id, $extraLifetime);
  610. }
  611. /**
  612. * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
  613. *
  614. * Throw an exception if a problem is found
  615. *
  616. * @param string $string Cache id or tag
  617. * @throws Zend_Cache_Exception
  618. * @return void
  619. */
  620. protected static function _validateIdOrTag($string)
  621. {
  622. if (!is_string($string)) {
  623. Zend_Cache::throwException('Invalid id or tag : must be a string');
  624. }
  625. if (substr($string, 0, 9) == 'internal-') {
  626. Zend_Cache::throwException('"internal-*" ids or tags are reserved');
  627. }
  628. if (!preg_match('~^[a-zA-Z0-9_]+$~D', $string)) {
  629. Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
  630. }
  631. }
  632. /**
  633. * Validate a tags array (security, reliable filenames, reserved prefixes...)
  634. *
  635. * Throw an exception if a problem is found
  636. *
  637. * @param array $tags Array of tags
  638. * @throws Zend_Cache_Exception
  639. * @return void
  640. */
  641. protected static function _validateTagsArray($tags)
  642. {
  643. if (!is_array($tags)) {
  644. Zend_Cache::throwException('Invalid tags array : must be an array');
  645. }
  646. foreach($tags as $tag) {
  647. self::_validateIdOrTag($tag);
  648. }
  649. reset($tags);
  650. }
  651. /**
  652. * Make sure if we enable logging that the Zend_Log class
  653. * is available.
  654. * Create a default log object if none is set.
  655. *
  656. * @throws Zend_Cache_Exception
  657. * @return void
  658. */
  659. protected function _loggerSanity()
  660. {
  661. if (!isset($this->_options['logging']) || !$this->_options['logging']) {
  662. return;
  663. }
  664. if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
  665. return;
  666. }
  667. // Create a default logger to the standard output stream
  668. require_once 'Zend/Log/Writer/Stream.php';
  669. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  670. $this->_options['logger'] = $logger;
  671. }
  672. /**
  673. * Log a message at the WARN (4) priority.
  674. *
  675. * @param string $message
  676. * @throws Zend_Cache_Exception
  677. * @return void
  678. */
  679. protected function _log($message, $priority = 4)
  680. {
  681. if (!$this->_options['logging']) {
  682. return;
  683. }
  684. if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
  685. Zend_Cache::throwException('Logging is enabled but logger is not set');
  686. }
  687. $logger = $this->_options['logger'];
  688. $logger->log($message, $priority);
  689. }
  690. /**
  691. * Make and return a cache id
  692. *
  693. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  694. *
  695. * @param string $id Cache id
  696. * @return string Cache id (with or without prefix)
  697. */
  698. protected function _id($id)
  699. {
  700. if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
  701. return $this->_options['cache_id_prefix'] . $id; // return with prefix
  702. }
  703. return $id; // no prefix, just return the $id passed
  704. }
  705. }