xcache.php
Go to the documentation of this file.00001 <?php 00002 /* SVN FILE: $Id: xcache_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */ 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. 00011 * 1785 E. Sahara Avenue, Suite 490-204 00012 * Las Vegas, Nevada 89104 00013 * 00014 * Licensed under The MIT License 00015 * Redistributions of files must retain the above copyright notice. 00016 * 00017 * @filesource 00018 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. 00019 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 00020 * @package cake 00021 * @subpackage cake.cake.libs.cache 00022 * @since CakePHP(tm) v 1.2.0.4947 00023 * @version $Revision: 580 $ 00024 * @modifiedby $LastChangedBy: gwoo $ 00025 * @lastmodified $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $ 00026 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 00027 */ 00028 /** 00029 * Xcache storage engine for cache 00030 * 00031 * @link http://trac.lighttpd.net/xcache/ Xcache 00032 * @package cake 00033 * @subpackage cake.cake.libs.cache 00034 */ 00035 class XcacheEngine extends CacheEngine { 00036 /** 00037 * settings 00038 * PHP_AUTH_USER = xcache.admin.user, default cake 00039 * PHP_AUTH_PW = xcache.admin.password, default cake 00040 * 00041 * @var array 00042 * @access public 00043 */ 00044 var $settings = array(); 00045 /** 00046 * Initialize the Cache Engine 00047 * 00048 * Called automatically by the cache frontend 00049 * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); 00050 * 00051 * @param array $setting array of setting for the engine 00052 * @return boolean True if the engine has been successfully initialized, false if not 00053 * @access public 00054 */ 00055 function init($settings) { 00056 parent::init(array_merge(array( 00057 'engine' => 'Xcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password' 00058 ), $settings) 00059 ); 00060 return function_exists('xcache_info'); 00061 } 00062 /** 00063 * Write data for key into cache 00064 * 00065 * @param string $key Identifier for the data 00066 * @param mixed $value Data to be cached 00067 * @param integer $duration How long to cache the data, in seconds 00068 * @return boolean True if the data was succesfully cached, false on failure 00069 * @access public 00070 */ 00071 function write($key, &$value, $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 return xcache_get($key); 00084 } 00085 return false; 00086 } 00087 /** 00088 * Delete a key from the cache 00089 * 00090 * @param string $key Identifier for the data 00091 * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed 00092 * @access public 00093 */ 00094 function delete($key) { 00095 return xcache_unset($key); 00096 } 00097 /** 00098 * Delete all keys from the cache 00099 * 00100 * @return boolean True if the cache was succesfully cleared, false otherwise 00101 * @access public 00102 */ 00103 function clear() { 00104 $result = true; 00105 $this->__auth(); 00106 for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) { 00107 if (!xcache_clear_cache(XC_TYPE_VAR, $i)) { 00108 $result = false; 00109 break; 00110 } 00111 } 00112 $this->__auth(true); 00113 return $result; 00114 } 00115 /** 00116 * Populates and reverses $_SERVER authentication values 00117 * Makes necessary changes (and reverting them back) in $_SERVER 00118 * 00119 * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth 00120 * (see xcache.admin configuration settings) 00121 * 00122 * @param boolean Revert changes 00123 * @access private 00124 */ 00125 function __auth($reverse = false) { 00126 static $backup = array(); 00127 $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW'); 00128 foreach ($keys as $key) { 00129 if ($reverse) { 00130 if (isset($backup[$key])) { 00131 $_SERVER[$key] = $backup[$key]; 00132 unset($backup[$key]); 00133 } else { 00134 unset($_SERVER[$key]); 00135 } 00136 } else { 00137 $value = env($key); 00138 if (!empty($value)) { 00139 $backup[$key] = $value; 00140 } 00141 $varName = '__' . $key; 00142 $_SERVER[$key] = $this->settings[$varName]; 00143 } 00144 } 00145 } 00146 } 00147 ?>