-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tioncico/2.x
2.x
Showing
5 changed files
with
71 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,61 @@ | ||
<?php | ||
|
||
|
||
namespace EasySwoole\RedisPool; | ||
|
||
|
||
use EasySwoole\Component\Pool\AbstractPool; | ||
use EasySwoole\Component\Pool\PoolConf; | ||
use EasySwoole\Component\Pool\PoolManager; | ||
use EasySwoole\Component\Singleton; | ||
use EasySwoole\Utility\Random; | ||
use EasySwoole\Redis\Config\RedisConfig; | ||
use EasySwoole\Pool\Config as PoolConfig; | ||
|
||
class Redis | ||
{ | ||
use Singleton; | ||
protected $container = []; | ||
|
||
private $list = []; | ||
|
||
function register(string $name,Config $config):PoolConf | ||
function register(string $name, RedisConfig $config): PoolConfig | ||
{ | ||
if(isset($this->list[$name])){ | ||
if(isset($this->list[$name])){ | ||
//已经注册,则抛出异常 | ||
throw new RedisPoolException("redis pool:{$name} is already been register"); | ||
} | ||
if(isset($this->container[$name])){ | ||
//已经注册,则抛出异常 | ||
throw new RedisPoolException("redis pool:{$name} is already been register"); | ||
} | ||
/* | ||
* 绕过去实现动态class | ||
*/ | ||
$class = 'C'.Random::character(16); | ||
$classContent = '<?php | ||
namespace EasySwoole\RedisPool; | ||
use EasySwoole\Component\Pool\AbstractPool; | ||
class '.$class.' extends AbstractPool { | ||
protected function createObject() | ||
{ | ||
$config = $this->getConfig()->getExtraConf(); | ||
$conn = new Connection(); | ||
$ret = $conn->connect($config->getHost(),$config->getPort()); | ||
if(!$ret){ | ||
return; | ||
} | ||
if(!empty($config->getAuth())){ | ||
$ret = $conn->auth($config->getAuth()); | ||
} | ||
$conn->setOptions($config->getOptions()); | ||
//选择数据库,默认为0 | ||
if(!empty($config->getDb())){ | ||
$conn->select($config->getDb()); | ||
} | ||
return $conn; | ||
} | ||
}'; | ||
$file = sys_get_temp_dir()."/{$class}.php"; | ||
file_put_contents($file,$classContent); | ||
require_once $file; | ||
unlink($file); | ||
$class = "EasySwoole\\RedisPool\\{$class}"; | ||
$poolConfig = PoolManager::getInstance()->register($class); | ||
$poolConfig->setExtraConf($config); | ||
$this->list[$name] = [ | ||
'class'=>$class, | ||
'config'=>$config | ||
]; | ||
|
||
$poolConfig = new PoolConfig(); | ||
$pool = new RedisPool($poolConfig, $config); | ||
$this->container[$name] = $pool; | ||
|
||
return $poolConfig; | ||
} | ||
|
||
static function defer(string $name,$timeout = null):?Connection | ||
function get(string $name): ?RedisPool | ||
{ | ||
$pool = static::getInstance()->pool($name); | ||
if($pool){ | ||
return $pool::defer($timeout); | ||
}else{ | ||
return null; | ||
if (isset($this->container[$name])) { | ||
return $this->container[$name]; | ||
} | ||
return null; | ||
} | ||
|
||
static function invoker(string $name,callable $call,float $timeout = null) | ||
function pool(string $name): ?RedisPool | ||
{ | ||
return $this->get($name); | ||
} | ||
|
||
static function defer(string $name,$timeout = null):?\EasySwoole\Redis\Redis | ||
{ | ||
$pool = static::getInstance()->pool($name); | ||
if($pool){ | ||
return $pool::invoke($call,$timeout); | ||
return $pool->defer($timeout); | ||
}else{ | ||
return null; | ||
} | ||
} | ||
|
||
public function pool(string $name):?AbstractPool | ||
static function invoker(string $name,callable $call,float $timeout = null) | ||
{ | ||
if(isset($this->list[$name])){ | ||
$item = $this->list[$name]; | ||
if($item instanceof AbstractPool){ | ||
return $item; | ||
}else{ | ||
$class = $item['class']; | ||
$pool = PoolManager::getInstance()->getPool($class); | ||
$this->list[$name] = $pool; | ||
return $this->pool($name); | ||
} | ||
$pool = static::getInstance()->pool($name); | ||
if($pool){ | ||
return $pool->invoke($call,$timeout); | ||
}else{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Tioncico | ||
* Date: 2019/10/15 0015 | ||
* Time: 14:46 | ||
*/ | ||
|
||
namespace EasySwoole\RedisPool; | ||
|
||
use EasySwoole\Pool\Config; | ||
use EasySwoole\Pool\AbstractPool; | ||
use EasySwoole\Redis\Config\RedisConfig; | ||
use EasySwoole\Redis\Redis; | ||
|
||
class RedisPool extends AbstractPool | ||
{ | ||
protected $redisConfig; | ||
|
||
/** | ||
* 重写构造函数,为了传入redis配置 | ||
* RedisPool constructor. | ||
* @param Config $conf | ||
* @param RedisConfig $redisConfig | ||
* @throws \EasySwoole\Pool\Exception\Exception | ||
*/ | ||
public function __construct(Config $conf,RedisConfig $redisConfig) | ||
{ | ||
parent::__construct($conf); | ||
$this->redisConfig = $redisConfig; | ||
} | ||
|
||
protected function createObject() | ||
{ | ||
//根据传入的redis配置进行new 一个redis | ||
$redis = new Redis($this->redisConfig); | ||
return $redis; | ||
} | ||
} |