3
* APC storage engine for cache.
8
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
9
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
11
* Licensed under The MIT License
12
* Redistributions of files must retain the above copyright notice.
14
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
15
* @link http://cakephp.org CakePHP(tm) Project
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)
23
* APC storage engine for cache
26
* @subpackage cake.cake.libs.cache
28
class ApcEngine extends CacheEngine {
31
* Initialize the Cache Engine
33
* Called automatically by the cache frontend
34
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
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
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');
47
* Write data for key into cache
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
55
function write($key, &$value, $duration) {
56
$expires = time() + $duration;
57
apc_store($key.'_expires', $expires, $duration);
58
return apc_store($key, $value, $duration);
62
* Read a key from the cache
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
70
$cachetime = intval(apc_fetch($key.'_expires'));
71
if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
74
return apc_fetch($key);
78
* Increments the value of an integer cached key
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
86
function increment($key, $offset = 1) {
87
return apc_inc($key, $offset);
91
* Decrements the value of an integer cached key
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
99
function decrement($key, $offset = 1) {
100
return apc_dec($key, $offset);
104
* Delete a key from the cache
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
110
function delete($key) {
111
return apc_delete($key);
115
* Delete all keys from the cache
117
* @return boolean True if the cache was succesfully cleared, false otherwise
121
return apc_clear_cache('user');