This commit is contained in:
thinkphp
2016-05-07 17:42:07 +08:00
4 changed files with 74 additions and 36 deletions

View File

@@ -115,4 +115,14 @@ class Redis
return $this->handler->flushDB();
}
/**
* 返回句柄对象,可执行其它高级方法
*
* @access public
* @return object
*/
public function handler()
{
return $this->handler;
}
}

View File

@@ -18,21 +18,21 @@ use think\Log;
/**
配置参数:
'cache' => [
'type' => 'Redisd'
'host' => 'A:6379,B:6379', //redis服务器ip多台用逗号隔开读写分离开启时默认写A当A主挂时再尝试写B
'slave' => 'B:6379,C:6379', //redis服务器ip多台用逗号隔开读写分离开启时所有IP随机读其中一台挂时尝试读其它节点可以配置权重
'port' => 6379, //默认的端口号
'password' => '', //AUTH认证密码当redis服务直接暴露在外网时推荐
'timeout' => 10, //连接超时时间
'expire' => false, //默认过期时间,默认0为永不过期
'prefix' => '', //缓存前缀,不宜过长
'persistent' => false, //是否长连接 false=短连接,推荐长连接
'type' => 'Redisd'
'host' => 'A:6379,B:6379', //redis服务器ip多台用逗号隔开读写分离开启时默认写A当A主挂时再尝试写B
'slave' => 'B:6379,C:6379', //redis服务器ip多台用逗号隔开读写分离开启时所有IP随机读其中一台挂时尝试读其它节点可以配置权重
'port' => 6379, //默认的端口号
'password' => '', //AUTH认证密码当redis服务直接暴露在外网时推荐
'timeout' => 10, //连接超时时间
'expire' => false, //默认过期时间,默认为永不过期
'prefix' => '', //缓存前缀,不宜过长
'persistent' => false, //是否长连接 false=短连接,推荐长连接
],
单例获取:
$redis = \think\Cache::connect(Config::get('cache'));
$redis->master(true);
$redis->get('key');
$redis = \think\Cache::connect(Config::get('cache'));
$redis->master(true)->setnx('key');
$redis->master(false)->get('key');
*/
/**
@@ -60,6 +60,7 @@ class Redisd
{
protected static $redis_rw_handler;
protected static $redis_err_pool;
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
@@ -146,12 +147,12 @@ class Redisd
//In any other problematic case that does not involve an unreachable server
//(such as a key not existing, an invalid command, etc), phpredis will return FALSE.
Log::record(sprintf("redisd->%s:%s:%s", $master ? "master" : "salve", $host, $e->getMessage()), Log::ALERT);
Log::record(sprintf("redisd->%s:%s:%s:%s", $master ? "master" : "salve", $host, $port, $e->getMessage()), Log::ALERT);
//主节点挂了以后,尝试连接主备,断开主备的主从连接进行升主
if ($master) {
if (!count($this->options["server_master_failover"])) {
throw new Exception("redisd master: no more server_master_failover. {$host} : " . $e->getMessage());
throw new Exception("redisd master: no more server_master_failover. {$host}:{$port} : " . $e->getMessage());
return false;
}
@@ -169,16 +170,15 @@ class Redisd
if (trim($v) == trim($host)) {
unset($this->options["server_slave"][$k]);
}
}
//如果无可用节点,则抛出异常
if (!count($this->options["server_slave"])) {
Log::record("已无可用Redis读节点", Log::ERROR);
throw new Exception("redisd slave: no more server_slave. {$host} : " . $e->getMessage());
throw new Exception("redisd slave: no more server_slave. {$host}:{$port} : " . $e->getMessage());
return false;
} else {
Log::record("salve {$host} is down, try another one.", Log::ALERT);
Log::record("salve {$host}:{$port} is down, try another one.", Log::ALERT);
return $this->master(false);
}
}
@@ -186,7 +186,7 @@ class Redisd
throw new Exception($e->getMessage(), $e->getCode());
}
self::$redis_rw_handler[$master] = $this->handler;
return self::$redis_rw_handler[$master] = $this->handler;
}
/**
@@ -204,8 +204,9 @@ class Redisd
$value = $this->handler->get($this->options['prefix'] . $name);
} catch (\RedisException $e) {
unset(self::$redis_rw_handler[0]);
$this->master();
$this->get($name);
return $this->get($name);
} catch (\Exception $e) {
Log::record($e->getMessage(), Log::ERROR);
}
@@ -257,8 +258,9 @@ class Redisd
}
} catch (\RedisException $e) {
unset(self::$redis_rw_handler[1]);
$this->master(true);
$this->set($name, $value, $expire);
return $this->set($name, $value, $expire);
} catch (\Exception $e) {
Log::record($e->getMessage());
}
@@ -266,18 +268,6 @@ class Redisd
return $result;
}
/**
* 返回句柄对象
* 需要先执行 $redis->master() 连接到 DB
*
* @access public
* @return object
*/
public function handler()
{
return $this->handler;
}
/**
* 删除缓存
*
@@ -302,6 +292,18 @@ class Redisd
$this->master(true);
return $this->handler->flushDB();
}
/**
* 返回句柄对象,可执行其它高级方法
* 需要先执行 $redis->master() 连接到 DB
*
* @access public
* @return object
*/
public function handler()
{
return $this->handler;
}
/**
* 析构释放连接