Skip to content

Commit

Permalink
Merge pull request #9 from tioncico/2.x
Browse files Browse the repository at this point in the history
2.x
evalor authored Oct 16, 2019
2 parents c9800c6 + 4fd3b8f commit 13f2656
Showing 5 changed files with 71 additions and 201 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@
},
"require": {
"easyswoole/component": "^1.5",
"easyswoole/spl": "^1.1"
"easyswoole/spl": "^1.1",
"easyswoole/redis": "^1.0",
"easyswoole/pool": "dev-master"
}
}
99 changes: 0 additions & 99 deletions src/Config.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Connection.php

This file was deleted.

104 changes: 29 additions & 75 deletions src/Redis.php
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;
}
}
}

}
39 changes: 39 additions & 0 deletions src/RedisPool.php
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;
}
}

0 comments on commit 13f2656

Please sign in to comment.