swoole mysql_基于 swoole 协程的 MySQL 连接池

本文介绍了一个基于Swoole协程的Mysql连接池类,通过通道(chan)作为连接池容器,初始化时建立指定数量的连接,并提供获取和释放连接的方法。在测试中,使用Swoole的HTTP服务器展示了如何在请求处理中获取并使用连接池中的连接执行SQL查询,最后回收连接。
摘要由CSDN通过智能技术生成

连接池类

// +----------------------------------------------------------------------

// | Created by PhpStorm

// +----------------------------------------------------------------------

// | Date: 19-1-4 上午9:42

// +----------------------------------------------------------------------

// | Author: woann <304550409@qq.com>

// +----------------------------------------------------------------------

class MysqlPool{

private $pool; //连接池容器

public static $instance = null; //实例

private $config = [

'max_num' => 100,

'mysql' =>[

'host' => '127.0.0.1',

'port' => 3306,

'user' => 'user',

'password' => 'password',

'database' => 'dbname',

]

];

//防止手欠在外部实例化,将构造函数私有化

private function __construct()

{

}

/**

* @author woann<304550409@qq.com>

* @des 初始化

*/

function init()

{

$this->pool = new chan($this->config["max_num"]);//用channel来作为连接池容器

for ($i = 0; $i < $this->config["max_num"]; $i++) {

$mysql = new Swoole\Coroutine\MySQL();

$res = $mysql->connect($this->config['mysql']);

if ($res == false) {

throw new RuntimeException("failed to connect mysql server");

}

$this->release($mysql);

}

}

/**

* @author woann<304550409@qq.com>

* @return MysqlPool|null

* @des 获取连接池实例

*/

static public function getInstance()

{

if (self::$instance == null) {

$mysqlpool = new MysqlPool();

$mysqlpool->init();

self::$instance = $mysqlpool;

}

return self::$instance;

}

/**

* @author woann<304550409@qq.com>

* @return mixed

* @des 获取链接

*/

function get()

{

return $this->pool->pop();

}

/**

* @author woann<304550409@qq.com>

* @param $mysql

* @des 回收链接

*/

function release($mysql)

{

$this->pool->push($mysql);

}

}

测试

用swoole的httpserver进行测试

// +----------------------------------------------------------------------

// | Created by PhpStorm

// +----------------------------------------------------------------------

// | Date: 19-1-4 上午9:58

// +----------------------------------------------------------------------

// | Author: woann <304550409@qq.com>

// +----------------------------------------------------------------------

require_once "MysqlPool.php";//引入连接池类

$server = new Swoole\Http\Server("127.0.0.1", 9501);//创建httpserver

$server->set([

'worker_num' => 1,

]);

$server->on('Request', function($request, $response) {

$pool = MysqlPool::getInstance();//获取连接池实例

$conn = $pool->get();//获取链接

$stmt = $conn->prepare('SELECT * FROM usertb WHERE id = ?'); //预处理

if ($stmt == false) {

var_dump($conn->errno, $conn->error);

}

$res = $stmt->execute(array(9988));//绑定参数 执行sql

$pool->release($conn);//释放回收链接

$response->end(json_encode($res));//将查询结果转成json格式输出到浏览器

});

$server->start();

测试结果

5c2ecb23d6b4c.png

查看数据库连接数

5c2ecb9b38101.png

本作品采用《CC 协议》,转载必须注明作者和本文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值