Skip to content

Commit

Permalink
Class改名
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Dec 29, 2020
1 parent 9c9e814 commit 223f3ba
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 115 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ composer require easyswoole/redis-pool

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

//redis集群连接池注册
\EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
\EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
Expand All @@ -27,13 +27,14 @@ composer require easyswoole/redis-pool

## 连接池配置
当注册好时,将返回连接池的poolConf用于配置连接池:

```php
$redisPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig());
$redisPoolConfig = \EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig());
//配置连接池连接数
$redisPoolConfig->setMinObjectNum(5);
$redisPoolConfig->setMaxObjectNum(20);

$redisClusterPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
$redisClusterPoolConfig = \EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
Expand All @@ -49,22 +50,22 @@ $redisPoolConfig->setMaxObjectNum(20);

```php
//defer方式获取连接
$redis = \EasySwoole\RedisPool\Redis::defer();
$redisCluster = \EasySwoole\RedisPool\Redis::defer();
$redis = \EasySwoole\RedisPool\RedisPool::defer();
$redisCluster = \EasySwoole\RedisPool\RedisPool::defer();
$redis->set('a', 1);
$redisCluster->set('a', 1);

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

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

$redis = $redisPool->getObj();
$redisPool->recycleObj($redis);
Expand Down
60 changes: 60 additions & 0 deletions src/Pool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Created by PhpStorm.
* User: Tioncico
* Date: 2019/10/15 0015
* Time: 14:46
*/

namespace EasySwoole\RedisPool;

use EasySwoole\Pool\MagicPool;
use EasySwoole\Redis\Config\RedisClusterConfig;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;
use EasySwoole\Redis\RedisCluster;

class Pool extends MagicPool
{
function __construct(RedisConfig $redisConfig,?string $cask = null)
{
parent::__construct(function ()use($redisConfig,$cask){
if($cask){
return new $cask($redisConfig);
}
if ($redisConfig instanceof RedisClusterConfig){
$redis = new RedisCluster($redisConfig);
}else{
$redis = new Redis($redisConfig);
$redis->connect($redisConfig->getTimeout());
}
return $redis;
},new PoolConfig());
}


/**
* @param RedisPool $redis
* @return bool
*/
public function itemIntervalCheck($redis): bool
{
/*
* 如果最后一次使用时间超过autoPing间隔
*/
if($this->getConfig()->getAutoPing() > 0 && (time() - $redis->__lastUseTime > $this->getConfig()->getAutoPing())){
try{
//执行一个ping
$redis->ping();
//标记使用时间,避免被再次gc
$redis->__lastUseTime = time();
return true;
}catch (\Throwable $throwable){
//异常说明该链接出错了,return 进行回收
return false;
}
}else{
return true;
}
}
}
62 changes: 0 additions & 62 deletions src/Redis.php

This file was deleted.

88 changes: 45 additions & 43 deletions src/RedisPool.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
<?php
/**
* Created by PhpStorm.
* User: Tioncico
* Date: 2019/10/15 0015
* Time: 14:46
*/

namespace EasySwoole\RedisPool;
namespace EasySwoole\RedisPool;

use EasySwoole\Pool\MagicPool;
use EasySwoole\Redis\Config\RedisClusterConfig;
use EasySwoole\Component\Singleton;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;
use EasySwoole\Pool\Config as PoolConfig;
use EasySwoole\Redis\Redis as RedisClient;
use EasySwoole\Redis\RedisCluster;
use EasySwoole\RedisPool\Exception\Exception;

class RedisPool extends MagicPool
class RedisPool
{
function __construct(RedisConfig $redisConfig,?string $cask = null)
use Singleton;

protected $container = [];

function register(RedisConfig $config, string $name ='default', ?string $cask = null): PoolConfig
{
parent::__construct(function ()use($redisConfig,$cask){
if($cask){
return new $cask($redisConfig);
}
if ($redisConfig instanceof RedisClusterConfig){
$redis = new RedisCluster($redisConfig);
}else{
$redis = new Redis($redisConfig);
$redis->connect($redisConfig->getTimeout());
if(isset($this->container[$name])){
//已经注册,则抛出异常
throw new RedisPoolException("redis pool:{$name} is already been register");
}
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");
}
return $redis;
},new PoolConfig());
}
$pool = new Pool($config,$cask);
$this->container[$name] = $pool;
return $pool->getConfig();
}

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

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

/**
* @param Redis $redis
* @return bool
*/
public function itemIntervalCheck($redis): bool
static function invoke(callable $call,string $name ='default',float $timeout = null)
{
/*
* 如果最后一次使用时间超过autoPing间隔
*/
if($this->getConfig()->getAutoPing() > 0 && (time() - $redis->__lastUseTime > $this->getConfig()->getAutoPing())){
try{
//执行一个ping
$redis->ping();
//标记使用时间,避免被再次gc
$redis->__lastUseTime = time();
return true;
}catch (\Throwable $throwable){
//异常说明该链接出错了,return 进行回收
return false;
}
$pool = static::getInstance()->getPool($name);
if($pool){
return $pool->invoke($call,$timeout);
}else{
return true;
return null;
}
}
}

0 comments on commit 223f3ba

Please sign in to comment.