00001 <?php 00002 /* SVN FILE: $Id: xcache.php 8046 2009-02-18 18:07:06Z TommyO $ */ 00003 /** 00004 * Xcache storage engine for cache. 00005 * 00006 * 00007 * PHP versions 4 and 5 00008 * 00009 * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) 00010 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) 00011 * 00012 * Licensed under The MIT License 00013 * Redistributions of files must retain the above copyright notice. 00014 * 00015 * @filesource 00016 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) 00017 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 00018 * @package cake 00019 * @subpackage cake.cake.libs.cache 00020 * @since CakePHP(tm) v 1.2.0.4947 00021 * @version $Revision: 8046 $ 00022 * @modifiedby $LastChangedBy: TommyO $ 00023 * @lastmodified $Date: 2009-02-18 13:07:06 -0500 (Wed, 18 Feb 2009) $ 00024 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 00025 */ 00026 /** 00027 * Xcache storage engine for cache 00028 * 00029 * @link http://trac.lighttpd.net/xcache/ Xcache 00030 * @package cake 00031 * @subpackage cake.cake.libs.cache 00032 */ 00033 class XcacheEngine extends CacheEngine { 00034 /** 00035 * settings 00036 * PHP_AUTH_USER = xcache.admin.user, default cake 00037 * PHP_AUTH_PW = xcache.admin.password, default cake 00038 * 00039 * @var array 00040 * @access public 00041 */ 00042 var $settings = array(); 00043 /** 00044 * Initialize the Cache Engine 00045 * 00046 * Called automatically by the cache frontend 00047 * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); 00048 * 00049 * @param array $setting array of setting for the engine 00050 * @return boolean True if the engine has been successfully initialized, false if not 00051 * @access public 00052 */ 00053 function init($settings) { 00054 parent::init(array_merge(array( 00055 'engine' => 'Xcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password' 00056 ), $settings) 00057 ); 00058 return function_exists('xcache_info'); 00059 } 00060 /** 00061 * Write data for key into cache 00062 * 00063 * @param string $key Identifier for the data 00064 * @param mixed $value Data to be cached 00065 * @param integer $duration How long to cache the data, in seconds 00066 * @return boolean True if the data was succesfully cached, false on failure 00067 * @access public 00068 */ 00069 function write($key, &$value, $duration) { 00070 $expires = time() + $duration; 00071 xcache_set($key.'_expires', $expires, $duration); 00072 return xcache_set($key, $value, $duration); 00073 } 00074 /** 00075 * Read a key from the cache 00076 * 00077 * @param string $key Identifier for the data 00078 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it 00079 * @access public 00080 */ 00081 function read($key) { 00082 if (xcache_isset($key)) { 00083 $time = time(); 00084 $cachetime = intval(xcache_get($key.'_expires')); 00085 if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) { 00086 return false; 00087 } 00088 return xcache_get($key); 00089 } 00090 return false; 00091 } 00092 /** 00093 * Delete a key from the cache 00094 * 00095 * @param string $key Identifier for the data 00096 * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed 00097 * @access public 00098 */ 00099 function delete($key) { 00100 return xcache_unset($key); 00101 } 00102 /** 00103 * Delete all keys from the cache 00104 * 00105 * @return boolean True if the cache was succesfully cleared, false otherwise 00106 * @access public 00107 */ 00108 function clear() { 00109 $this->__auth(); 00110 $max = xcache_count(XC_TYPE_VAR); 00111 for ($i = 0; $i < $max; $i++) { 00112 xcache_clear_cache(XC_TYPE_VAR, $i); 00113 } 00114 $this->__auth(true); 00115 return true; 00116 } 00117 /** 00118 * Populates and reverses $_SERVER authentication values 00119 * Makes necessary changes (and reverting them back) in $_SERVER 00120 * 00121 * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth 00122 * (see xcache.admin configuration settings) 00123 * 00124 * @param boolean Revert changes 00125 * @access private 00126 */ 00127 function __auth($reverse = false) { 00128 static $backup = array(); 00129 $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password'); 00130 foreach ($keys as $key => $setting) { 00131 if ($reverse) { 00132 if (isset($backup[$key])) { 00133 $_SERVER[$key] = $backup[$key]; 00134 unset($backup[$key]); 00135 } else { 00136 unset($_SERVER[$key]); 00137 } 00138 } else { 00139 $value = env($key); 00140 if (!empty($value)) { 00141 $backup[$key] = $value; 00142 } 00143 if (!empty($this->settings[$setting])) { 00144 $_SERVER[$key] = $this->settings[$setting]; 00145 } else if (!empty($this->settings[$key])) { 00146 $_SERVER[$key] = $this->settings[$key]; 00147 } else { 00148 $_SERVER[$key] = $value; 00149 } 00150 } 00151 } 00152 } 00153 } 00154 ?>
1.4.7