1: <?php
2: /* SVN FILE: $Id$ */
3: /**
4: * Xcache storage engine for cache.
5: *
6: *
7: * PHP versions 4 and 5
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * Redistributions of files must retain the above copyright notice.
14: *
15: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
16: * @link http://cakephp.org CakePHP(tm) Project
17: * @package cake
18: * @subpackage cake.cake.libs.cache
19: * @since CakePHP(tm) v 1.2.0.4947
20: * @version $Revision$
21: * @modifiedby $LastChangedBy$
22: * @lastmodified $Date$
23: * @license http://www.opensource.org/licenses/mit-license.php The MIT License
24: */
25: /**
26: * Xcache storage engine for cache
27: *
28: * @link http://trac.lighttpd.net/xcache/ Xcache
29: * @package cake
30: * @subpackage cake.cake.libs.cache
31: */
32: class XcacheEngine extends CacheEngine {
33: /**
34: * settings
35: * PHP_AUTH_USER = xcache.admin.user, default cake
36: * PHP_AUTH_PW = xcache.admin.password, default cake
37: *
38: * @var array
39: * @access public
40: */
41: var $settings = array();
42: /**
43: * Initialize the Cache Engine
44: *
45: * Called automatically by the cache frontend
46: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
47: *
48: * @param array $setting array of setting for the engine
49: * @return boolean True if the engine has been successfully initialized, false if not
50: * @access public
51: */
52: function init($settings) {
53: parent::init(array_merge(array(
54: 'engine' => 'Xcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password'
55: ), $settings)
56: );
57: return function_exists('xcache_info');
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: * Read a key from the cache
75: *
76: * @param string $key Identifier for the data
77: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
78: * @access public
79: */
80: 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: * Delete a key from the cache
93: *
94: * @param string $key Identifier for the data
95: * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
96: * @access public
97: */
98: function delete($key) {
99: return xcache_unset($key);
100: }
101: /**
102: * Delete all keys from the cache
103: *
104: * @return boolean True if the cache was succesfully cleared, false otherwise
105: * @access public
106: */
107: function clear() {
108: $this->__auth();
109: $max = xcache_count(XC_TYPE_VAR);
110: for ($i = 0; $i < $max; $i++) {
111: xcache_clear_cache(XC_TYPE_VAR, $i);
112: }
113: $this->__auth(true);
114: return true;
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: ?>