使用Mysqli 的链接池
EasySwoole Mysqli 组件文档地址 https://www.easyswoole.com/Components/Mysqli/install.html 先看文档
EasySwoole 新增ORM 自带连接池 和 读写分离 不过 Swoole 的版本需要大于等于 4.4.8 笔者的版本不足与满足要求 所以自行实现 通用对象池的方式实现连接池 ORM的文档地址https://www.easyswoole.com/Components/Orm/install.html
1. 首先安装 mysqli 和 通用连接池组件
# 通用连接池安装 https://www.easyswoole.com/Components/Mysqli/install.html
composer require easyswoole/pool
# mysqli 组件的安装 https://www.easyswoole.com/Components/Pool/introduction.html
composer require easyswoole/mysqli
2. 配置mysql信息 写在dev.php 中新增mysql配置
/******** Mysql 配置 *********/
'mysql' => [
'host' => '127.0.0.1',
'port' => '3306',
'user' => '****',
'password' => '****',
'database' => 'xiaoshuo',
'timeout' => 5,
'charset' => 'utf8'
]
3. 创建连接池所需要的链接对象 在 App\System 文件夹下创建 MysqliObject.php 文件 MysqliObject 类继承自 \EasySwoole\Mysqli\Client 获得所有的方法 并实现 \EasySwoole\Pool\ObjectInterface 接口的函数 代码如下
<?php
/**
* 用户Mysqli 链接池的对象
* Created by PhpStorm.
* User: taojiaheng
* Date: 2019/11/19
* Time: 18:41
*/
namespace App\System;
use \EasySwoole\EasySwoole\Config;
use EasySwoole\Pool\Manager;
class MysqliObject extends \EasySwoole\Mysqli\Client implements \EasySwoole\Pool\ObjectInterface
{
public function __construct()
{
$mysqlConfig = Config::getInstance()->getConf('mysql');
parent::__construct(new \EasySwoole\Mysqli\Config($mysqlConfig));
}
// 被连接池 回收的时候执行
public function objectRestore()
{
}
// 取出连接池的时候被调用,若返回false,则当前对象被弃用回收
public function beforeUse(): ?bool
{
return true;
}
// 被连接池 unset 的时候执行
public function gc()
{
$this->close();
}
}
4. 实现连接池的对象 在 App\System 文件夹下创建 MysqliPool.php 并实现 \EasySwoole\Pool\AbstractPool 接口 代码如下
<?php
/**
* Mysqli 连接池
* Created by PhpStorm.
* User: taojiaheng
* Date: 2019/11/19
* Time: 18:43
*/
namespace App\System;
use EasySwoole\Pool\Config;
class MysqliPool extends \EasySwoole\Pool\AbstractPool
{
public function __construct(Config $conf)
{
parent::__construct($conf);
}
/**
* 对象池创建对象时调用
* @return MysqliObject
*/
protected function createObject()
{
return new MysqliObject();
}
}
5. 在主服务创建事件中注册对象连接池 EasySwooleEvent.php 文件 mainServerCreate 函数中添加一下代码
// 注册mysqli 连接池
$mysqliPoolConfig = new \EasySwoole\Pool\Config();
\EasySwoole\Pool\Manager::getInstance()->register(new \App\System\MysqliPool($mysqliPoolConfig), 'mysql');
这里 Mysqli的连接池就实现完毕了 实际使用一下 线重启服务 是对象连接池生效
所需要用到的数据库表
继续在 App/HttpController/Admin/Index.php 文件中实现 (懵逼的朋友看上一节的博客)
# 从连接池 中获得对象
$client = \EasySwoole\Pool\Manager::getInstance()->get('mysql')->getObj();
# 查询 xs_channel 表中所有的数据
$client->queryBuilder()->get('xs_channel');
$channelData = $client->execBuilder();
$this->assign('channel', $channelData);
# 回收对象 PS 用完一定要回收
\EasySwoole\Pool\Manager::getInstance()->get('mysql')->recycleObj($client);
# 渲染模板
$this->response()->write($this->fetch('Admin/Index/index'));
渲染的模板内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tao</title>
</head>
<body>
<p>channel data list</p>
<ul>
{foreach $channel as $key => $val}
<li>channel_id = {$val.channel_id} channel_name = {$val.channel_name}</li>
{/foreach}
</ul>
</body>
</html>
运行结果如下