memcache.php
Go to the documentation of this file.00001 <?php 00002 /* SVN FILE: $Id: memcache_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */ 00003 /** 00004 * Memcache 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.4933 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 * Memcache storage engine for cache 00030 * 00031 * @package cake 00032 * @subpackage cake.cake.libs.cache 00033 */ 00034 class MemcacheEngine extends CacheEngine { 00035 /** 00036 * Memcache wrapper. 00037 * 00038 * @var object 00039 * @access private 00040 */ 00041 var $__Memcache = null; 00042 /** 00043 * settings 00044 * servers = string or array of memcache servers, default => 127.0.0.1 00045 * compress = boolean, default => false 00046 * 00047 * @var array 00048 * @access public 00049 */ 00050 var $settings = array(); 00051 /** 00052 * Initialize the Cache Engine 00053 * 00054 * Called automatically by the cache frontend 00055 * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); 00056 * 00057 * @param array $setting array of setting for the engine 00058 * @return boolean True if the engine has been successfully initialized, false if not 00059 * @access public 00060 */ 00061 function init($settings = array()) { 00062 if (!class_exists('Memcache')) { 00063 return false; 00064 } 00065 parent::init(array_merge(array( 00066 'engine'=> 'Memcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'servers' => array('127.0.0.1'), 'compress'=> false 00067 ), $settings) 00068 ); 00069 00070 if ($this->settings['compress']) { 00071 $this->settings['compress'] = MEMCACHE_COMPRESSED; 00072 } 00073 if (!is_array($this->settings['servers'])) { 00074 $this->settings['servers'] = array($this->settings['servers']); 00075 } 00076 00077 $this->__Memcache =& new Memcache(); 00078 foreach ($this->settings['servers'] as $server) { 00079 $parts = explode(':', $server); 00080 $host = $parts[0]; 00081 $port = 11211; 00082 if (isset($parts[1])) { 00083 $port = $parts[1]; 00084 } 00085 if ($this->__Memcache->addServer($host, $port)) { 00086 if ($this->__Memcache->connect($host, $port)) { 00087 return true; 00088 } 00089 } 00090 } 00091 return false; 00092 } 00093 /** 00094 * Write data for key into cache 00095 * 00096 * @param string $key Identifier for the data 00097 * @param mixed $value Data to be cached 00098 * @param integer $duration How long to cache the data, in seconds 00099 * @return boolean True if the data was succesfully cached, false on failure 00100 * @access public 00101 */ 00102 function write($key, &$value, $duration) { 00103 return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration); 00104 } 00105 /** 00106 * Read a key from the cache 00107 * 00108 * @param string $key Identifier for the data 00109 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it 00110 * @access public 00111 */ 00112 function read($key) { 00113 return $this->__Memcache->get($key); 00114 } 00115 /** 00116 * Delete a key from the cache 00117 * 00118 * @param string $key Identifier for the data 00119 * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed 00120 * @access public 00121 */ 00122 function delete($key) { 00123 return $this->__Memcache->delete($key); 00124 } 00125 /** 00126 * Delete all keys from the cache 00127 * 00128 * @return boolean True if the cache was succesfully cleared, false otherwise 00129 * @access public 00130 */ 00131 function clear() { 00132 return $this->__Memcache->flush(); 00133 } 00134 /** 00135 * Connects to a server in connection pool 00136 * 00137 * @param string $host host ip address or name 00138 * @param integer $port Server port 00139 * @return boolean True if memcache server was connected 00140 * @access public 00141 */ 00142 function connect($host, $port = 11211) { 00143 if ($this->__Memcache->getServerStatus($host, $port) === 0) { 00144 if ($this->__Memcache->connect($host, $port)) { 00145 return true; 00146 } 00147 return false; 00148 } 00149 return true; 00150 } 00151 } 00152 ?>