<?php
class RedisCluster{
private $_isUseCluster = false;
private $_sn = 0;
private $_linkHandle = array(
'master'=>null,
'slave'=>array(),
);
public function __construct($isUseCluster=false){
$this->_isUseCluster = $isUseCluster;
}
public function connect($config=array('host'=>'127.0.0.1','port'=>6379), $isMaster=true){
if(!isset($config['port'])){
$config['port'] = 6379;
}
if($isMaster){
$this->_linkHandle['master'] = new Redis();
$ret = $this->_linkHandle['master']->pconnect($config['host'],$config['port']);
}else{
$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){
case 0:
$this->getRedis()->close();
break;
case 1:
for($i=0; $i<$this->_sn; ++$i){
$this->_linkHandle['slave'][$i]->close();
}
break;
case 1:
$this->getRedis()->close();
for($i=0; $i<$this->_sn; ++$i){
$this->_linkHandle['slave'][$i]->close();
}
break;
}
return true;
}
public function getRedis($isMaster=true,$slaveOne=true){
if($isMaster){
return $this->_linkHandle['master'];
}else{
return $slaveOne ? $this->_getSlaveRedis() : $this->_linkHandle['slave'];
}
}
public function set($key, $value, $expire=0){
if($expire == 0){
$ret = $this->getRedis()->set($key, $value);
}else{
$ret = $this->getRedis()->setex($key, $expire, $value);
}
return $ret;
}
public function get($key){
$func = is_array($key) ? 'mGet' : 'get';
if(! $this->_isUseCluster){
return $this->getRedis()->{$func}($key);
}
return $this->_getSlaveRedis()->{$func}($key);
}
public function setnx($key, $value){
return $this->getRedis()->setnx($key, $value);
}
public function remove($key){
return $this->getRedis()->delete($key);
}
public function incr($key,$default=1){
if($default == 1){
return $this->getRedis()->incr($key);
}else{
return $this->getRedis()->incrBy($key, $default);
}
}
public function decr($key,$default=1){
if($default == 1){
return $this->getRedis()->decr($key);
}else{
return $this->getRedis()->decrBy($key, $default);
}
}
public function clear(){
return $this->getRedis()->flushDB();
}
private function _getSlaveRedis(){
if($this->_sn <= 1){
return $this->_linkHandle['slave'][0];
}
$hash = $this->_hashId(mt_rand(), $this->_sn);
return $this->_linkHandle['slave'][$hash];
}
private function _hashId($id,$m=10)
{
$k = md5($id);
$l = strlen($k);
$b = bin2hex($k);
$h = 0;
for($i=0;$i<$l;$i++)
{
$h += substr($b,$i*2,2);
}
$hash = ($h*1)%$m;
return $hash;
}
public function lpush($key,$value){
return $this->getRedis()->lpush($key,$value);
}
public function lpop($key){
return $this->getRedis()->lpop($key);
}
public function lrange($key,$start,$end){
return $this->getRedis()->lrange($key,$start,$end);
}
public function hset($name,$key,$value){
if(is_array($value)){
return $this->getRedis()->hset($name,$key,serialize($value));
}
return $this->getRedis()->hset($name,$key,$value);
}
public function hget($name,$key = null,$serialize=true){
if($key){
$row = $this->getRedis()->hget($name,$key);
if($row && $serialize){
unserialize($row);
}
return $row;
}
return $this->getRedis()->hgetAll($name);
}
public function hdel($name,$key = null){
if($key){
return $this->getRedis()->hdel($name,$key);
}
return $this->getRedis()->hdel($name);
}
public function multi(){
return $this->getRedis()->multi();
}
public function exec(){
return $this->getRedis()->exec();
}
}
$redis = new RedisCluster();
$redis->connect(array('host'=>'127.0.0.1','port'=>6379));
$cron_id = 10001;
$CRON_KEY = 'CRON_LIST';
$PHONE_KEY = 'PHONE_LIST:'.$cron_id;
$cron = $redis->hget($CRON_KEY,$cron_id);
if(empty($cron)){
$cron = array('id'=>10,'name'=>'jackluo');
$redis->hset($CRON_KEY,$cron_id,$cron);
}
$phone_list = $redis->lrange($PHONE_KEY,0,-1);
print_r($phone_list);
if(empty($phone_list)){
$phone_list =explode(',','13228191831,18608041585');
if($phone_list){
$redis->multi();
foreach ($phone_list as $phone) {
$redis->lpush($PHONE_KEY,$phone);
}
$redis->exec();
}
}
print_r($phone_list);