1: <?php
2: /**
3: * APC storage engine for cache.
4: *
5: *
6: * PHP 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.Cache.Engine
17: * @since CakePHP(tm) v 1.2.0.4933
18: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19: */
20:
21: /**
22: * APC storage engine for cache
23: *
24: * @package Cake.Cache.Engine
25: */
26: class ApcEngine extends CacheEngine {
27:
28: /**
29: * Initialize the Cache Engine
30: *
31: * Called automatically by the cache frontend
32: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
33: *
34: * @param array $settings array of setting for the engine
35: * @return boolean True if the engine has been successfully initialized, false if not
36: * @see CacheEngine::__defaults
37: */
38: public function init($settings = array()) {
39: parent::init(array_merge(array('engine' => 'Apc', 'prefix' => Inflector::slug(APP_DIR) . '_'), $settings));
40: return function_exists('apc_dec');
41: }
42:
43: /**
44: * Write data for key into cache
45: *
46: * @param string $key Identifier for the data
47: * @param mixed $value Data to be cached
48: * @param integer $duration How long to cache the data, in seconds
49: * @return boolean True if the data was successfully cached, false on failure
50: */
51: public function write($key, $value, $duration) {
52: if ($duration == 0) {
53: $expires = 0;
54: } else {
55: $expires = time() + $duration;
56: }
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: */
67: public function read($key) {
68: $time = time();
69: $cachetime = intval(apc_fetch($key . '_expires'));
70: if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
71: return false;
72: }
73: return apc_fetch($key);
74: }
75:
76: /**
77: * Increments the value of an integer cached key
78: *
79: * @param string $key Identifier for the data
80: * @param integer $offset How much to increment
81: * @return New incremented value, false otherwise
82: */
83: public function increment($key, $offset = 1) {
84: return apc_inc($key, $offset);
85: }
86:
87: /**
88: * Decrements the value of an integer cached key
89: *
90: * @param string $key Identifier for the data
91: * @param integer $offset How much to subtract
92: * @return New decremented value, false otherwise
93: */
94: public function decrement($key, $offset = 1) {
95: return apc_dec($key, $offset);
96: }
97:
98: /**
99: * Delete a key from the cache
100: *
101: * @param string $key Identifier for the data
102: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
103: */
104: public function delete($key) {
105: return apc_delete($key);
106: }
107:
108: /**
109: * Delete all keys from the cache. This will clear every cache config using APC.
110: *
111: * @param boolean $check If true, nothing will be cleared, as entries are removed
112: * from APC as they expired. This flag is really only used by FileEngine.
113: * @return boolean True Returns true.
114: */
115: public function clear($check) {
116: if ($check) {
117: return true;
118: }
119: $info = apc_cache_info('user');
120: $cacheKeys = $info['cache_list'];
121: unset($info);
122: foreach ($cacheKeys as $key) {
123: if (strpos($key['info'], $this->settings['prefix']) === 0) {
124: apc_delete($key['info']);
125: }
126: }
127: return true;
128: }
129:
130: }
131: