中文在线一区二区_欧美在线综合_久久久久久综合_欧美一区二区三区视频_国产免费看_国产福利精品一区

簡單 Redis 連接池示例

安裝 easyswoole/redis 組件:

composer require easyswoole/redis

定義 RedisPool 管理器:

基于 AbstractPool 實現:

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.jrrswxmm.cn
 * @document http://www.jrrswxmm.cn
 * @contact http://www.jrrswxmm.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.jrrswxmm.cn
 * @document http://www.jrrswxmm.cn
 * @contact http://www.jrrswxmm.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.jrrswxmm.cn
 * @document http://www.jrrswxmm.cn
 * @contact http://www.jrrswxmm.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.jrrswxmm.cn
 * @document http://www.jrrswxmm.cn
 * @contact http://www.jrrswxmm.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);
    }
}
主站蜘蛛池模板: 丁香伊人 | 精品国产99| 久久久国产一级 | 成人超碰在线 | 久久久久99啪啪免费 | 国产精品久久久久国产a级 国产免费久久 | 国产精品一区二区无线 | 日韩一区二区三区在线视频 | 91麻豆蜜桃一区二区三区 | 亚洲人视频 | 精品国产乱码久久久久久影片 | 午夜久久久 | 曰韩免费视频 | 伊人中文 | 欧美黄色成人 | 免费黄色在线观看视频 | 日韩免费视频 | 精品久久久久久久久久久久久久 | 激情成人综合 | 欧美九九九 | 免费大片黄 | 91国内外精品自在线播放 | 国产一区二区三区视频 | 日本成人高清视频 | 日韩不卡一区二区三区 | 国产成人精品一区二区 | 日韩视频一区 | 国产免费一区二区三区 | 中文字幕1区2区3区 亚洲欧美日韩精品久久亚洲区 | 久久精品一 | 欧美一区二区三区免费观看视频 | 成人在线播放网站 | 日本中文字幕一区二区 | 污视频在线观看免费 | 国产一区二区三区免费 | 免费污视频在线 | 久久精品不卡 | 国产精品99久久 | 亚洲综合视频在线观看 | 精精国产xxxx视频在线播放 | 久久男人 |