作者 karlet

feat:subredis

... ... @@ -7,6 +7,7 @@ use Swoole\Database\RedisPool;
class RedisCli
{
private $pool;
public $subRedis = null;
public function __construct($host,$port,$password,$dbIndex, $num = 20)
{
$this->pool = new RedisPool((new RedisConfig)
... ... @@ -117,6 +118,7 @@ class RedisCli
public function subscribe($channels,callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
$res = $redis->subscribe($channels,function($redis, $channel, $message)use($onMessage){
call_user_func($onMessage,$redis, $channel, $message);
... ... @@ -124,6 +126,7 @@ class RedisCli
} catch (\RedisException $e) {
return false;
}
$this->subRedis = null;
$this->pool->put($redis);
return $res;
}
... ... @@ -131,6 +134,7 @@ class RedisCli
public function psubscribe($channels,callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
$res = $redis->psubscribe($channels,function($redis, $pattern, $channel, $message)use($onMessage){
call_user_func($onMessage, $pattern, $redis, $channel, $message);
... ... @@ -138,6 +142,7 @@ class RedisCli
}catch (\RedisException $e) {
return false;
}
$this->subRedis = null;
$this->pool->put($redis);
return $res;
}
... ...