午夜免费_欧美一级免费看_欧美日韩一区二区视频在线观看_午夜视频在线_日本特黄a级高清免费大片_成年人黄色小视频

简单 Redis 连接池示例

安装 easyswoole/redis 组件:

composer require easyswoole/redis

定义 RedisPool 管理器:

基于 AbstractPool 实现:

新增文件 \App\Pool\RedisPool.php,内容如下:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.icaituan.cn
 * @document http://www.icaituan.cn
 * @contact http://www.icaituan.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Pool;

use EasySwoole\Pool\AbstractPool;
use EasySwoole\Pool\Config;
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;
    }
}

或者基于 MagicPool 实现:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.icaituan.cn
 * @document http://www.icaituan.cn
 * @contact http://www.icaituan.cn/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 RedisPool1 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);
    }
}

不管是基于 AbstractPool 实现还是基于 MagicPool 实现效果是一致的。

注册连接池管理对象

EasySwooleEvent.php 中的 initialize/mainServerCreate 事件中注册,然后可以在控制器中获取连接池然后进行获取连接:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.icaituan.cn
 * @document http://www.icaituan.cn
 * @contact http://www.icaituan.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        $config = new \EasySwoole\Pool\Config();

        $redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(Config::getInstance()->getConf('REDIS1'));
        $redisConfig2 = new \EasySwoole\Redis\Config\RedisConfig(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');
    }

    public static function mainServerCreate(EventRegister $register)
    {

    }
}

调用

在控制器中获取连接池中连接对象,进行调用:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.icaituan.cn
 * @document http://www.icaituan.cn
 * @contact http://www.icaituan.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        // 取出连接池管理对象,然后获取连接对象(getObject)
        $redis1 = \EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
        $redis2 = \EasySwoole\Pool\Manager::getInstance()->get('redis2')->getObj();

        $redis1->set('name', '仙士可');
        var_dump($redis1->get('name'));

        $redis2->set('name', '仙士可2号');
        var_dump($redis2->get('name'));

        // 回收连接对象(将连接对象重新归还到连接池,方便后续使用)
        \EasySwoole\Pool\Manager::getInstance()->get('redis1')->recycleObj($redis1);
        \EasySwoole\Pool\Manager::getInstance()->get('redis2')->recycleObj($redis2);

        // 释放连接对象(将连接对象直接彻底释放,后续不再使用)
        // \EasySwoole\Pool\Manager::getInstance()->get('redis1')->unsetObj($redis1);
        // \EasySwoole\Pool\Manager::getInstance()->get('redis2')->unsetObj($redis2);
    }
}
主站蜘蛛池模板: 91视频在线免费看 | 日韩欧美精品一区二区三区 | 成年免费在线 | 午夜成人免费视频 | 全部免费毛片在线播放 | 国产成人啪午夜精品网站男同 | 91亚洲精品在线 | 亚洲视频www | 夜夜草影院 | 亚洲毛片在线视频 | 免费成人午夜视频 | 亚洲综合四区 | 国产成人啪午夜精品网站男同 | 91| 亚洲精品美女在线观看 | 久久男人天堂 | 9191成人精品久久 | www.久久av.com | 懂色av色香蕉一区二区蜜桃 | 亚洲精品视频网站在线观看 | 国产成人免费视频 | 91国内精品久久久久 | 天堂一区| 久久久成人999亚洲区美女 | 免费人成在线观看 | 91视频污版 | 久久久精品91 | 免费真人毛片在线播放 | 亚洲免费国产 | 91手机在线播放 | 国产区h| 91sex国产 | 国产精品精品久久久久久 | 91夜夜蜜桃臀一区二区三区 | 一区精品国产欧美在线 | 99久久夜色精品国产亚洲96 | 久草美女 | 亚洲精品在线视频观看 | 偷拍福利视频一区二区三区 | 91视频高清免费 | 一区二区三区不卡免费视频97 |