nstwf / mysql-connection-pool
Reactphp MySQL 连接池
Requires
- nstwf/mysql-connection: ^1.2
Requires (Dev)
- phpunit/phpunit: ^9.5
- react/async: ^4.0
This package is auto-updated.
Last update: 2024-09-30 15:04:06 UTC
README
目录
快速入门示例
$pool = new \Nstwf\MysqlConnectionPool\Pool('localhost:3306'); $pool ->getConnection() ->then(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) use ($pool) { $connection->query("UPDATE users SET blocked = 1 WHERE id = 3"); $pool->releaseConnection($connection); });
用法
PoolInterface
PoolInterface 的主要作用 - 使用选定选项管理连接
选项
waitForConnections: bool,在不存在空闲连接且用户调用getConnection方法时的行为设置。如果设置为false- 抛出异常,否则返回一个带有空闲连接的承诺。(默认:true)connectionLimit: int,同时连接的最大数量。0- 表示无限。(默认:5)
$pool = new \Nstwf\MysqlConnectionPool\Pool('localhost:3306', null, 10, false);
getConnection
可以使用 getConnection(): PromiseInterface<ConnectionInterface> 方法在无可用空闲连接的情况下创建一个新的 ConnectionInterface 实例,否则选择一个空闲实例。
$pool ->getConnection() ->then(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) { $connection->query("UPDATE users SET active = 0 WHERE id = 2"); $connection->query("UPDATE users SET blocked = 1 WHERE id = 3"); $pool->releaseConnection($connection); });
releaseConnection
可以使用 releaseConnection(ConnectionInterface $connection): void 方法释放连接到池中。
$pool->releaseConnection($connection);
query
使用 query(string $sql, array $params = []): PromiseInterface<QueryResult> 方法是调用 getConnection() -> query() -> releaseConnection() 的快捷方式。
$pool->query("UPDATE users SET active = 0 WHERE id = ?", [2]);
transaction
使用 transaction(callable $callable): PromiseInterface 方法是调用:getConnection() -> transaction() -> releaseConnection() 的快捷方式。
$pool->transaction(function(\Nstwf\MysqlConnection\ConnectionInterface $connection) { $connection->query("UPDATE users SET active = 0 WHERE id = 2"); });
安装
建议通过 Composer 安装此库。 初识 Composer?
本项目遵循 SemVer。这将安装最新支持的版本。
composer require nstwf/mysql-connection
有关版本升级的详细信息,请参阅 CHANGELOG。
强烈建议使用 PHP 8+ * 为此项目。
测试
要运行测试套件,您首先需要克隆此存储库,然后通过 Composer 安装所有依赖项。
composer install
要运行测试套件,请转到项目根目录并运行
vendor/bin/phpunit
许可证
MIT,请参阅 LICENSE 文件。
- friends-of-reactphp/mysql - 主项目
- mysqljs/mysql - 主概念