转的 http://www.yiibase.com/server/view/411.html
php
class RedisCluster{
// 是否使用 M/S 的读写集群方案
private $_isUseCluster = false;
// Slave 句柄标记
private $_sn = 0;
// 服务器连接句柄
private $_linkHandle = array(
'master'=>null,// 只支持一台 Master
'slave'=>array(),// 可以有多台 Slave
);
public function __construct($isUseCluster=false){
$this->_isUseCluster = $isUseCluster;
}
public function connect($config=array('host'=>'127.0.0.1','port'=>6379), $isMaster=true){
// default port
if(!isset($config['port'])){
$config['port'] = 6379;
}
// 设置 Master 连接
if($isMaster){
$this->_linkHandle['master'] = new Redis();
$ret = $this->_linkHandle['master']->pconnect($config['host'],$config['port']);
}else{
// 多个 Slave 连接
$this->_linkHandle['slave'][$this->_sn] = new Redis();
$ret = $this->_linkHandle['slave'][$this->_sn]->pconnect($config['host'],$config['port']);
++$this->_sn;
}
return $ret;
}
public function close($flag=2){
switch($flag){