Skip to content

Commit

Permalink
支持cask迁移
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Mar 9, 2020
1 parent 5bc5d41 commit af3b80d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace EasySwoole\RedisPool\Exception;


class Exception extends \Exception
{

}
15 changes: 12 additions & 3 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
use EasySwoole\Component\Singleton;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Pool\Config as PoolConfig;
use EasySwoole\Redis\Redis as RedisClient;
use EasySwoole\Redis\RedisCluster;
use EasySwoole\RedisPool\Exception\Exception;

class Redis
{
use Singleton;
protected $container = [];

function register(string $name, RedisConfig $config): PoolConfig
function register(string $name, RedisConfig $config,?string $cask = null): PoolConfig
{
if(isset($this->container[$name])){
//已经注册,则抛出异常
throw new RedisPoolException("redis pool:{$name} is already been register");
}
$pool = new RedisPool($config);
if($cask){
$ref = new \ReflectionClass($cask);
if((!$ref->isSubclassOf(RedisClient::class)) || (!$ref->isSubclassOf(RedisCluster::class))){
throw new Exception("cask {$cask} not a sub class of EasySwoole\Redis\Redis or EasySwoole\Redis\RedisCluster");
}
}
$pool = new RedisPool($config,$cask);
$this->container[$name] = $pool;
return $pool->getConfig();
}
Expand All @@ -35,7 +44,7 @@ function pool(string $name): ?RedisPool
return $this->get($name);
}

static function defer(string $name,$timeout = null):?\EasySwoole\Redis\Redis
static function defer(string $name,$timeout = null):?RedisClient
{
$pool = static::getInstance()->pool($name);
if($pool){
Expand Down
7 changes: 5 additions & 2 deletions src/RedisPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

class RedisPool extends MagicPool
{
function __construct(RedisConfig $redisConfig)
function __construct(RedisConfig $redisConfig,?string $cask = null)
{
parent::__construct(function ()use($redisConfig){
parent::__construct(function ()use($redisConfig,$cask){
if($cask){
return new $cask($redisConfig);
}
if ($redisConfig instanceof RedisClusterConfig){
$redis = new RedisCluster($redisConfig);
}else{
Expand Down

0 comments on commit af3b80d

Please sign in to comment.