12 * obtain it through the world-wide-web, please send an email |
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. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Cache |
16 * @package Zend_Cache |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
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 |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @version $Id: Core.php 22651 2010-07-21 04:19:44Z ramon $ |
19 * @version $Id: Core.php 24989 2012-06-21 07:24:13Z mabe $ |
20 */ |
20 */ |
21 |
21 |
22 |
22 |
23 /** |
23 /** |
24 * @package Zend_Cache |
24 * @package Zend_Cache |
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
25 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
27 */ |
27 */ |
28 class Zend_Cache_Core |
28 class Zend_Cache_Core |
29 { |
29 { |
30 /** |
30 /** |
209 * @return void |
209 * @return void |
210 */ |
210 */ |
211 public function setOption($name, $value) |
211 public function setOption($name, $value) |
212 { |
212 { |
213 if (!is_string($name)) { |
213 if (!is_string($name)) { |
214 Zend_Cache::throwException("Incorrect option name : $name"); |
214 Zend_Cache::throwException("Incorrect option name!"); |
215 } |
215 } |
216 $name = strtolower($name); |
216 $name = strtolower($name); |
217 if (array_key_exists($name, $this->_options)) { |
217 if (array_key_exists($name, $this->_options)) { |
218 // This is a Core option |
218 // This is a Core option |
219 $this->_setOption($name, $value); |
219 $this->_setOption($name, $value); |
233 * @throws Zend_Cache_Exception |
233 * @throws Zend_Cache_Exception |
234 * @return mixed option value |
234 * @return mixed option value |
235 */ |
235 */ |
236 public function getOption($name) |
236 public function getOption($name) |
237 { |
237 { |
238 if (is_string($name)) { |
238 $name = strtolower($name); |
239 $name = strtolower($name); |
239 |
240 if (array_key_exists($name, $this->_options)) { |
240 if (array_key_exists($name, $this->_options)) { |
241 // This is a Core option |
241 // This is a Core option |
242 return $this->_options[$name]; |
242 return $this->_options[$name]; |
243 } |
243 } |
244 if (array_key_exists($name, $this->_specificOptions)) { |
244 |
245 // This a specic option of this frontend |
245 if (array_key_exists($name, $this->_specificOptions)) { |
246 return $this->_specificOptions[$name]; |
246 // This a specic option of this frontend |
247 } |
247 return $this->_specificOptions[$name]; |
248 } |
248 } |
|
249 |
249 Zend_Cache::throwException("Incorrect option name : $name"); |
250 Zend_Cache::throwException("Incorrect option name : $name"); |
250 } |
251 } |
251 |
252 |
252 /** |
253 /** |
253 * Set an option |
254 * Set an option |
298 return false; |
299 return false; |
299 } |
300 } |
300 $id = $this->_id($id); // cache id may need prefix |
301 $id = $this->_id($id); // cache id may need prefix |
301 $this->_lastId = $id; |
302 $this->_lastId = $id; |
302 self::_validateIdOrTag($id); |
303 self::_validateIdOrTag($id); |
|
304 |
|
305 $this->_log("Zend_Cache_Core: load item '{$id}'", 7); |
303 $data = $this->_backend->load($id, $doNotTestCacheValidity); |
306 $data = $this->_backend->load($id, $doNotTestCacheValidity); |
304 if ($data===false) { |
307 if ($data===false) { |
305 // no cache available |
308 // no cache available |
306 return false; |
309 return false; |
307 } |
310 } |
324 return false; |
327 return false; |
325 } |
328 } |
326 $id = $this->_id($id); // cache id may need prefix |
329 $id = $this->_id($id); // cache id may need prefix |
327 self::_validateIdOrTag($id); |
330 self::_validateIdOrTag($id); |
328 $this->_lastId = $id; |
331 $this->_lastId = $id; |
|
332 |
|
333 $this->_log("Zend_Cache_Core: test item '{$id}'", 7); |
329 return $this->_backend->test($id); |
334 return $this->_backend->test($id); |
330 } |
335 } |
331 |
336 |
332 /** |
337 /** |
333 * Save some data in a cache |
338 * Save some data in a cache |
358 } else { |
363 } else { |
359 if (!is_string($data)) { |
364 if (!is_string($data)) { |
360 Zend_Cache::throwException("Datas must be string or set automatic_serialization = true"); |
365 Zend_Cache::throwException("Datas must be string or set automatic_serialization = true"); |
361 } |
366 } |
362 } |
367 } |
|
368 |
363 // automatic cleaning |
369 // automatic cleaning |
364 if ($this->_options['automatic_cleaning_factor'] > 0) { |
370 if ($this->_options['automatic_cleaning_factor'] > 0) { |
365 $rand = rand(1, $this->_options['automatic_cleaning_factor']); |
371 $rand = rand(1, $this->_options['automatic_cleaning_factor']); |
366 if ($rand==1) { |
372 if ($rand==1) { |
367 if ($this->_extendedBackend) { |
373 // new way || deprecated way |
368 // New way |
374 if ($this->_extendedBackend || method_exists($this->_backend, 'isAutomaticCleaningAvailable')) { |
369 if ($this->_backendCapabilities['automatic_cleaning']) { |
375 $this->_log("Zend_Cache_Core::save(): automatic cleaning running", 7); |
370 $this->clean(Zend_Cache::CLEANING_MODE_OLD); |
376 $this->clean(Zend_Cache::CLEANING_MODE_OLD); |
371 } else { |
|
372 $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend'); |
|
373 } |
|
374 } else { |
377 } else { |
375 // Deprecated way (will be removed in next major version) |
378 $this->_log("Zend_Cache_Core::save(): automatic cleaning is not available/necessary with current backend", 4); |
376 if (method_exists($this->_backend, 'isAutomaticCleaningAvailable') && ($this->_backend->isAutomaticCleaningAvailable())) { |
|
377 $this->clean(Zend_Cache::CLEANING_MODE_OLD); |
|
378 } else { |
|
379 $this->_log('Zend_Cache_Core::save() / automatic cleaning is not available/necessary with this backend'); |
|
380 } |
|
381 } |
379 } |
382 } |
380 } |
383 } |
381 } |
|
382 |
|
383 $this->_log("Zend_Cache_Core: save item '{$id}'", 7); |
384 if ($this->_options['ignore_user_abort']) { |
384 if ($this->_options['ignore_user_abort']) { |
385 $abort = ignore_user_abort(true); |
385 $abort = ignore_user_abort(true); |
386 } |
386 } |
387 if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) { |
387 if (($this->_extendedBackend) && ($this->_backendCapabilities['priority'])) { |
388 $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority); |
388 $result = $this->_backend->save($data, $id, $tags, $specificLifetime, $priority); |
390 $result = $this->_backend->save($data, $id, $tags, $specificLifetime); |
390 $result = $this->_backend->save($data, $id, $tags, $specificLifetime); |
391 } |
391 } |
392 if ($this->_options['ignore_user_abort']) { |
392 if ($this->_options['ignore_user_abort']) { |
393 ignore_user_abort($abort); |
393 ignore_user_abort($abort); |
394 } |
394 } |
|
395 |
395 if (!$result) { |
396 if (!$result) { |
396 // maybe the cache is corrupted, so we remove it ! |
397 // maybe the cache is corrupted, so we remove it ! |
397 if ($this->_options['logging']) { |
398 $this->_log("Zend_Cache_Core::save(): failed to save item '{$id}' -> removing it", 4); |
398 $this->_log("Zend_Cache_Core::save() : impossible to save cache (id=$id)"); |
399 $this->_backend->remove($id); |
399 } |
|
400 $this->remove($id); |
|
401 return false; |
400 return false; |
402 } |
401 } |
|
402 |
403 if ($this->_options['write_control']) { |
403 if ($this->_options['write_control']) { |
404 $data2 = $this->_backend->load($id, true); |
404 $data2 = $this->_backend->load($id, true); |
405 if ($data!=$data2) { |
405 if ($data!=$data2) { |
406 $this->_log('Zend_Cache_Core::save() / write_control : written and read data do not match'); |
406 $this->_log("Zend_Cache_Core::save(): write control of item '{$id}' failed -> removing it", 4); |
407 $this->_backend->remove($id); |
407 $this->_backend->remove($id); |
408 return false; |
408 return false; |
409 } |
409 } |
410 } |
410 } |
|
411 |
411 return true; |
412 return true; |
412 } |
413 } |
413 |
414 |
414 /** |
415 /** |
415 * Remove a cache |
416 * Remove a cache |
422 if (!$this->_options['caching']) { |
423 if (!$this->_options['caching']) { |
423 return true; |
424 return true; |
424 } |
425 } |
425 $id = $this->_id($id); // cache id may need prefix |
426 $id = $this->_id($id); // cache id may need prefix |
426 self::_validateIdOrTag($id); |
427 self::_validateIdOrTag($id); |
|
428 |
|
429 $this->_log("Zend_Cache_Core: remove item '{$id}'", 7); |
427 return $this->_backend->remove($id); |
430 return $this->_backend->remove($id); |
428 } |
431 } |
429 |
432 |
430 /** |
433 /** |
431 * Clean cache entries |
434 * Clean cache entries |
456 Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG, |
459 Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG, |
457 Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) { |
460 Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG))) { |
458 Zend_Cache::throwException('Invalid cleaning mode'); |
461 Zend_Cache::throwException('Invalid cleaning mode'); |
459 } |
462 } |
460 self::_validateTagsArray($tags); |
463 self::_validateTagsArray($tags); |
|
464 |
461 return $this->_backend->clean($mode, $tags); |
465 return $this->_backend->clean($mode, $tags); |
462 } |
466 } |
463 |
467 |
464 /** |
468 /** |
465 * Return an array of stored cache ids which match given tags |
469 * Return an array of stored cache ids which match given tags |
647 { |
651 { |
648 if (!$this->_extendedBackend) { |
652 if (!$this->_extendedBackend) { |
649 Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); |
653 Zend_Cache::throwException(self::BACKEND_NOT_IMPLEMENTS_EXTENDED_IF); |
650 } |
654 } |
651 $id = $this->_id($id); // cache id may need prefix |
655 $id = $this->_id($id); // cache id may need prefix |
|
656 |
|
657 $this->_log("Zend_Cache_Core: touch item '{$id}'", 7); |
652 return $this->_backend->touch($id, $extraLifetime); |
658 return $this->_backend->touch($id, $extraLifetime); |
653 } |
659 } |
654 |
660 |
655 /** |
661 /** |
656 * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...) |
662 * Validate a cache id or a tag (security, reliable filenames, reserved prefixes...) |
711 if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) { |
717 if (isset($this->_options['logger']) && $this->_options['logger'] instanceof Zend_Log) { |
712 return; |
718 return; |
713 } |
719 } |
714 |
720 |
715 // Create a default logger to the standard output stream |
721 // Create a default logger to the standard output stream |
|
722 require_once 'Zend/Log.php'; |
716 require_once 'Zend/Log/Writer/Stream.php'; |
723 require_once 'Zend/Log/Writer/Stream.php'; |
717 require_once 'Zend/Log.php'; |
724 require_once 'Zend/Log/Filter/Priority.php'; |
718 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output')); |
725 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output')); |
|
726 $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<=')); |
719 $this->_options['logger'] = $logger; |
727 $this->_options['logger'] = $logger; |
720 } |
728 } |
721 |
729 |
722 /** |
730 /** |
723 * Log a message at the WARN (4) priority. |
731 * Log a message at the WARN (4) priority. |