Changeset 2092

Show
Ignore:
Timestamp:
07/26/2007 11:01:17 AM (15 months ago)
Author:
carl
Message:

#623 - MemCache? to support multiple servers

Location:
trunk
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/framework/Caching/TMemCache.php

    r1398 r2092  
    44 * 
    55 * @author Qiang Xue <qiang.xue@gmail.com> 
     6 * @author Carl G. Mathisen <carlgmathisen@gmail.com> 
    67 * @link http://www.pradosoft.com/ 
    78 * @copyright Copyright &copy; 2005 PradoSoft 
     
    4950 * </code> 
    5051 * 
     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 * 
    5166 * If loaded, TMemCache will register itself with {@link TApplication} as the 
    5267 * cache module. It can be accessed via {@link TApplication::getCache()}. 
     
    86101         */ 
    87102        private $_port=11211; 
     103        /** 
     104         * @var boolean controls the use of a persistent connection. Default to true. 
     105         */ 
     106    private $_persistence = true; 
     107    /** 
     108     * @var integer number of buckets to create for this server which in turn control its 
     109     * probability of it being selected. The probability is relative to the total weight  
     110     * of all servers. 
     111     */ 
     112    private $_weight = 1; 
     113     
     114    private $_timeout = 360; 
     115 
     116    private $_retryInterval = 15; 
     117     
     118    private $_status = true; 
     119     
     120    private $_failureCallback = null; 
     121 
     122        /** 
     123         * @var array list of servers available 
     124         */ 
     125        private $_servers=array(); 
    88126 
    89127        /** 
     
    111149                        throw new TConfigurationException('memcache_extension_required'); 
    112150                $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); 
     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); 
    115170                $this->_initialized=true; 
    116171                parent::init($config); 
     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            } 
    117210        } 
    118211 
  • trunk/tests/unit/Caching/AllTests.php

    r1992 r2092  
    88require_once 'TSqliteCacheTest.php'; 
    99require_once 'TAPCCacheTest.php'; 
     10require_once 'TMemCacheTest.php'; 
    1011 
    1112class Caching_AllTests { 
     
    1920        $suite->addTestSuite('TSqliteCacheTest'); 
    2021        $suite->addTestSuite('TAPCCacheTest'); 
     22        $suite->addTestSuite('TMemCacheTest'); 
    2123         
    2224    return $suite;