PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Core.php

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