1: <?php
2: /**
3: * Xcache storage engine for cache.
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
14: * @link http://cakephp.org CakePHP(tm) Project
15: * @package Cake.Cache.Engine
16: * @since CakePHP(tm) v 1.2.0.4947
17: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
18: */
19:
20: /**
21: * Xcache storage engine for cache
22: *
23: * @link http://trac.lighttpd.net/xcache/ Xcache
24: * @package Cake.Cache.Engine
25: */
26: class XcacheEngine extends CacheEngine {
27:
28: /**
29: * Settings
30: *
31: * - PHP_AUTH_USER = xcache.admin.user, default cake
32: * - PHP_AUTH_PW = xcache.admin.password, default cake
33: *
34: * @var array
35: */
36: public $settings = 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: */
47: public function init($settings = array()) {
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:
58: /**
59: * Write data for key into cache
60: *
61: * @param string $key Identifier for the data
62: * @param mixed $value Data to be cached
63: * @param integer $duration How long to cache the data, in seconds
64: * @return boolean True if the data was successfully cached, false on failure
65: */
66: public function write($key, $value, $duration) {
67: $expires = time() + $duration;
68: xcache_set($key . '_expires', $expires, $duration);
69: return xcache_set($key, $value, $duration);
70: }
71:
72: /**
73: * Read a key from the cache
74: *
75: * @param string $key Identifier for the data
76: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
77: */
78: public function read($key) {
79: if (xcache_isset($key)) {
80: $time = time();
81: $cachetime = intval(xcache_get($key . '_expires'));
82: if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
83: return false;
84: }
85: return xcache_get($key);
86: }
87: return false;
88: }
89:
90: /**
91: * Increments the value of an integer cached key
92: * If the cache key is not an integer it will be treated as 0
93: *
94: * @param string $key Identifier for the data
95: * @param integer $offset How much to increment
96: * @return New incremented value, false otherwise
97: */
98: public function increment($key, $offset = 1) {
99: return xcache_inc($key, $offset);
100: }
101:
102: /**
103: * Decrements the value of an integer cached key.
104: * If the cache key is not an integer it will be treated as 0
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 xcache_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 xcache_unset($key);
122: }
123:
124: /**
125: * Delete all keys from the cache
126: *
127: * @param boolean $check
128: * @return boolean True if the cache was successfully cleared, false otherwise
129: */
130: public function clear($check) {
131: $this->_auth();
132: $max = xcache_count(XC_TYPE_VAR);
133: for ($i = 0; $i < $max; $i++) {
134: xcache_clear_cache(XC_TYPE_VAR, $i);
135: }
136: $this->_auth(true);
137: return true;
138: }
139:
140: /**
141: * Populates and reverses $_SERVER authentication values
142: * Makes necessary changes (and reverting them back) in $_SERVER
143: *
144: * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
145: * (see xcache.admin configuration settings)
146: *
147: * @param boolean $reverse Revert changes
148: * @return void
149: */
150: protected function _auth($reverse = false) {
151: static $backup = array();
152: $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
153: foreach ($keys as $key => $setting) {
154: if ($reverse) {
155: if (isset($backup[$key])) {
156: $_SERVER[$key] = $backup[$key];
157: unset($backup[$key]);
158: } else {
159: unset($_SERVER[$key]);
160: }
161: } else {
162: $value = env($key);
163: if (!empty($value)) {
164: $backup[$key] = $value;
165: }
166: if (!empty($this->settings[$setting])) {
167: $_SERVER[$key] = $this->settings[$setting];
168: } else if (!empty($this->settings[$key])) {
169: $_SERVER[$key] = $this->settings[$key];
170: } else {
171: $_SERVER[$key] = $value;
172: }
173: }
174: }
175: }
176: }
177: