Pinga DB Pool: A Universal Database Connection Pool for Swoole
Pinga DB Pool is a PHP library that provides a universal database connection pool for the Swoole server framework. It is based on the excellent ipejasinovic/swoole-universal-pool package, but is tuned specifically for the Pinga framework. However, it can be used in virtually any Swoole-based application that requires efficient, asynchronous database connections. The library supports multiple database drivers, automatic reconnection, and configurable connection pooling parameters, making it an essential tool for high-concurrency applications that require reliable, low-latency database access.
How to use
# Example MySQL or PostgreSQL
<?php
require __DIR__ . '/vendor/autoload.php';
Swoole\Runtime::enableCoroutine();
$dbConfig = (new Pinga\DbPool\Config)
->withDriver('mysql')
->withHost('127.0.0.1')
->withPort(3306)
->withDbName('test')
->withUsername('admin')
->withPassword('admin');
$dbPool = new Pinga\DbPool\Pool($dbConfig, 2);
go(function () use (&$dbPool) {
$conn = $dbPool->get();
$result = $conn->query("select * from my_table;");
if ($result) {
var_dump($conn->fetchAll());
}
$dbPool->put($conn);
});
# Example Redis
<?php
require __DIR__ . '/vendor/autoload.php';
Swoole\Runtime::enableCoroutine();
$dbConfig = (new Pinga\DbPool\Config)
->withDriver('redis')
->withHost('127.0.0.1')
->withPort(6379)
->withAuth('admin')
->withReadTimeout(-1);
$dbPool = new Pinga\DbPool\Pool($dbConfig, 2);
go(function () use (&$dbPool) {
$conn = $dbPool->get();
$conn->set('test', 'test_value');
var_dump($conn->get('test'));
$dbPool->put($conn);
});