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-2012, 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-2012, 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: if ($duration == 0) {
57: $expires = 0;
58: } else {
59: $expires = time() + $duration;
60: }
61: apc_store($key.'_expires', $expires, $duration);
62: return apc_store($key, $value, $duration);
63: }
64:
65: /**
66: * Read a key from the cache
67: *
68: * @param string $key Identifier for the data
69: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
70: * @access public
71: */
72: function read($key) {
73: $time = time();
74: $cachetime = intval(apc_fetch($key.'_expires'));
75: if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
76: return false;
77: }
78: return apc_fetch($key);
79: }
80:
81: /**
82: * Increments the value of an integer cached key
83: *
84: * @param string $key Identifier for the data
85: * @param integer $offset How much to increment
86: * @param integer $duration How long to cache the data, in seconds
87: * @return New incremented value, false otherwise
88: * @access public
89: */
90: function increment($key, $offset = 1) {
91: return apc_inc($key, $offset);
92: }
93:
94: /**
95: * Decrements the value of an integer cached key
96: *
97: * @param string $key Identifier for the data
98: * @param integer $offset How much to substract
99: * @param integer $duration How long to cache the data, in seconds
100: * @return New decremented value, false otherwise
101: * @access public
102: */
103: function decrement($key, $offset = 1) {
104: return apc_dec($key, $offset);
105: }
106:
107: /**
108: * Delete a key from the cache
109: *
110: * @param string $key Identifier for the data
111: * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
112: * @access public
113: */
114: function delete($key) {
115: return apc_delete($key);
116: }
117:
118: /**
119: * Delete all keys from the cache
120: *
121: * @return boolean True if the cache was succesfully cleared, false otherwise
122: * @access public
123: */
124: function clear() {
125: return apc_clear_cache('user');
126: }
127: }
128: