连接池
open-smf/connection-pool 是一个基于Swoole的通用连接池,常被用作数据库连接池。
依赖
依赖
版本
>=7.0.0
>=4.2.9 Recommend 4.2.13+
安装
composer require "open-smf/connection-pool:~1.0"
使用
更多示例。
可用的连接器
连接器
说明
CoroutineMySQLConnector
Swoole\Coroutine\MySQL的实例
CoroutinePostgreSQLConnector
Swoole\Coroutine\PostgreSQL的实例,编译Swoole时需要添加参数--enable-coroutine-postgresql
CoroutineRedisConnector
Swoole\Coroutine\Redis的实例
PhpRedisConnector
Redis的实例,需要安装redis
PDOConnector
PDO的实例,需要安装PDO
YourConnector
YourConnector必须实现接口ConnectorInterface,任何对象均可作为连接实例
基本用法
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector;
use Swoole\Coroutine\MySQL;
go(function () {
// MySQL连接数区间:[10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new CoroutineMySQLConnector, // 指明连接器实例,这里使用协程MySQL连接器,这样就可以创建一个协程MySQL的数据库连接池
[
'host' => '127.0.0.1&