五月天激情丁香,国产精品2019,国产成人精品亚洲2020,国产精品免费视频一区二区三区,开心久久婷婷综合中文字幕,天堂视频在线观看免费完整版

簡單 Redis 連接池示例

安裝 easyswoole/redis 組件:

composer require easyswoole/redis

定義 RedisPool 管理器:

基于 AbstractPool 實現(xiàn):

新增文件 \App\Pool\RedisPool.php,內(nèi)容如下:

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

    /**
     * 重寫構(gòu)造函數(shù),為了傳入 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()
    {
        // 根據(jù)傳入的 redis 配置進行 new 一個 redis 連接
        $redis = new Redis($this->redisConfig);
        return $redis;
    }
}

或者基于 MagicPool 實現(xiàn):

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.fe88.cn
 * @document http://www.fe88.cn
 * @contact http://www.fe88.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
{
    /**
     * 重寫構(gòu)造函數(shù),為了傳入 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 實現(xiàn)還是基于 MagicPool 實現(xiàn)效果是一致的。

注冊連接池管理對象

EasySwooleEvent.php 中的 initialize/mainServerCreate 事件中注冊,然后可以在控制器中獲取連接池然后進行獲取連接:

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

    }
}

調(diào)用

在控制器中獲取連接池中連接對象,進行調(diào)用:

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

        // 回收連接對象(將連接對象重新歸還到連接池,方便后續(xù)使用)
        \EasySwoole\Pool\Manager::getInstance()->get('redis1')->recycleObj($redis1);
        \EasySwoole\Pool\Manager::getInstance()->get('redis2')->recycleObj($redis2);

        // 釋放連接對象(將連接對象直接徹底釋放,后續(xù)不再使用)
        // \EasySwoole\Pool\Manager::getInstance()->get('redis1')->unsetObj($redis1);
        // \EasySwoole\Pool\Manager::getInstance()->get('redis2')->unsetObj($redis2);
    }
}
主站蜘蛛池模板: 日韩男人的天堂 | 六月天综合网 | 国产高清成人mv在线观看 | 欧美日韩成人高清色视频 | 五月婷婷激情网 | 国产色婷婷精品综合在线观看 | 国产精品视频在这里有精品 | 亚洲狠狠综合久久 | 久久国产精品最新一区 | 99在线视频播放 | 国产99视频精品免视看9 | 精品免费福利视频 | 欧美在线观看一区二区三 | 午夜毛片电影 | 借种无删减版 | 精品亚洲欧美高清不卡高清 | 潦草影视在线 | 国产精品久久久久久久久久久不卡 | 免费一级性片 | 婷婷综合丁香 | 福利小视频在线播放 | 国产亚洲精品激情都市 | 久久精品久久久久 | 99久久精品免费看国产一区二区 | 免费色在线 | 国产片欧美片亚洲片久久综合 | 无法忍受电影完整版 | 国产s色 | 五月婷婷激情视频 | 免费一级毛片在线播放不收费 | 国产精品12p | 婷婷电影网 | 国产高清一级在线观看 | 久久成人国产精品免费 | 国产成人免费影片在线观看 | 亚洲国产天堂久久综合 | 久久青春 | 成人免费黄色小视频 | 试看视频免费体验区 | 婷婷开心色 | 精品天海翼一区二区 |