cake/libs/cache/apc.php
| 1 | <?php |
|---|---|
| 2 | /** |
| 3 | * APC storage engine for cache. |
| 4 | * |
| 5 | * |
| 6 | * PHP versions 4 and 5 |
| 7 | * |
| 8 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 9 | * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 10 | * |
| 11 | * Licensed under The MIT License |
| 12 | * Redistributions of files must retain the above copyright notice. |
| 13 | * |
| 14 | * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 15 | * @link http://cakephp.org CakePHP(tm) Project |
| 16 | * @package cake |
| 17 | * @subpackage cake.cake.libs.cache |
| 18 | * @since CakePHP(tm) v 1.2.0.4933 |
| 19 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * APC storage engine for cache |
| 24 | * |
| 25 | * @package cake |
| 26 | * @subpackage cake.cake.libs.cache |
| 27 | */ |
| 28 | class ApcEngine extends CacheEngine { |
| 29 | |
| 30 | /** |
| 31 | * Initialize the Cache Engine |
| 32 | * |
| 33 | * Called automatically by the cache frontend |
| 34 | * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); |
| 35 | * |
| 36 | * @param array $setting array of setting for the engine |
| 37 | * @return boolean True if the engine has been successfully initialized, false if not |
| 38 | * @see CacheEngine::__defaults |
| 39 | * @access public |
| 40 | */ |
| 41 | function init($settings = array()) { |
| 42 | parent::init(array_merge(array('engine' => 'Apc', 'prefix' => Inflector::slug(APP_DIR) . '_'), $settings)); |
| 43 | return function_exists('apc_cache_info'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Write data for key into cache |
| 48 | * |
| 49 | * @param string $key Identifier for the data |
| 50 | * @param mixed $value Data to be cached |
| 51 | * @param integer $duration How long to cache the data, in seconds |
| 52 | * @return boolean True if the data was succesfully cached, false on failure |
| 53 | * @access public |
| 54 | */ |
| 55 | function write($key, &$value, $duration) { |
| 56 | $expires = time() + $duration; |
| 57 | apc_store($key.'_expires', $expires, $duration); |
| 58 | return apc_store($key, $value, $duration); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Read a key from the cache |
| 63 | * |
| 64 | * @param string $key Identifier for the data |
| 65 | * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it |
| 66 | * @access public |
| 67 | */ |
| 68 | function read($key) { |
| 69 | $time = time(); |
| 70 | $cachetime = intval(apc_fetch($key.'_expires')); |
| 71 | if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) { |
| 72 | return false; |
| 73 | } |
| 74 | return apc_fetch($key); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Increments the value of an integer cached key |
| 79 | * |
| 80 | * @param string $key Identifier for the data |
| 81 | * @param integer $offset How much to increment |
| 82 | * @param integer $duration How long to cache the data, in seconds |
| 83 | * @return New incremented value, false otherwise |
| 84 | * @access public |
| 85 | */ |
| 86 | function increment($key, $offset = 1) { |
| 87 | return apc_inc($key, $offset); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Decrements the value of an integer cached key |
| 92 | * |
| 93 | * @param string $key Identifier for the data |
| 94 | * @param integer $offset How much to substract |
| 95 | * @param integer $duration How long to cache the data, in seconds |
| 96 | * @return New decremented value, false otherwise |
| 97 | * @access public |
| 98 | */ |
| 99 | function decrement($key, $offset = 1) { |
| 100 | return apc_dec($key, $offset); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Delete a key from the cache |
| 105 | * |
| 106 | * @param string $key Identifier for the data |
| 107 | * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed |
| 108 | * @access public |
| 109 | */ |
| 110 | function delete($key) { |
| 111 | return apc_delete($key); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Delete all keys from the cache |
| 116 | * |
| 117 | * @return boolean True if the cache was succesfully cleared, false otherwise |
| 118 | * @access public |
| 119 | */ |
| 120 | function clear() { |
| 121 | return apc_clear_cache('user'); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 |