1: <?php
2: /**
3: * Wincache storage engine for cache.
4: *
5: * Supports wincache 1.1.0 and higher.
6: *
7: * PHP 5
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * For full copyright and license information, please see the LICENSE.txt
14: * Redistributions of files must retain the above copyright notice.
15: *
16: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
17: * @link http://cakephp.org CakePHP(tm) Project
18: * @package Cake.Cache.Engine
19: * @since CakePHP(tm) v 1.2.0.4933
20: * @license http://www.opensource.org/licenses/mit-license.php MIT License
21: */
22:
23: /**
24: * Wincache storage engine for cache
25: *
26: * @package Cake.Cache.Engine
27: */
28: class WincacheEngine extends CacheEngine {
29:
30: /**
31: * Contains the compiled group names
32: * (prefixed with the global configuration prefix)
33: *
34: * @var array
35: */
36: protected $_compiledGroupNames = array();
37:
38: /**
39: * Initialize the Cache Engine
40: *
41: * Called automatically by the cache frontend
42: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
43: *
44: * @param array $settings array of setting for the engine
45: * @return boolean True if the engine has been successfully initialized, false if not
46: * @see CacheEngine::__defaults
47: */
48: public function init($settings = array()) {
49: if (!isset($settings['prefix'])) {
50: $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
51: }
52: $settings += array('engine' => 'Wincache');
53: parent::init($settings);
54: return function_exists('wincache_ucache_info');
55: }
56:
57: /**
58: * Write data for key into cache
59: *
60: * @param string $key Identifier for the data
61: * @param mixed $value Data to be cached
62: * @param integer $duration How long to cache the data, in seconds
63: * @return boolean True if the data was successfully cached, false on failure
64: */
65: public function write($key, $value, $duration) {
66: $expires = time() + $duration;
67:
68: $data = array(
69: $key . '_expires' => $expires,
70: $key => $value
71: );
72: $result = wincache_ucache_set($data, null, $duration);
73: return empty($result);
74: }
75:
76: /**
77: * Read a key from the cache
78: *
79: * @param string $key Identifier for the data
80: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if
81: * there was an error fetching it
82: */
83: public function read($key) {
84: $time = time();
85: $cachetime = intval(wincache_ucache_get($key . '_expires'));
86: if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
87: return false;
88: }
89: return wincache_ucache_get($key);
90: }
91:
92: /**
93: * Increments the value of an integer cached key
94: *
95: * @param string $key Identifier for the data
96: * @param integer $offset How much to increment
97: * @return New incremented value, false otherwise
98: */
99: public function increment($key, $offset = 1) {
100: return wincache_ucache_inc($key, $offset);
101: }
102:
103: /**
104: * Decrements the value of an integer cached key
105: *
106: * @param string $key Identifier for the data
107: * @param integer $offset How much to subtract
108: * @return New decremented value, false otherwise
109: */
110: public function decrement($key, $offset = 1) {
111: return wincache_ucache_dec($key, $offset);
112: }
113:
114: /**
115: * Delete a key from the cache
116: *
117: * @param string $key Identifier for the data
118: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
119: */
120: public function delete($key) {
121: return wincache_ucache_delete($key);
122: }
123:
124: /**
125: * Delete all keys from the cache. This will clear every
126: * item in the cache matching the cache config prefix.
127: *
128: * @param boolean $check If true, nothing will be cleared, as entries will
129: * naturally expire in wincache..
130: * @return boolean True Returns true.
131: */
132: public function clear($check) {
133: if ($check) {
134: return true;
135: }
136: $info = wincache_ucache_info();
137: $cacheKeys = $info['ucache_entries'];
138: unset($info);
139: foreach ($cacheKeys as $key) {
140: if (strpos($key['key_name'], $this->settings['prefix']) === 0) {
141: wincache_ucache_delete($key['key_name']);
142: }
143: }
144: return true;
145: }
146:
147: /**
148: * Returns the `group value` for each of the configured groups
149: * If the group initial value was not found, then it initializes
150: * the group accordingly.
151: *
152: * @return array
153: */
154: public function groups() {
155: if (empty($this->_compiledGroupNames)) {
156: foreach ($this->settings['groups'] as $group) {
157: $this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
158: }
159: }
160:
161: $groups = wincache_ucache_get($this->_compiledGroupNames);
162: if (count($groups) !== count($this->settings['groups'])) {
163: foreach ($this->_compiledGroupNames as $group) {
164: if (!isset($groups[$group])) {
165: wincache_ucache_set($group, 1);
166: $groups[$group] = 1;
167: }
168: }
169: ksort($groups);
170: }
171:
172: $result = array();
173: $groups = array_values($groups);
174: foreach ($this->settings['groups'] as $i => $group) {
175: $result[] = $group . $groups[$i];
176: }
177: return $result;
178: }
179:
180: /**
181: * Increments the group value to simulate deletion of all keys under a group
182: * old values will remain in storage until they expire.
183: *
184: * @return boolean success
185: */
186: public function clearGroup($group) {
187: $success = null;
188: wincache_ucache_inc($this->settings['prefix'] . $group, 1, $success);
189: return $success;
190: }
191:
192: }
193: