1. Go to this page and download the library: Download easyswoole/pool library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easyswoole / pool example snippets
class Std implements \EasySwoole\Pool\ObjectInterface
{
function gc()
{
/*
* 本对象被pool执行unset的时候
*/
}
function objectRestore()
{
/*
* 回归到连接池的时候
*/
}
function beforeUse(): ?bool
{
/*
* 取出连接池的时候,若返回false,则当前对象被弃用回收
*/
return true;
}
public function who()
{
return spl_object_id($this);
}
}
class StdPool extends \EasySwoole\Pool\AbstractPool
{
protected function createObject()
{
return new Std();
}
}
$config = new \EasySwoole\Pool\Config();
$pool = new StdPool($config);
go(function () use ($pool) {
$obj = $pool->getObj();
$obj2 = $pool->getObj();
var_dump($obj->who());
var_dump($obj2->who());
});
/**
* This file is part of EasySwoole.
*
* @link https://www.easyswoole.com
* @document https://www.easyswoole.com
* @contact https://www.easyswoole.com/Preface/contact.html
* @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
*/
namespace App\Pool;
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;
}
}
/**
* This file is part of EasySwoole.
*
* @link https://www.easyswoole.com
* @document https://www.easyswoole.com
* @contact https://www.easyswoole.com/Preface/contact.html
* @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
*/
namespace App\Pool;
use EasySwoole\Pool\Config;
use EasySwoole\Pool\MagicPool;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;
class RedisPool extends MagicPool
{
/**
* 重写构造函数,为了传入 redis 配置
* RedisPool constructor.
* @param Config $config 连接池配置
* @param RedisConfig $redisConfig
* @throws \EasySwoole\Pool\Exception\Exception
*/
public function __construct(Config $config,RedisConfig $redisConfig)
{
parent::__construct(function () use ($redisConfig) {
$redis = new Redis($redisConfig);
return $redis;
}, $config);
}
}
$config = new \EasySwoole\Pool\Config();
$redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS1'));
$redisConfig2 = new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS2'));
// 注册连接池管理对象
\EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config, $redisConfig1), 'redis1');
\EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config, $redisConfig2), 'redis2');
go(function () {
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
// 获取一个连接池的对象
$redis = $redisPool->getObj();
var_dump($redis->echo('仙士可'));
$redisPool->recycleObj($redis);
});
go(function () {
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
$redisPool->invoke(function (\EasySwoole\Redis\Redis $redis) {
var_dump($redis->echo('仙士可'));
});
});
go(function () {
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
$redis = $redisPool->defer();
var_dump($redis->echo('仙士可'));
});
public static function mainServerCreate(EventRegister $register)
{
$register->add($register::onWorkerStart,function (\swoole_server $server,int $workerId){
if ($server->taskworker == false) {
//每个worker进程都预创建连接
\EasySwoole\Pool\Manager::getInstance()->get('redis')->keepMin(10);
var_dump(\EasySwoole\Pool\Manager::getInstance()->get('redis')->status());
}
});
}
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
var_dump($redisPool->getConfig());
go(function () {
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
var_dump($redisPool->getObj());
$redisPool->destroy();
var_dump($redisPool->getObj());
});
go(function () {
$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
var_dump($redisPool->getObj());
$redisPool->reset();
var_dump($redisPool->getObj());
});
public function intervalCheck()
{
$this->idleCheck($this->getConfig()->getMaxIdleTime());
$this->keepMin($this->getConfig()->getMinObjectNum());
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.