1: <?php
2: /**
3: * Xcache storage engine for cache.
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.Cache.Engine
15: * @since CakePHP(tm) v 1.2.0.4947
16: * @license http://www.opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: /**
20: * Xcache storage engine for cache
21: *
22: * @link http://trac.lighttpd.net/xcache/ Xcache
23: * @package Cake.Cache.Engine
24: */
25: class XcacheEngine extends CacheEngine {
26:
27: /**
28: * Settings
29: *
30: * - PHP_AUTH_USER = xcache.admin.user, default cake
31: * - PHP_AUTH_PW = xcache.admin.password, default cake
32: *
33: * @var array
34: */
35: public $settings = array();
36:
37: /**
38: * Initialize the Cache Engine
39: *
40: * Called automatically by the cache frontend
41: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
42: *
43: * @param array $settings array of setting for the engine
44: * @return boolean True if the engine has been successfully initialized, false if not
45: */
46: public function init($settings = array()) {
47: if (php_sapi_name() !== 'cli') {
48: parent::init(array_merge(array(
49: 'engine' => 'Xcache',
50: 'prefix' => Inflector::slug(APP_DIR) . '_',
51: 'PHP_AUTH_USER' => 'user',
52: 'PHP_AUTH_PW' => 'password'
53: ), $settings)
54: );
55: return function_exists('xcache_info');
56: }
57: return false;
58: }
59:
60: /**
61: * Write data for key into cache
62: *
63: * @param string $key Identifier for the data
64: * @param mixed $value Data to be cached
65: * @param integer $duration How long to cache the data, in seconds
66: * @return boolean True if the data was successfully cached, false on failure
67: */
68: public function write($key, $value, $duration) {
69: $expires = time() + $duration;
70: xcache_set($key . '_expires', $expires, $duration);
71: return xcache_set($key, $value, $duration);
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 there was an error fetching it
79: */
80: public function read($key) {
81: if (xcache_isset($key)) {
82: $time = time();
83: $cachetime = intval(xcache_get($key . '_expires'));
84: if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
85: return false;
86: }
87: return xcache_get($key);
88: }
89: return false;
90: }
91:
92: /**
93: * Increments the value of an integer cached key
94: * If the cache key is not an integer it will be treated as 0
95: *
96: * @param string $key Identifier for the data
97: * @param integer $offset How much to increment
98: * @return New incremented value, false otherwise
99: */
100: public function increment($key, $offset = 1) {
101: return xcache_inc($key, $offset);
102: }
103:
104: /**
105: * Decrements the value of an integer cached key.
106: * If the cache key is not an integer it will be treated as 0
107: *
108: * @param string $key Identifier for the data
109: * @param integer $offset How much to subtract
110: * @return New decremented value, false otherwise
111: */
112: public function decrement($key, $offset = 1) {
113: return xcache_dec($key, $offset);
114: }
115:
116: /**
117: * Delete a key from the cache
118: *
119: * @param string $key Identifier for the data
120: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
121: */
122: public function delete($key) {
123: return xcache_unset($key);
124: }
125:
126: /**
127: * Delete all keys from the cache
128: *
129: * @param boolean $check
130: * @return boolean True if the cache was successfully cleared, false otherwise
131: */
132: public function clear($check) {
133: $this->_auth();
134: $max = xcache_count(XC_TYPE_VAR);
135: for ($i = 0; $i < $max; $i++) {
136: xcache_clear_cache(XC_TYPE_VAR, $i);
137: }
138: $this->_auth(true);
139: return true;
140: }
141:
142: /**
143: * Returns the `group value` for each of the configured groups
144: * If the group initial value was not found, then it initializes
145: * the group accordingly.
146: *
147: * @return array
148: */
149: public function groups() {
150: $result = array();
151: foreach ($this->settings['groups'] as $group) {
152: $value = xcache_get($this->settings['prefix'] . $group);
153: if (!$value) {
154: $value = 1;
155: xcache_set($this->settings['prefix'] . $group, $value, 0);
156: }
157: $result[] = $group . $value;
158: }
159: return $result;
160: }
161:
162: /**
163: * Increments the group value to simulate deletion of all keys under a group
164: * old values will remain in storage until they expire.
165: *
166: * @return boolean success
167: */
168: public function clearGroup($group) {
169: return (bool)xcache_inc($this->settings['prefix'] . $group, 1);
170: }
171:
172: /**
173: * Populates and reverses $_SERVER authentication values
174: * Makes necessary changes (and reverting them back) in $_SERVER
175: *
176: * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
177: * (see xcache.admin configuration settings)
178: *
179: * @param boolean $reverse Revert changes
180: * @return void
181: */
182: protected function _auth($reverse = false) {
183: static $backup = array();
184: $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
185: foreach ($keys as $key => $setting) {
186: if ($reverse) {
187: if (isset($backup[$key])) {
188: $_SERVER[$key] = $backup[$key];
189: unset($backup[$key]);
190: } else {
191: unset($_SERVER[$key]);
192: }
193: } else {
194: $value = env($key);
195: if (!empty($value)) {
196: $backup[$key] = $value;
197: }
198: if (!empty($this->settings[$setting])) {
199: $_SERVER[$key] = $this->settings[$setting];
200: } elseif (!empty($this->settings[$key])) {
201: $_SERVER[$key] = $this->settings[$key];
202: } else {
203: $_SERVER[$key] = $value;
204: }
205: }
206: }
207: }
208: }
209: