| | 52 | * You can configure TMemCache two different ways. If you only need one memcache server |
| | 53 | * you may use the method as follows. |
| | 54 | * <code> |
| | 55 | * <module id="cache" class="System.Caching.TMemCache" Host="localhost" Port="11211" /> |
| | 56 | * </code> |
| | 57 | * |
| | 58 | * If you want a more complex configuration, you may use the method as follows. |
| | 59 | * <code> |
| | 60 | * <module id="cache" classs="System.Caching.TMemCache"> |
| | 61 | * <server Host="localhost" Port="11211" Weight="1" Timeout="300" RetryInterval="15" /> |
| | 62 | * <server Host="anotherhost" Port="11211" Weight="1" Timeout="300" RetryInterval="15" /> |
| | 63 | * </module> |
| | 64 | * </code> |
| | 65 | * |
| 113 | | if($this->_cache->connect($this->_host,$this->_port)===false) |
| 114 | | throw new TConfigurationException('memcache_connection_failed',$this->_host,$this->_port); |
| | 151 | $this->loadConfig($config); |
| | 152 | if(count($this->_servers)) |
| | 153 | { |
| | 154 | foreach($this->_servers as $server) |
| | 155 | { |
| | 156 | Prado::trace('Adding server '.$server['Host'].' from serverlist', 'System.Caching.TMemCache'); |
| | 157 | if($this->_cache->addServer($server['Host'],$server['Port'],$server['Persistent'], |
| | 158 | $server['Weight'],$server['Timeout'],$server['RetryInterval'])===false) |
| | 159 | throw new TConfigurationException('memcache_connection_failed',$server['Host'],$server['Port']); |
| | 160 | } |
| | 161 | } |
| | 162 | else |
| | 163 | { |
| | 164 | Prado::trace('Adding server '.$this->_host, 'System.Caching.TMemCache'); |
| | 165 | if($this->_cache->addServer($this->_host,$this->_port)===false) |
| | 166 | throw new TConfigurationException('memcache_connection_failed',$this->_host,$this->_port); |
| | 167 | } |
| | 168 | //if($this->_cache->connect($this->_host,$this->_port)===false) |
| | 169 | // throw new TConfigurationException('memcache_connection_failed',$this->_host,$this->_port); |
| | 172 | } |
| | 173 | |
| | 174 | /** |
| | 175 | * Loads configuration from an XML element |
| | 176 | * @param TXmlElement configuration node |
| | 177 | * @throws TConfigurationException if log route class or type is not specified |
| | 178 | */ |
| | 179 | private function loadConfig($xml) |
| | 180 | { |
| | 181 | if($xml instanceof TXmlElement) |
| | 182 | { |
| | 183 | foreach($xml->getElementsByTagName('server') as $serverConfig) |
| | 184 | { |
| | 185 | $properties=$serverConfig->getAttributes(); |
| | 186 | if(($host=$properties->remove('Host'))===null) |
| | 187 | throw new TConfigurationException('memcache_serverhost_required'); |
| | 188 | if(($port=$properties->remove('Port'))===null) |
| | 189 | throw new TConfigurationException('memcache_serverport_required'); |
| | 190 | if(!is_numeric($port)) |
| | 191 | throw new TConfigurationException('memcache_serverport_invalid'); |
| | 192 | $server = array('Host'=>$host,'Port'=>$port,'Weight'=>1,'Timeout'=>1800,'RetryInterval'=>15,'Persistent'=>true); |
| | 193 | $checks = array( |
| | 194 | 'Weight'=>'memcache_serverweight_invalid', |
| | 195 | 'Timeout'=>'memcache_servertimeout_invalid', |
| | 196 | 'RetryInterval'=>'memcach_serverretryinterval_invalid' |
| | 197 | ); |
| | 198 | foreach($checks as $property=>$exception) |
| | 199 | { |
| | 200 | $value=$properties->remove($property); |
| | 201 | if($value!==null && is_numeric($value)) |
| | 202 | $server[$property]=$value; |
| | 203 | else if($value!==null) |
| | 204 | throw new TConfigurationException($exception); |
| | 205 | } |
| | 206 | $server['Persistent']= TPropertyValue::ensureBoolean($properties->remove('Persistent')); |
| | 207 | $this->_servers[]=$server; |
| | 208 | } |
| | 209 | } |