Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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