亚洲精品成人_精品成人一区_999视频在线播放_免费黄色在线_亚洲成人久久久_久久www免费视频

簡單 Redis 連接池示例

安裝 easyswoole/redis 組件:

composer require easyswoole/redis

定義 RedisPool 管理器:

基于 AbstractPool 實(shí)現(xiàn):

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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 配置進(jìn)行 new 一個(gè) redis 連接
        $redis = new Redis($this->redisConfig);
        return $redis;
    }
}

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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 實(shí)現(xiàn)還是基于 MagicPool 實(shí)現(xiàn)效果是一致的。

注冊(cè)連接池管理對(duì)象

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

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

        // 注冊(cè)連接池管理對(duì)象
        \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)用

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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()
    {
        // 取出連接池管理對(duì)象,然后獲取連接對(duì)象(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號(hào)');
        var_dump($redis2->get('name'));

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

        // 釋放連接對(duì)象(將連接對(duì)象直接徹底釋放,后續(xù)不再使用)
        // \EasySwoole\Pool\Manager::getInstance()->get('redis1')->unsetObj($redis1);
        // \EasySwoole\Pool\Manager::getInstance()->get('redis2')->unsetObj($redis2);
    }
}
主站蜘蛛池模板: 在线不卡中文字幕 | 日日摸日日干 | 精品亚洲永久免费精品鬼片影片 | 久久在线免费观看视频 | 全国免费av| 日本精品在线观看 | 免费观看v片m3837cc | 成人免费播放视频 | 亚洲国产综合在线播放av66 | 国产精品久久久久久久久免费 | 亚洲狠狠干 | 狠狠躁天天躁夜夜躁婷婷 | 天天躁夜夜躁天干天干2022 | 777久久久免费精品国产 | 亚洲国产视频网站 | 三级精品在线观看 | 欧美精品久久久久久久 | 亚洲网在线 | 国精品一区二区三区 | 在线免费黄色 | 国产福利免费 | 欧美日韩亚洲国产精品 | 精品国产欧美一区二区三区成人 | www.77伦理bt.com| 亚洲综合一区二区三区 | 伊人久久中文 | 国产一级片免费播放 | 亚洲经典一区 | 成人性色生活片免费看l | 日韩美女一级毛片 | 国产三级精品在线观看 | 毛片一 | 一区二区三区欧美在线 | 91精品无人成人www | 亚洲一区二区免费电影 | 国产精品久久久久久久久免费高清 | h片在线看 | 免费三级毛片 | 狠狠做六月爱婷婷综合aⅴ 狠日日 | 亚洲aaaaa特级 | 佐山爱巨大黑人司机在线观看 |