Skip to content

Commit

Permalink
参数调整
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Dec 29, 2020
1 parent 3d6d060 commit f22386c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
61 changes: 23 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ composer require easyswoole/redis-pool

```php
//redis连接池注册(config默认为127.0.0.1,端口6379)
\EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
\EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig(),'redis');

//redis集群连接池注册
\EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
\EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
['172.16.253.156', 9004],
]
));
),'redisCluster');
```

## 连接池配置
当注册好时,将返回连接池的poolConf用于配置连接池:
```php
$redisPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
$redisPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig());
//配置连接池连接数
$redisPoolConfig->setMinObjectNum(5);
$redisPoolConfig->setMaxObjectNum(20);

$redisClusterPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
$redisClusterPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
Expand All @@ -48,40 +48,25 @@ $redisPoolConfig->setMaxObjectNum(20);
## 使用连接池:

```php
//redis连接池注册(config默认为127.0.0.1,端口6379)
\EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());

//redis集群连接池注册
\EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
['172.16.253.156', 9004],
]
));
go(function () {
//defer方式获取连接
$redis = \EasySwoole\RedisPool\Redis::defer('redis');
$redisCluster = \EasySwoole\RedisPool\Redis::defer('redisCluster');
$redis->set('a', 1);
$redisCluster->set('a', 1);

//invoke方式获取连接
\EasySwoole\RedisPool\Redis::invoker('redis', function (\EasySwoole\Redis\Redis $redis) {
var_dump($redis->set('a', 1));
});
\EasySwoole\RedisPool\Redis::invoker('redisCluster', function (\EasySwoole\Redis\Redis $redis) {
var_dump($redis->set('a', 1));
});
//defer方式获取连接
$redis = \EasySwoole\RedisPool\Redis::defer();
$redisCluster = \EasySwoole\RedisPool\Redis::defer();
$redis->set('a', 1);
$redisCluster->set('a', 1);

//获取连接池对象
$redisPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redis');
$redisClusterPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redisCluster');
//invoke方式获取连接
\EasySwoole\RedisPool\Redis::invoker(function (\EasySwoole\Redis\Redis $redis) {
var_dump($redis->set('a', 1));
});
\EasySwoole\RedisPool\Redis::invoker(function (\EasySwoole\Redis\Redis $redis) {
var_dump($redis->set('a', 1));
});

$redis = $redisPool->getObj();
$redisPool->recycleObj($redis);
//获取连接池对象
$redisPool = \EasySwoole\RedisPool\Redis::getInstance()->getPool();
$redisClusterPool = \EasySwoole\RedisPool\Redis::getInstance()->getPool();

//清除pool中的定时器
\EasySwoole\Component\Timer::getInstance()->clearAll();
});
$redis = $redisPool->getObj();
$redisPool->recycleObj($redis);
```
!!!注意,在未指定连接池名称是,注册的连接池名称为默认的```default```
18 changes: 7 additions & 11 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
class Redis
{
use Singleton;

protected $container = [];

function register(string $name, RedisConfig $config,?string $cask = null): PoolConfig
function register(RedisConfig $config, string $name ='default', ?string $cask = null): PoolConfig
{
if(isset($this->container[$name])){
//已经注册,则抛出异常
Expand All @@ -31,32 +32,27 @@ function register(string $name, RedisConfig $config,?string $cask = null): PoolC
return $pool->getConfig();
}

function get(string $name): ?RedisPool
function getPool(string $name ='default'): ?RedisPool
{
if (isset($this->container[$name])) {
return $this->container[$name];
}
return null;
}

function pool(string $name): ?RedisPool
{
return $this->get($name);
}

static function defer(string $name,$timeout = null):?RedisClient
static function defer(string $name ='default',$timeout = null):?RedisClient
{
$pool = static::getInstance()->pool($name);
$pool = static::getInstance()->getPool($name);
if($pool){
return $pool->defer($timeout);
}else{
return null;
}
}

static function invoke(string $name,callable $call,float $timeout = null)
static function invoke(callable $call,string $name ='default',float $timeout = null)
{
$pool = static::getInstance()->pool($name);
$pool = static::getInstance()->getPool($name);
if($pool){
return $pool->invoke($call,$timeout);
}else{
Expand Down

0 comments on commit f22386c

Please sign in to comment.