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