| [ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TMemCache class file 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TMemCache.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Caching 11 */ 12 13 /** 14 * TMemCache class 15 * 16 * TMemCache implements a cache application module based on {@link http://www.danga.com/memcached/ memcached}. 17 * 18 * TMemCache can be configured with the Host and Port properties, which 19 * specify the host and port of the memcache server to be used. 20 * By default, they take the value 'localhost' and 11211, respectively. 21 * These properties must be set before {@link init} is invoked. 22 * 23 * The following basic cache operations are implemented: 24 * - {@link get} : retrieve the value with a key (if any) from cache 25 * - {@link set} : store the value with a key into cache 26 * - {@link add} : store the value only if cache does not have this key 27 * - {@link delete} : delete the value with the specified key from cache 28 * - {@link flush} : delete all values from cache 29 * 30 * Each value is associated with an expiration time. The {@link get} operation 31 * ensures that any expired value will not be returned. The expiration time can 32 * be specified by the number of seconds (maximum 60*60*24*30) 33 * or a UNIX timestamp. A expiration time 0 represents never expire. 34 * 35 * By definition, cache does not ensure the existence of a value 36 * even if it never expires. Cache is not meant to be an persistent storage. 37 * 38 * Also note, there is no security measure to protected data in memcache. 39 * All data in memcache can be accessed by any process running in the system. 40 * 41 * To use this module, the memcache PHP extension must be loaded. 42 * 43 * Some usage examples of TMemCache are as follows, 44 * <code> 45 * $cache=new TMemCache; // TMemCache may also be loaded as a Prado application module 46 * $cache->init(null); 47 * $cache->add('object',$object); 48 * $object2=$cache->get('object'); 49 * </code> 50 * 51 * If loaded, TMemCache will register itself with {@link TApplication} as the 52 * cache module. It can be accessed via {@link TApplication::getCache()}. 53 * 54 * TMemCache may be configured in application configuration file as follows 55 * <code> 56 * <module id="cache" class="System.Caching.TMemCache" Host="localhost" Port="11211" /> 57 * </code> 58 * where {@link getHost Host} and {@link getPort Port} are configurable properties 59 * of TMemCache. 60 * 61 * @author Qiang Xue <qiang.xue@gmail.com> 62 * @version $Id: TMemCache.php 1397 2006-09-07 07:55:53Z wei $ 63 * @package System.Caching 64 * @since 3.0 65 */ 66 class TMemCache extends TCache 67 { 68 /** 69 * @var boolean if the module is initialized 70 */ 71 private $_initialized=false; 72 /** 73 * @var Memcache the Memcache instance 74 */ 75 private $_cache=null; 76 /** 77 * @var string a unique prefix used to identify this cache instance from the others 78 */ 79 private $_prefix=null; 80 /** 81 * @var string host name of the memcache server 82 */ 83 private $_host='localhost'; 84 /** 85 * @var integer the port number of the memcache server 86 */ 87 private $_port=11211; 88 89 /** 90 * Destructor. 91 * Disconnect the memcache server. 92 */ 93 public function __destruct() 94 { 95 if($this->_cache!==null) 96 $this->_cache->close(); 97 } 98 99 /** 100 * Initializes this module. 101 * This method is required by the IModule interface. It makes sure that 102 * UniquePrefix has been set, creates a Memcache instance and connects 103 * to the memcache server. 104 * @param TApplication Prado application, can be null 105 * @param TXmlElement configuration for this module, can be null 106 * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails 107 */ 108 public function init($config) 109 { 110 if(!extension_loaded('memcache')) 111 throw new TConfigurationException('memcache_extension_required'); 112 $this->_cache=new Memcache; 113 if($this->_cache->connect($this->_host,$this->_port)===false) 114 throw new TConfigurationException('memcache_connection_failed',$this->_host,$this->_port); 115 $this->_initialized=true; 116 parent::init($config); 117 } 118 119 /** 120 * @return string host name of the memcache server 121 */ 122 public function getHost() 123 { 124 return $this->_host; 125 } 126 127 /** 128 * @param string host name of the memcache server 129 * @throws TInvalidOperationException if the module is already initialized 130 */ 131 public function setHost($value) 132 { 133 if($this->_initialized) 134 throw new TInvalidOperationException('memcache_host_unchangeable'); 135 else 136 $this->_host=$value; 137 } 138 139 /** 140 * @return integer port number of the memcache server 141 */ 142 public function getPort() 143 { 144 return $this->_port; 145 } 146 147 /** 148 * @param integer port number of the memcache server 149 * @throws TInvalidOperationException if the module is already initialized 150 */ 151 public function setPort($value) 152 { 153 if($this->_initialized) 154 throw new TInvalidOperationException('memcache_port_unchangeable'); 155 else 156 $this->_port=TPropertyValue::ensureInteger($value); 157 } 158 159 /** 160 * Retrieves a value from cache with a specified key. 161 * This is the implementation of the method declared in the parent class. 162 * @param string a unique key identifying the cached value 163 * @return string the value stored in cache, false if the value is not in the cache or expired. 164 */ 165 protected function getValue($key) 166 { 167 return $this->_cache->get($key); 168 } 169 170 /** 171 * Stores a value identified by a key in cache. 172 * This is the implementation of the method declared in the parent class. 173 * 174 * @param string the key identifying the value to be cached 175 * @param string the value to be cached 176 * @param integer the number of seconds in which the cached value will expire. 0 means never expire. 177 * @return boolean true if the value is successfully stored into cache, false otherwise 178 */ 179 protected function setValue($key,$value,$expire) 180 { 181 return $this->_cache->set($key,$value,0,$expire); 182 } 183 184 /** 185 * Stores a value identified by a key into cache if the cache does not contain this key. 186 * This is the implementation of the method declared in the parent class. 187 * 188 * @param string the key identifying the value to be cached 189 * @param string the value to be cached 190 * @param integer the number of seconds in which the cached value will expire. 0 means never expire. 191 * @return boolean true if the value is successfully stored into cache, false otherwise 192 */ 193 protected function addValue($key,$value,$expire) 194 { 195 return $this->_cache->add($key,$value,0,$expire); 196 } 197 198 /** 199 * Deletes a value with the specified key from cache 200 * This is the implementation of the method declared in the parent class. 201 * @param string the key of the value to be deleted 202 * @return boolean if no error happens during deletion 203 */ 204 protected function deleteValue($key) 205 { 206 return $this->_cache->delete($key); 207 } 208 209 /** 210 * Deletes all values from cache. 211 * Be careful of performing this operation if the cache is shared by multiple applications. 212 */ 213 public function flush() 214 { 215 return $this->_cache->flush(); 216 } 217 } 218 219 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |