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.
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
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
26 * @license http://framework.zend.com/license/new-bsd New BSD License
33 const BACKEND_NOT_SUPPORTS_TAG
= 'tags are not supported by the current backend';
34 const BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
= 'Current backend doesn\'t implement the Zend_Cache_Backend_ExtendedInterface, so this method is not available';
39 * @var Zend_Cache_Backend_Interface $_backend
41 protected $_backend = null;
46 * ====> (boolean) write_control :
47 * - Enable / disable write control (the cache is read just after writing to detect corrupt entries)
48 * - Enable write control will lightly slow the cache writing but not the cache reading
49 * Write control can detect some corrupt cache files but maybe it's not a perfect control
51 * ====> (boolean) caching :
52 * - Enable / disable caching
53 * (can be very useful for the debug of cached scripts)
55 * =====> (string) cache_id_prefix :
56 * - prefix for cache ids (namespace)
58 * ====> (boolean) automatic_serialization :
59 * - Enable / disable automatic serialization
60 * - It can be used to save directly datas which aren't strings (but it's slower)
62 * ====> (int) automatic_cleaning_factor :
63 * - Disable / Tune the automatic cleaning process
64 * - The automatic cleaning process destroy too old (for the given life time)
65 * cache files when a new cache file is written :
66 * 0 => no automatic cache cleaning
67 * 1 => systematic cache cleaning
68 * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
70 * ====> (int) lifetime :
71 * - Cache lifetime (in seconds)
72 * - If null, the cache is valid forever.
74 * ====> (boolean) logging :
75 * - If set to true, logging is activated (but the system is slower)
77 * ====> (boolean) ignore_user_abort
78 * - If set to true, the core will set the ignore_user_abort PHP flag inside the
79 * save() method to avoid cache corruptions in some cases (default false)
81 * @var array $_options available options
83 protected $_options = array(
84 'write_control' => true,
86 'cache_id_prefix' => null,
87 'automatic_serialization' => false,
88 'automatic_cleaning_factor' => 10,
92 'ignore_user_abort' => false
96 * Array of options which have to be transfered to backend
98 * @var array $_directivesList
100 protected static $_directivesList = array('lifetime', 'logging', 'logger');
103 * Not used for the core, just a sort a hint to get a common setOption() method (for the core and for frontends)
105 * @var array $_specificOptions
107 protected $_specificOptions = array();
112 * @var string $_lastId
114 private $_lastId = null;
117 * True if the backend implements Zend_Cache_Backend_ExtendedInterface
119 * @var boolean $_extendedBackend
121 protected $_extendedBackend = false;
124 * Array of capabilities of the backend (only if it implements Zend_Cache_Backend_ExtendedInterface)
128 protected $_backendCapabilities = array();
133 * @param array|Zend_Config $options Associative array of options or Zend_Config instance
134 * @throws Zend_Cache_Exception
137 public function __construct($options = array())
139 if ($options instanceof Zend_Config
) {
140 $options = $options->toArray();
142 if (!is_array($options)) {
143 Zend_Cache
::throwException("Options passed were not an array"
144 . " or Zend_Config instance.");
146 while (list($name, $value) = each($options)) {
147 $this->setOption($name, $value);
149 $this->_loggerSanity();
153 * Set options using an instance of type Zend_Config
155 * @param Zend_Config $config
156 * @return Zend_Cache_Core
158 public function setConfig(Zend_Config
$config)
160 $options = $config->toArray();
161 while (list($name, $value) = each($options)) {
162 $this->setOption($name, $value);
170 * @param Zend_Cache_Backend $backendObject
171 * @throws Zend_Cache_Exception
174 public function setBackend(Zend_Cache_Backend
$backendObject)
176 $this->_backend
= $backendObject;
177 // some options (listed in $_directivesList) have to be given
178 // to the backend too (even if they are not "backend specific")
179 $directives = array();
180 foreach (Zend_Cache_Core
::$_directivesList as $directive) {
181 $directives[$directive] = $this->_options
[$directive];
183 $this->_backend
->setDirectives($directives);
184 if (in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_backend
))) {
185 $this->_extendedBackend
= true;
186 $this->_backendCapabilities
= $this->_backend
->getCapabilities();
192 * Returns the backend
194 * @return Zend_Cache_Backend backend object
196 public function getBackend()
198 return $this->_backend
;
202 * Public frontend to set an option
204 * There is an additional validation (relatively to the protected _setOption method)
206 * @param string $name Name of the option
207 * @param mixed $value Value of the option
208 * @throws Zend_Cache_Exception
211 public function setOption($name, $value)
213 if (!is_string($name)) {
214 Zend_Cache
::throwException("Incorrect option name : $name");
216 $name = strtolower($name);
217 if (array_key_exists($name, $this->_options
)) {
218 // This is a Core option
219 $this->_setOption($name, $value);
222 if (array_key_exists($name, $this->_specificOptions
)) {
223 // This a specic option of this frontend
224 $this->_specificOptions
[$name] = $value;
230 * Public frontend to get an option value
232 * @param string $name Name of the option
233 * @throws Zend_Cache_Exception
234 * @return mixed option value
236 public function getOption($name)
238 if (is_string($name)) {
239 $name = strtolower($name);
240 if (array_key_exists($name, $this->_options
)) {
241 // This is a Core option
242 return $this->_options
[$name];
244 if (array_key_exists($name, $this->_specificOptions
)) {
245 // This a specic option of this frontend
246 return $this->_specificOptions
[$name];
249 Zend_Cache
::throwException("Incorrect option name : $name");
255 * @param string $name Name of the option
256 * @param mixed $value Value of the option
257 * @throws Zend_Cache_Exception
260 private function _setOption($name, $value)
262 if (!is_string($name) ||
!array_key_exists($name, $this->_options
)) {
263 Zend_Cache
::throwException("Incorrect option name : $name");
265 if ($name == 'lifetime' && empty($value)) {
268 $this->_options
[$name] = $value;
272 * Force a new lifetime
274 * The new value is set for the core/frontend but for the backend too (directive)
276 * @param int $newLifetime New lifetime (in seconds)
279 public function setLifetime($newLifetime)
281 $this->_options
['lifetime'] = $newLifetime;
282 $this->_backend
->setDirectives(array(
283 'lifetime' => $newLifetime
288 * Test if a cache is available for the given id and (if yes) return it (false else)
290 * @param string $id Cache id
291 * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
292 * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
293 * @return mixed|false Cached datas
295 public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
297 if (!$this->_options
['caching']) {
300 $id = $this->_id($id); // cache id may need prefix
301 $this->_lastId
= $id;
302 self
::_validateIdOrTag($id);
303 $data = $this->_backend
->load($id, $doNotTestCacheValidity);
305 // no cache available
308 if ((!$doNotUnserialize) && $this->_options
['automatic_serialization']) {
309 // we need to unserialize before sending the result
310 return unserialize($data);
316 * Test if a cache is available for the given id
318 * @param string $id Cache id
319 * @return int|false Last modified time of cache entry if it is available, false otherwise
321 public function test($id)
323 if (!$this->_options
['caching']) {
326 $id = $this->_id($id); // cache id may need prefix
327 self
::_validateIdOrTag($id);
328 $this->_lastId
= $id;
329 return $this->_backend
->test($id);
333 * Save some data in a cache
335 * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
336 * @param string $id Cache id (if not set, the last cache id will be used)
337 * @param array $tags Cache tags
338 * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
339 * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
340 * @throws Zend_Cache_Exception
341 * @return boolean True if no problem
343 public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
345 if (!$this->_options
['caching']) {
349 $id = $this->_lastId
;
351 $id = $this->_id($id);
353 self
::_validateIdOrTag($id);
354 self
::_validateTagsArray($tags);
355 if ($this->_options
['automatic_serialization']) {
356 // we need to serialize datas before storing them
357 $data = serialize($data);
359 if (!is_string($data)) {
360 Zend_Cache
::throwException("Datas must be string or set automatic_serialization = true");
363 // automatic cleaning
364 if ($this->_options
['automatic_cleaning_factor'] > 0) {
365 $rand = rand(1, $this->_options
['automatic_cleaning_factor']);
367 if ($this->_extendedBackend
) {
369 if ($this->_backendCapabilities
['automatic_cleaning']) {
370 $this->clean(Zend_Cache
::CLEANING_MODE_OLD
);
372 $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
375 // Deprecated way (will be removed in next major version)
376 if (method_exists($this->_backend
, 'isAutomaticCleaningAvailable') && ($this->_backend
->isAutomaticCleaningAvailable())) {
377 $this->clean(Zend_Cache
::CLEANING_MODE_OLD
);
379 $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend');
384 if ($this->_options
['ignore_user_abort']) {
385 $abort = ignore_user_abort(true);
387 if (($this->_extendedBackend
) && ($this->_backendCapabilities
['priority'])) {
388 $result = $this->_backend
->save($data, $id, $tags, $specificLifetime, $priority);
390 $result = $this->_backend
->save($data, $id, $tags, $specificLifetime);
392 if ($this->_options
['ignore_user_abort']) {
393 ignore_user_abort($abort);
396 // maybe the cache is corrupted, so we remove it !
397 if ($this->_options
['logging']) {
398 $this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)");
403 if ($this->_options
['write_control']) {
404 $data2 = $this->_backend
->load($id, true);
406 $this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match');
407 $this->_backend
->remove($id);
417 * @param string $id Cache id to remove
418 * @return boolean True if ok
420 public function remove($id)
422 if (!$this->_options
['caching']) {
425 $id = $this->_id($id); // cache id may need prefix
426 self
::_validateIdOrTag($id);
427 return $this->_backend
->remove($id);
431 * Clean cache entries
433 * Available modes are :
434 * 'all' (default) => remove all cache entries ($tags is not used)
435 * 'old' => remove too old cache entries ($tags is not used)
436 * 'matchingTag' => remove cache entries matching all given tags
437 * ($tags can be an array of strings or a single string)
438 * 'notMatchingTag' => remove cache entries not matching one of the given tags
439 * ($tags can be an array of strings or a single string)
440 * 'matchingAnyTag' => remove cache entries matching any given tags
441 * ($tags can be an array of strings or a single string)
443 * @param string $mode
444 * @param array|string $tags
445 * @throws Zend_Cache_Exception
446 * @return boolean True if ok
448 public function clean($mode = 'all', $tags = array())
450 if (!$this->_options
['caching']) {
453 if (!in_array($mode, array(Zend_Cache
::CLEANING_MODE_ALL
,
454 Zend_Cache
::CLEANING_MODE_OLD
,
455 Zend_Cache
::CLEANING_MODE_MATCHING_TAG
,
456 Zend_Cache
::CLEANING_MODE_NOT_MATCHING_TAG
,
457 Zend_Cache
::CLEANING_MODE_MATCHING_ANY_TAG
))) {
458 Zend_Cache
::throwException('Invalid cleaning mode');
460 self
::_validateTagsArray($tags);
461 return $this->_backend
->clean($mode, $tags);
465 * Return an array of stored cache ids which match given tags
467 * In case of multiple tags, a logical AND is made between tags
469 * @param array $tags array of tags
470 * @return array array of matching cache ids (string)
472 public function getIdsMatchingTags($tags = array())
474 if (!$this->_extendedBackend
) {
475 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
477 if (!($this->_backendCapabilities
['tags'])) {
478 Zend_Cache
::throwException(self
::BACKEND_NOT_SUPPORTS_TAG
);
481 $ids = $this->_backend
->getIdsMatchingTags($tags);
483 // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
484 if (isset($this->_options
['cache_id_prefix']) && $this->_options
['cache_id_prefix'] !== '') {
485 $prefix = & $this->_options
['cache_id_prefix'];
486 $prefixLen = strlen($prefix);
487 foreach ($ids as &$id) {
488 if (strpos($id, $prefix) === 0) {
489 $id = substr($id, $prefixLen);
498 * Return an array of stored cache ids which don't match given tags
500 * In case of multiple tags, a logical OR is made between tags
502 * @param array $tags array of tags
503 * @return array array of not matching cache ids (string)
505 public function getIdsNotMatchingTags($tags = array())
507 if (!$this->_extendedBackend
) {
508 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
510 if (!($this->_backendCapabilities
['tags'])) {
511 Zend_Cache
::throwException(self
::BACKEND_NOT_SUPPORTS_TAG
);
514 $ids = $this->_backend
->getIdsNotMatchingTags($tags);
516 // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
517 if (isset($this->_options
['cache_id_prefix']) && $this->_options
['cache_id_prefix'] !== '') {
518 $prefix = & $this->_options
['cache_id_prefix'];
519 $prefixLen = strlen($prefix);
520 foreach ($ids as &$id) {
521 if (strpos($id, $prefix) === 0) {
522 $id = substr($id, $prefixLen);
531 * Return an array of stored cache ids which match any given tags
533 * In case of multiple tags, a logical OR is made between tags
535 * @param array $tags array of tags
536 * @return array array of matching any cache ids (string)
538 public function getIdsMatchingAnyTags($tags = array())
540 if (!$this->_extendedBackend
) {
541 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
543 if (!($this->_backendCapabilities
['tags'])) {
544 Zend_Cache
::throwException(self
::BACKEND_NOT_SUPPORTS_TAG
);
547 $ids = $this->_backend
->getIdsMatchingAnyTags($tags);
549 // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
550 if (isset($this->_options
['cache_id_prefix']) && $this->_options
['cache_id_prefix'] !== '') {
551 $prefix = & $this->_options
['cache_id_prefix'];
552 $prefixLen = strlen($prefix);
553 foreach ($ids as &$id) {
554 if (strpos($id, $prefix) === 0) {
555 $id = substr($id, $prefixLen);
564 * Return an array of stored cache ids
566 * @return array array of stored cache ids (string)
568 public function getIds()
570 if (!$this->_extendedBackend
) {
571 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
574 $ids = $this->_backend
->getIds();
576 // we need to remove cache_id_prefix from ids (see #ZF-6178, #ZF-7600)
577 if (isset($this->_options
['cache_id_prefix']) && $this->_options
['cache_id_prefix'] !== '') {
578 $prefix = & $this->_options
['cache_id_prefix'];
579 $prefixLen = strlen($prefix);
580 foreach ($ids as &$id) {
581 if (strpos($id, $prefix) === 0) {
582 $id = substr($id, $prefixLen);
591 * Return an array of stored tags
593 * @return array array of stored tags (string)
595 public function getTags()
597 if (!$this->_extendedBackend
) {
598 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
600 if (!($this->_backendCapabilities
['tags'])) {
601 Zend_Cache
::throwException(self
::BACKEND_NOT_SUPPORTS_TAG
);
603 return $this->_backend
->getTags();
607 * Return the filling percentage of the backend storage
609 * @return int integer between 0 and 100
611 public function getFillingPercentage()
613 if (!$this->_extendedBackend
) {
614 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
616 return $this->_backend
->getFillingPercentage();
620 * Return an array of metadatas for the given cache id
622 * The array will include these keys :
623 * - expire : the expire timestamp
624 * - tags : a string array of tags
625 * - mtime : timestamp of last modification time
627 * @param string $id cache id
628 * @return array array of metadatas (false if the cache id is not found)
630 public function getMetadatas($id)
632 if (!$this->_extendedBackend
) {
633 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
635 $id = $this->_id($id); // cache id may need prefix
636 return $this->_backend
->getMetadatas($id);
640 * Give (if possible) an extra lifetime to the given cache id
642 * @param string $id cache id
643 * @param int $extraLifetime
644 * @return boolean true if ok
646 public function touch($id, $extraLifetime)
648 if (!$this->_extendedBackend
) {
649 Zend_Cache
::throwException(self
::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF
);
651 $id = $this->_id($id); // cache id may need prefix
652 return $this->_backend
->touch($id, $extraLifetime);
656 * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...)
658 * Throw an exception if a problem is found
660 * @param string $string Cache id or tag
661 * @throws Zend_Cache_Exception
664 protected static function _validateIdOrTag($string)
666 if (!is_string($string)) {
667 Zend_Cache
::throwException('Invalid id or tag : must be a string');
669 if (substr($string, 0, 9) == 'internal-') {
670 Zend_Cache
::throwException('"internal-*" ids or tags are reserved');
672 if (!preg_match('~^[a-zA-Z0-9_]+$~D', $string)) {
673 Zend_Cache
::throwException("Invalid id or tag '$string' : must use only [a-zA-Z0-9_]");
678 * Validate a tags array (security, reliable filenames, reserved prefixes...)
680 * Throw an exception if a problem is found
682 * @param array $tags Array of tags
683 * @throws Zend_Cache_Exception
686 protected static function _validateTagsArray($tags)
688 if (!is_array($tags)) {
689 Zend_Cache
::throwException('Invalid tags array : must be an array');
691 foreach($tags as $tag) {
692 self
::_validateIdOrTag($tag);
698 * Make sure if we enable logging that the Zend_Log class
700 * Create a default log object if none is set.
702 * @throws Zend_Cache_Exception
705 protected function _loggerSanity()
707 if (!isset($this->_options
['logging']) ||
!$this->_options
['logging']) {
711 if (isset($this->_options
['logger']) && $this->_options
['logger'] instanceof Zend_Log
) {
715 // Create a default logger to the standard output stream
716 require_once 'Zend/Log/Writer/Stream.php';
717 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
718 $this->_options
['logger'] = $logger;
722 * Log a message at the WARN (4) priority.
724 * @param string $message
725 * @throws Zend_Cache_Exception
728 protected function _log($message, $priority = 4)
730 if (!$this->_options
['logging']) {
733 if (!(isset($this->_options
['logger']) ||
$this->_options
['logger'] instanceof Zend_Log
)) {
734 Zend_Cache
::throwException('Logging is enabled but logger is not set');
736 $logger = $this->_options
['logger'];
737 $logger->log($message, $priority);
741 * Make and return a cache id
743 * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
745 * @param string $id Cache id
746 * @return string Cache id (with or without prefix)
748 protected function _id($id)
750 if (($id !== null) && isset($this->_options
['cache_id_prefix'])) {
751 return $this->_options
['cache_id_prefix'] . $id; // return with prefix
753 return $id; // no prefix, just return the $id passed