Skip to content

Commit

Permalink
优化 Redis 连接池资源类
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Feb 27, 2020
1 parent 976761c commit 2d09da2
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/Redis/RedisResource.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Imi\Redis;

use Imi\App;
use Imi\Pool\BasePoolResource;

class RedisResource extends BasePoolResource
Expand Down Expand Up @@ -30,24 +31,28 @@ public function __construct(\Imi\Pool\Interfaces\IPool $pool, RedisHandler $redi
*/
public function open()
{
$result = $this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379, $this->config['timeout'] ?? null);
if('' !== ($this->config['password'] ?? ''))
$this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379, $this->config['timeout'] ?? null);
if(('' !== ($this->config['password'] ?? '')) && !$this->redis->auth($this->config['password']))
{
$result = $result && $this->redis->auth($this->config['password']);
throw new \RedisException('Redis auth failed');
}
if(isset($this->config['db']))
if(isset($this->config['db']) && !$this->redis->select($this->config['db']))
{
$result = $result && $this->redis->select($this->config['db']);
throw new \RedisException('Redis select db failed');
}
if($this->config['serialize'] ?? true)
$options = $this->config['options'] ?? [];
if(($this->config['serialize'] ?? true) && !isset($options[\Redis::OPT_SERIALIZER]))
{
$result = $result && $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
$options[\Redis::OPT_SERIALIZER] = \Redis::SERIALIZER_PHP;
}
foreach($this->config['options'] ?? [] as $key => $value)
foreach($options as $key => $value)
{
$this->redis->setOption($key, $value);
if(!$this->redis->setOption($key, $value))
{
throw new \RuntimeException(sprintf('Redis setOption %s=%s failed', $key, $value));
}
}
return $result;
return true;
}

/**
Expand All @@ -74,8 +79,15 @@ public function getInstance()
*/
public function reset()
{
$this->redis->select($this->config['db'] ?? 0);
$this->redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
if(!$this->redis->select($this->config['db'] ?? 0))
{
throw new \RedisException('Redis select db failed');
}
$optScan = $this->config['options'][\Redis::OPT_SCAN] ?? \Redis::SCAN_RETRY;
if(!$this->redis->setOption(\Redis::OPT_SCAN, $optScan))
{
throw new \RuntimeException(sprintf('Redis setOption %s=%s failed', \Redis::OPT_SCAN, $optScan));
}
}

/**
Expand All @@ -88,8 +100,10 @@ public function checkState(): bool
$result = $this->redis->ping();
// PHPRedis 扩展,5.0.0 版本开始,ping() 返回为 true,旧版本为 +PONG
return true === $result || '+PONG' === $result;
}catch(\Throwable $ex)
{
} catch(\Throwable $ex) {
/** @var \Imi\Log\ErrorLog $errorLog */
$errorLog = App::getBean('ErrorLog');
$errorLog->onException($ex);
return false;
}
}
Expand Down

0 comments on commit 2d09da2

Please sign in to comment.