部分重构Redisd驱动

This commit is contained in:
尘缘
2016-05-09 19:37:02 +08:00
parent 3868b6e771
commit 6d46bf3276

View File

@@ -72,6 +72,7 @@ class Redisd
'persistent' => false, 'persistent' => false,
'length' => 0, 'length' => 0,
'prefix' => '', 'prefix' => '',
'serialize' => \Redis::SERIALIZER_PHP,
]; ];
/** /**
@@ -107,8 +108,9 @@ class Redisd
* *
* @access public * @access public
* @param bool $master true 默认主写 * @param bool $master true 默认主写
* @return Redisd
*/ */
public function master($master = false) public function master($master = true)
{ {
if (isset(self::$redis_rw_handler[$master])) { if (isset(self::$redis_rw_handler[$master])) {
$this->handler = self::$redis_rw_handler[$master]; $this->handler = self::$redis_rw_handler[$master];
@@ -131,17 +133,23 @@ class Redisd
$host = isset($parse['host']) ? $parse['host'] : $host; $host = isset($parse['host']) ? $parse['host'] : $host;
$port = isset($parse['host']) ? $parse['port'] : $this->options['port']; $port = isset($parse['host']) ? $parse['port'] : $this->options['port'];
$this->handler->$func($host, $port, $this->options['timeout']); //发生错误则摘掉当前节点
try {
$result = $this->handler->$func($host, $port, $this->options['timeout']);
if($result === false) {
$this->handler->getLastError();
}
if (null != $this->options['password']) { if (null != $this->options['password']) {
$this->handler->auth($this->options['password']); $this->handler->auth($this->options['password']);
} }
APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), 'info'); $this->handler->setOption(\Redis::OPT_SERIALIZER, $this->options['serialize']);
if(strlen($this->options['prefix'])) {
$this->handler->setOption(\Redis::OPT_PREFIX, $this->options['prefix']);
}
//发生错误则摘掉当前节点 APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), Log::ALERT);
try {
$error = $this->handler->getLastError();
} catch (\RedisException $e) { } catch (\RedisException $e) {
//phpredis throws a RedisException object if it can't reach the Redis server. //phpredis throws a RedisException object if it can't reach the Redis server.
//That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded. //That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded.
@@ -203,9 +211,8 @@ class Redisd
{ {
$this->master($master); $this->master($master);
$value = null;
try { try {
$value = $this->handler->get($this->options['prefix'] . $name); $value = $this->handler->get($name);
} catch (\RedisException $e) { } catch (\RedisException $e) {
unset(self::$redis_rw_handler[0]); unset(self::$redis_rw_handler[0]);
@@ -215,13 +222,7 @@ class Redisd
Log::record($e->getMessage(), Log::ERROR); Log::record($e->getMessage(), Log::ERROR);
} }
$jsonData = null; return isset($value) ? $value : null;
//如果是对象则进行反转
if (!empty($value) && false !== strpos("[{", $value[0])) {
$jsonData = $value ? json_decode($value, true) : $value;
}
return (null === $jsonData) ? $value : $jsonData;
} }
/** /**
@@ -240,21 +241,12 @@ class Redisd
if (is_null($expire)) { if (is_null($expire)) {
$expire = $this->options['expire']; $expire = $this->options['expire'];
} }
$name = $this->options['prefix'] . $name;
/**
* 兼容历史版本
* Redis不支持存储对象存入对象会转换成字符串
* 但在这里对所有数据做json_decode会有性能开销
*/
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
try {
if (null === $value) { if (null === $value) {
return $this->handler->delete($this->options['prefix'] . $name); return $this->handler->delete($name);
} }
// $expire < 0 则等于ttl操作列为todo吧
try {
if (is_int($expire) && $expire) { if (is_int($expire) && $expire) {
$result = $this->handler->setex($name, $expire, $value); $result = $this->handler->setex($name, $expire, $value);
} else { } else {
@@ -282,7 +274,7 @@ class Redisd
public function rm($name) public function rm($name)
{ {
$this->master(true); $this->master(true);
return $this->handler->delete($this->options['prefix'] . $name); return $this->handler->delete($name);
} }
/** /**
@@ -303,7 +295,7 @@ class Redisd
* *
* @access public * @access public
* @param bool $master 指定主从节点,可以从主节点获取结果 * @param bool $master 指定主从节点,可以从主节点获取结果
* @return object * @return \Redis
*/ */
public function handler($master = true) public function handler($master = true)
{ {