| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: xcache.php 8004 2009-01-16 20:15:21Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Xcache storage engine for cache.
|
|
|
5 |
*
|
|
|
6 |
*
|
|
|
7 |
* PHP versions 4 and 5
|
|
|
8 |
*
|
|
|
9 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
10 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
11 |
*
|
|
|
12 |
* Licensed under The MIT License
|
|
|
13 |
* Redistributions of files must retain the above copyright notice.
|
|
|
14 |
*
|
|
|
15 |
* @filesource
|
|
|
16 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
17 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
18 |
* @package cake
|
|
|
19 |
* @subpackage cake.cake.libs.cache
|
|
|
20 |
* @since CakePHP(tm) v 1.2.0.4947
|
|
|
21 |
* @version $Revision: 8004 $
|
|
|
22 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
23 |
* @lastmodified $Date: 2009-01-16 12:15:21 -0800 (Fri, 16 Jan 2009) $
|
|
|
24 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
25 |
*/
|
|
|
26 |
/**
|
|
|
27 |
* Xcache storage engine for cache
|
|
|
28 |
*
|
|
|
29 |
* @link http://trac.lighttpd.net/xcache/ Xcache
|
|
|
30 |
* @package cake
|
|
|
31 |
* @subpackage cake.cake.libs.cache
|
|
|
32 |
*/
|
|
|
33 |
class XcacheEngine extends CacheEngine {
|
|
|
34 |
/**
|
|
|
35 |
* settings
|
|
|
36 |
* PHP_AUTH_USER = xcache.admin.user, default cake
|
|
|
37 |
* PHP_AUTH_PW = xcache.admin.password, default cake
|
|
|
38 |
*
|
|
|
39 |
* @var array
|
|
|
40 |
* @access public
|
|
|
41 |
*/
|
|
|
42 |
var $settings = array();
|
|
|
43 |
/**
|
|
|
44 |
* Initialize the Cache Engine
|
|
|
45 |
*
|
|
|
46 |
* Called automatically by the cache frontend
|
|
|
47 |
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
|
|
48 |
*
|
|
|
49 |
* @param array $setting array of setting for the engine
|
|
|
50 |
* @return boolean True if the engine has been successfully initialized, false if not
|
|
|
51 |
* @access public
|
|
|
52 |
*/
|
|
|
53 |
function init($settings) {
|
|
|
54 |
parent::init(array_merge(array(
|
|
|
55 |
'engine' => 'Xcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password'
|
|
|
56 |
), $settings)
|
|
|
57 |
);
|
|
|
58 |
return function_exists('xcache_info');
|
|
|
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 succesfully cached, false on failure
|
|
|
67 |
* @access public
|
|
|
68 |
*/
|
|
|
69 |
function write($key, &$value, $duration) {
|
|
|
70 |
$expires = time() + $duration;
|
|
|
71 |
xcache_set($key.'_expires', $expires, $duration);
|
|
|
72 |
return xcache_set($key, $value, $duration);
|
|
|
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 |
* Delete a key from the cache
|
|
|
94 |
*
|
|
|
95 |
* @param string $key Identifier for the data
|
|
|
96 |
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
|
|
97 |
* @access public
|
|
|
98 |
*/
|
|
|
99 |
function delete($key) {
|
|
|
100 |
return xcache_unset($key);
|
|
|
101 |
}
|
|
|
102 |
/**
|
|
|
103 |
* Delete all keys from the cache
|
|
|
104 |
*
|
|
|
105 |
* @return boolean True if the cache was succesfully cleared, false otherwise
|
|
|
106 |
* @access public
|
|
|
107 |
*/
|
|
|
108 |
function clear() {
|
|
|
109 |
$this->__auth();
|
|
|
110 |
for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
|
|
|
111 |
xcache_clear_cache(XC_TYPE_VAR, $i);
|
|
|
112 |
}
|
|
|
113 |
$this->__auth(true);
|
|
|
114 |
return xcache_count(XC_TYPE_VAR) == 0;
|
|
|
115 |
}
|
|
|
116 |
/**
|
|
|
117 |
* Populates and reverses $_SERVER authentication values
|
|
|
118 |
* Makes necessary changes (and reverting them back) in $_SERVER
|
|
|
119 |
*
|
|
|
120 |
* This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
|
|
|
121 |
* (see xcache.admin configuration settings)
|
|
|
122 |
*
|
|
|
123 |
* @param boolean Revert changes
|
|
|
124 |
* @access private
|
|
|
125 |
*/
|
|
|
126 |
function __auth($reverse = false) {
|
|
|
127 |
static $backup = array();
|
|
|
128 |
$keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
|
|
|
129 |
foreach ($keys as $key => $setting) {
|
|
|
130 |
if ($reverse) {
|
|
|
131 |
if (isset($backup[$key])) {
|
|
|
132 |
$_SERVER[$key] = $backup[$key];
|
|
|
133 |
unset($backup[$key]);
|
|
|
134 |
} else {
|
|
|
135 |
unset($_SERVER[$key]);
|
|
|
136 |
}
|
|
|
137 |
} else {
|
|
|
138 |
$value = env($key);
|
|
|
139 |
if (!empty($value)) {
|
|
|
140 |
$backup[$key] = $value;
|
|
|
141 |
}
|
|
|
142 |
if (!empty($this->settings[$setting])) {
|
|
|
143 |
$_SERVER[$key] = $this->settings[$setting];
|
|
|
144 |
} else if (!empty($this->settings[$key])) {
|
|
|
145 |
$_SERVER[$key] = $this->settings[$key];
|
|
|
146 |
} else {
|
|
|
147 |
$_SERVER[$key] = $value;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
?>
|