美文网首页
Easyswoole Mysqli连接池的使用

Easyswoole Mysqli连接池的使用

作者: 骑蚂蚁上高速_jun | 来源:发表于2020-05-02 23:19 被阅读0次
    1. 安装
    安装通用连接池
    composer require easyswoole/pool --ignore-platform-reqs
    安装mysqli 组件
    composer require easyswoole/mysqli --ignore-platform-reqs
    

    2 . 修改配置文件

    在 dev.php | produce.php 中
    'mysql' => [
            'host' => '127.0.0.1',
            'port' => '3306',
            'user' => 'root',
            'password' => '123456',
            'database' => 'dbName',
            'timeout' => 15,
            'charset' => 'utf8',
        ],
    
    1. 创建 mysqli 连接池的 注册文件 和 对象文件
    1. 创建 MysqliPool.php 用于注册
    <?php
    
    namespace App\Pool;
    
    use EasySwoole\Pool\AbstractPool;
    use EasySwoole\Pool\Config;
    use EasySwoole\Pool\Exception\Exception;
    
    class MysqliPool extends AbstractPool
    {
        public function __construct(Config $conf)
        {
            try {
                parent::__construct($conf);
            } catch (Exception $e) {
            }
        }
        // 创建对象
        protected function createObject()
        {
            // TODO: Implement createObject() method.
            return new MysqliObject();
        }
    }
    2 . 创建 mysqli 获取对象文件
    <?php
    
    namespace App\Pool;
    
    use EasySwoole\EasySwoole\Config;
    use EasySwoole\Mysqli\Client;
    use EasySwoole\Pool\ObjectInterface;
    use EasySwoole\Pool\Manager;
    
    
    class MysqliObject extends Client implements ObjectInterface
    {
    
        const TYPE = "mysql";
    
        public function __construct()
        {
            $mysqlConfig = Config::getInstance()->getConf("mysql"); // 获取 dev.php | produce.php 中的配置
            parent::__construct(new \EasySwoole\Mysqli\Config($mysqlConfig));
        }
    
        // 被连接池 unset 的时候执行
        public function gc()
        {
            // TODO: Implement gc() method.
            $this->close();
        }
    
        // 被连接池 回收的时候执行
        public function objectRestore()
        {
            // TODO: Implement objectRestore() method.
        }
    
        // 取出连接池的时候被调用,若返回false,则当前对象被弃用回收
        public function beforeUse(): ?bool
        {
            // TODO: Implement beforeUse() method.
            return true;
        }
    
        /**
         * 个人创建的方法 用于 获取连接池的连接
         */
        public static function borrowPool():?MysqliObject{
            try {
                return Manager::getInstance()->get(self::TYPE)->getObj(500,6);
            } catch (\Throwable $e) {
                \EasySwoole\EasySwoole\Trigger::getInstance()->error("获取连接失败" );
            }
        }
    
        /**
         * @title 个人创建方法 用于 归还连接
         */
        public static function returnPool(?MysqliObject $client){
            try {
                Manager::getInstance()->get(self::TYPE)->recycleObj($client);
            } catch (\Throwable $e) {
                //
                \EasySwoole\EasySwoole\Trigger::getInstance()->error("归还连接失败" );
            }
        }
    }
    
    1. 在启动事件类 EasySwooleEvent.php 中的mainServerCreate() 方法中注册
    // 注册 mysqli 连接池
    $mysqliPoolConfig = new \EasySwoole\Pool\Config([
        "maxIdleTime"=>10, // 获取连接最大等待时间
        "minObjectNum"=>20, // 最小连接数
        "maxObjectNum"=>50, // 最大连接数
        "getObjectTimeout"=>65, // 获取连接超时时间
    ]);
    \EasySwoole\Pool\Manager::getInstance()->register(
        new \App\Pool\MysqliPool($mysqliPoolConfig),
        \App\Pool\MysqliObject::TYPE
    );
    

    4 . 在业务中获取使用

    try {
        $client = \App\Pool\MysqliObject::borrowPool();
        $client->queryBuilder()->where("id",1)->getOne('tableName');
        $data = $client->execBuilder();
        var_dump($data);
        \App\Pool\MysqliObject::returnPool($client); # 回收对象 PS 用完一定要回收
    } catch (\Throwable $e) {
        var_dump($e->getMessage());
        \EasySwoole\EasySwoole\Trigger::getInstance()->error($e->getMessage() );
    }
    

    相关文章

      网友评论

          本文标题:Easyswoole Mysqli连接池的使用

          本文链接:https://www.haomeiwen.com/subject/onfrghtx.html