PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Cache/Core.php

https://bitbucket.org/hieronim1981/tunethemusic
PHP | 765 lines | 382 code | 64 blank | 319 comment | 80 complexity | 232bc3c6fae5f7d54e7338b949e174c1 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-2012 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 24989 2012-06-21 07:24:13Z mabe $
  20. */
  21. /**
  22. * @package Zend_Cache
  23. * @copyright Copyright (c) 2005-2012 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 Zend_Cache_Backend_Interface $_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 Zend_Cache_Backend $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 Zend_Cache_Backend 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!");
  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. $name = strtolower($name);
  223. if (array_key_exists($name, $this->_options)) {
  224. // This is a Core option
  225. return $this->_options[$name];
  226. }
  227. if (array_key_exists($name, $this->_specificOptions)) {
  228. // This a specic option of this frontend
  229. return $this->_specificOptions[$name];
  230. }
  231. Zend_Cache::throwException("Incorrect option name : $name");
  232. }
  233. /**
  234. * Set an option
  235. *
  236. * @param string $name Name of the option
  237. * @param mixed $value Value of the option
  238. * @throws Zend_Cache_Exception
  239. * @return void
  240. */
  241. private function _setOption($name, $value)
  242. {
  243. if (!is_string($name) || !array_key_exists($name, $this->_options)) {
  244. Zend_Cache::throwException("Incorrect option name : $name");
  245. }
  246. if ($name == 'lifetime' && empty($value)) {
  247. $value = null;
  248. }
  249. $this->_options[$name] = $value;
  250. }
  251. /**
  252. * Force a new lifetime
  253. *
  254. * The new value is set for the core/frontend but for the backend too (directive)
  255. *
  256. * @param int $newLifetime New lifetime (in seconds)
  257. * @return void
  258. */
  259. public function setLifetime($newLifetime)
  260. {
  261. $this->_options['lifetime'] = $newLifetime;
  262. $this->_backend->setDirectives(array(
  263. 'lifetime' => $newLifetime
  264. ));
  265. }
  266. /**
  267. * Test if a cache is available for the given id and (if yes) return it (false else)
  268. *
  269. * @param string $id Cache id
  270. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  271. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  272. * @return mixed|false Cached datas
  273. */
  274. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  275. {
  276. if (!$this->_options['caching']) {
  277. return false;
  278. }
  279. $id = $this->_id($id); // cache id may need prefix
  280. $this->_lastId = $id;
  281. self::_validateIdOrTag($id);
  282. $this->_log("Zend_Cache_Core: load item '{$id}'", 7);
  283. $data = $this->_backend->load($id, $doNotTestCacheValidity);
  284. if ($data===false) {
  285. // no cache available
  286. return false;
  287. }
  288. if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
  289. // we need to unserialize before sending the result
  290. return unserialize($data);
  291. }
  292. return $data;
  293. }
  294. /**
  295. * Test if a cache is available for the given id
  296. *
  297. * @param string $id Cache id
  298. * @return int|false Last modified time of cache entry if it is available, false otherwise
  299. */
  300. public function test($id)
  301. {
  302. if (!$this->_options['caching']) {
  303. return false;
  304. }
  305. $id = $this->_id($id); // cache id may need prefix
  306. self::_validateIdOrTag($id);
  307. $this->_lastId = $id;
  308. $this->_log("Zend_Cache_Core: test item '{$id}'", 7);
  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. // new way || deprecated way
  347. if ($this->_extendedBackend || method_exists($this->_backend, 'isAutomaticCleaningAvailable')) {
  348. $this->_log("Zend_Cache_Core::save(): automatic cleaning running", 7);
  349. $this->clean(Zend_Cache::CLEANING_MODE_OLD);
  350. } else {
  351. $this->_log("Zend_Cache_Core::save(): automatic cleaning is not available/necessary with current backend", 4);
  352. }
  353. }
  354. }
  355. $this->_log("Zend_Cache_Core: save item '{$id}'", 7);
  356. if ($this->_options['ignore_user_abort']) {
  357. $abort = ignore_user_abort(true);
  358. }
  359. if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) {
  360. $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority);
  361. } else {
  362. $result = $this->_backend->save($data, $id, $tags, $specificLifetime);
  363. }
  364. if ($this->_options['ignore_user_abort']) {
  365. ignore_user_abort($abort);
  366. }
  367. if (!$result) {
  368. // maybe the cache is corrupted, so we remove it !
  369. $this->_log("Zend_Cache_Core::save(): failed to save item '{$id}' -> removing it", 4);
  370. $this->_backend->remove($id);
  371. return false;
  372. }
  373. if ($this->_options['write_control']) {
  374. $data2 = $this->_backend->load($id, true);
  375. if ($data!=$data2) {
  376. $this->_log("Zend_Cache_Core::save(): write control of item '{$id}' failed -> removing it", 4);
  377. $this->_backend->remove($id);
  378. return false;
  379. }
  380. }
  381. return true;
  382. }
  383. /**
  384. * Remove a cache
  385. *
  386. * @param string $id Cache id to remove
  387. * @return boolean True if ok
  388. */
  389. public function remove($id)
  390. {
  391. if (!$this->_options['caching']) {
  392. return true;
  393. }
  394. $id = $this->_id($id); // cache id may need prefix
  395. self::_validateIdOrTag($id);
  396. $this->_log("Zend_Cache_Core: remove item '{$id}'", 7);
  397. return $this->_backend->remove($id);
  398. }
  399. /**
  400. * Clean cache entries
  401. *
  402. * Available modes are :
  403. * 'all' (default) => remove all cache entries ($tags is not used)
  404. * 'old' => remove too old cache entries ($tags is not used)
  405. * 'matchingTag' => remove cache entries matching all given tags
  406. * ($tags can be an array of strings or a single string)
  407. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  408. * ($tags can be an array of strings or a single string)
  409. * 'matchingAnyTag' => remove cache entries matching any given tags
  410. * ($tags can be an array of strings or a single string)
  411. *
  412. * @param string $mode
  413. * @param array|string $tags
  414. * @throws Zend_Cache_Exception
  415. * @return boolean True if ok
  416. */
  417. public function clean($mode = 'all', $tags = array())
  418. {
  419. if (!$this->_options['caching']) {
  420. return true;
  421. }
  422. if (!in_array($mode, array(Zend_Cache::CLEANING_MODE_ALL,
  423. Zend_Cache::CLEANING_MODE_OLD,
  424. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  425. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  426. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) {
  427. Zend_Cache::throwException('Invalid cleaning mode');
  428. }
  429. self::_validateTagsArray($tags);
  430. return $this->_backend->clean($mode, $tags);
  431. }
  432. /**
  433. * Return an array of stored cache ids which match given tags
  434. *
  435. * In case of multiple tags, a logical AND is made between tags
  436. *
  437. * @param array $tags array of tags
  438. * @return array array of matching cache ids (string)
  439. */
  440. public function getIdsMatchingTags($tags = array())
  441. {
  442. if (!$this->_extendedBackend) {
  443. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  444. }
  445. if (!($this->_backendCapabilities['tags'])) {
  446. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG);
  447. }
  448. $ids = $this->_backend->getIdsMatchingTags($tags);
  449. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  450. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  451. $prefix = & $this->_options['cache_id_prefix'];
  452. $prefixLen = strlen($prefix);
  453. foreach ($ids as &$id) {
  454. if (strpos($id, $prefix) === 0) {
  455. $id = substr($id, $prefixLen);
  456. }
  457. }
  458. }
  459. return $ids;
  460. }
  461. /**
  462. * Return an array of stored cache ids which don't match given tags
  463. *
  464. * In case of multiple tags, a logical OR is made between tags
  465. *
  466. * @param array $tags array of tags
  467. * @return array array of not matching cache ids (string)
  468. */
  469. public function getIdsNotMatchingTags($tags = array())
  470. {
  471. if (!$this->_extendedBackend) {
  472. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  473. }
  474. if (!($this->_backendCapabilities['tags'])) {
  475. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG);
  476. }
  477. $ids = $this->_backend->getIdsNotMatchingTags($tags);
  478. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  479. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  480. $prefix = & $this->_options['cache_id_prefix'];
  481. $prefixLen = strlen($prefix);
  482. foreach ($ids as &$id) {
  483. if (strpos($id, $prefix) === 0) {
  484. $id = substr($id, $prefixLen);
  485. }
  486. }
  487. }
  488. return $ids;
  489. }
  490. /**
  491. * Return an array of stored cache ids which match any given tags
  492. *
  493. * In case of multiple tags, a logical OR is made between tags
  494. *
  495. * @param array $tags array of tags
  496. * @return array array of matching any cache ids (string)
  497. */
  498. public function getIdsMatchingAnyTags($tags = array())
  499. {
  500. if (!$this->_extendedBackend) {
  501. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  502. }
  503. if (!($this->_backendCapabilities['tags'])) {
  504. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG);
  505. }
  506. $ids = $this->_backend->getIdsMatchingAnyTags($tags);
  507. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  508. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  509. $prefix = & $this->_options['cache_id_prefix'];
  510. $prefixLen = strlen($prefix);
  511. foreach ($ids as &$id) {
  512. if (strpos($id, $prefix) === 0) {
  513. $id = substr($id, $prefixLen);
  514. }
  515. }
  516. }
  517. return $ids;
  518. }
  519. /**
  520. * Return an array of stored cache ids
  521. *
  522. * @return array array of stored cache ids (string)
  523. */
  524. public function getIds()
  525. {
  526. if (!$this->_extendedBackend) {
  527. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  528. }
  529. $ids = $this->_backend->getIds();
  530. // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
  531. if (isset($this->_options['cache_id_prefix']) && $this->_options['cache_id_prefix'] !== '') {
  532. $prefix = & $this->_options['cache_id_prefix'];
  533. $prefixLen = strlen($prefix);
  534. foreach ($ids as &$id) {
  535. if (strpos($id, $prefix) === 0) {
  536. $id = substr($id, $prefixLen);
  537. }
  538. }
  539. }
  540. return $ids;
  541. }
  542. /**
  543. * Return an array of stored tags
  544. *
  545. * @return array array of stored tags (string)
  546. */
  547. public function getTags()
  548. {
  549. if (!$this->_extendedBackend) {
  550. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  551. }
  552. if (!($this->_backendCapabilities['tags'])) {
  553. Zend_Cache::throwException(self::BACKEND_NOT_SUPPORTS_TAG);
  554. }
  555. return $this->_backend->getTags();
  556. }
  557. /**
  558. * Return the filling percentage of the backend storage
  559. *
  560. * @return int integer between 0 and 100
  561. */
  562. public function getFillingPercentage()
  563. {
  564. if (!$this->_extendedBackend) {
  565. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  566. }
  567. return $this->_backend->getFillingPercentage();
  568. }
  569. /**
  570. * Return an array of metadatas for the given cache id
  571. *
  572. * The array will include these keys :
  573. * - expire : the expire timestamp
  574. * - tags : a string array of tags
  575. * - mtime : timestamp of last modification time
  576. *
  577. * @param string $id cache id
  578. * @return array array of metadatas (false if the cache id is not found)
  579. */
  580. public function getMetadatas($id)
  581. {
  582. if (!$this->_extendedBackend) {
  583. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  584. }
  585. $id = $this->_id($id); // cache id may need prefix
  586. return $this->_backend->getMetadatas($id);
  587. }
  588. /**
  589. * Give (if possible) an extra lifetime to the given cache id
  590. *
  591. * @param string $id cache id
  592. * @param int $extraLifetime
  593. * @return boolean true if ok
  594. */
  595. public function touch($id, $extraLifetime)
  596. {
  597. if (!$this->_extendedBackend) {
  598. Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF);
  599. }
  600. $id = $this->_id($id); // cache id may need prefix
  601. $this->_log("Zend_Cache_Core: touch item '{$id}'", 7);
  602. return $this->_backend->touch($id, $extraLifetime);
  603. }
  604. /**
  605. * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
  606. *
  607. * Throw an exception if a problem is found
  608. *
  609. * @param string $string Cache id or tag
  610. * @throws Zend_Cache_Exception
  611. * @return void
  612. */
  613. protected static function _validateIdOrTag($string)
  614. {
  615. if (!is_string($string)) {
  616. Zend_Cache::throwException('Invalid id or tag : must be a string');
  617. }
  618. if (substr($string, 0, 9) == 'internal-') {
  619. Zend_Cache::throwException('"internal-*" ids or tags are reserved');
  620. }
  621. if (!preg_match('~^[a-zA-Z0-9_]+$~D', $string)) {
  622. Zend_Cache::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
  623. }
  624. }
  625. /**
  626. * Validate a tags array (security, reliable filenames, reserved prefixes...)
  627. *
  628. * Throw an exception if a problem is found
  629. *
  630. * @param array $tags Array of tags
  631. * @throws Zend_Cache_Exception
  632. * @return void
  633. */
  634. protected static function _validateTagsArray($tags)
  635. {
  636. if (!is_array($tags)) {
  637. Zend_Cache::throwException('Invalid tags array : must be an array');
  638. }
  639. foreach($tags as $tag) {
  640. self::_validateIdOrTag($tag);
  641. }
  642. reset($tags);
  643. }
  644. /**
  645. * Make sure if we enable logging that the Zend_Log class
  646. * is available.
  647. * Create a default log object if none is set.
  648. *
  649. * @throws Zend_Cache_Exception
  650. * @return void
  651. */
  652. protected function _loggerSanity()
  653. {
  654. if (!isset($this->_options['logging']) || !$this->_options['logging']) {
  655. return;
  656. }
  657. if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) {
  658. return;
  659. }
  660. // Create a default logger to the standard output stream
  661. require_once 'Zend/Log.php';
  662. require_once 'Zend/Log/Writer/Stream.php';
  663. require_once 'Zend/Log/Filter/Priority.php';
  664. $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  665. $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<='));
  666. $this->_options['logger'] = $logger;
  667. }
  668. /**
  669. * Log a message at the WARN (4) priority.
  670. *
  671. * @param string $message
  672. * @throws Zend_Cache_Exception
  673. * @return void
  674. */
  675. protected function _log($message, $priority = 4)
  676. {
  677. if (!$this->_options['logging']) {
  678. return;
  679. }
  680. if (!(isset($this->_options['logger']) || $this->_options['logger'] instanceof Zend_Log)) {
  681. Zend_Cache::throwException('Logging is enabled but logger is not set');
  682. }
  683. $logger = $this->_options['logger'];
  684. $logger->log($message, $priority);
  685. }
  686. /**
  687. * Make and return a cache id
  688. *
  689. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  690. *
  691. * @param string $id Cache id
  692. * @return string Cache id (with or without prefix)
  693. */
  694. protected function _id($id)
  695. {
  696. if (($id !== null) && isset($this->_options['cache_id_prefix'])) {
  697. return $this->_options['cache_id_prefix'] . $id; // return with prefix
  698. }
  699. return $id; // no prefix, just return the $id passed
  700. }
  701. }