PHP code example of phpdot / pool

1. Go to this page and download the library: Download phpdot/pool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpdot / pool example snippets


use PHPdot\Contracts\Pool\ConnectorInterface;

final class RedisConnector implements ConnectorInterface
{
    public function connect(): object
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);

        return $redis;
    }

    public function isAlive(object $connection): bool
    {
        return $connection->ping() === true; // lightweight server round-trip
    }

    public function close(object $connection): void
    {
        $connection->close();
    }
}

use PHPdot\Pool\Pool;
use PHPdot\Pool\PoolConfig;

$pool = new Pool(new RedisConnector(), new PoolConfig(minConnections: 4, maxConnections: 20));
$redis = $pool->borrow();        // initialises, then borrows

$redis = $pool->borrow();       // object, or throws BorrowTimeoutException / PoolClosedException

try {
    $redis->set('key', 'value');
} finally {
    $pool->release($redis);     // return for reuse; double release is ignored
}

$pool->discard($redis);         // close + free the slot, never re-pool

use PHPdot\Pool\PoolConfig;

new PoolConfig(
    minConnections: 2,                  // pre-created on init; pool never shrinks below this
    maxConnections: 10,                 // hard cap per worker
    borrowTimeout: 3.0,                 // seconds to wait when exhausted
    maxIdleTime: 300.0,                 // seconds before an idle connection is reaped (0.0 = off)
    idleCheckInterval: 30.0,            // seconds between idle-cleanup runs
    heartbeatInterval: 0.0,             // seconds between heartbeats (0.0 = off)
    validateOnBorrowAfterIdle: 5.0,     // isAlive() on borrow after N idle secs; 0.0 = always; <0 = off
    validateOnReturn: true,             // isAlive() on release; discard dead instead of re-pooling
);

$s = $pool->stats();
$s->active; $s->idle; $s->total;                     // live counts
$s->borrowCount; $s->releaseCount; $s->discardCount; // lifetime counters
$s->createCount; $s->closeCount; $s->timeoutCount; $s->waitingCount;

use function PHPdot\Pool\pooled;

$builder->add(DatabaseConnection::class, pooled('database', DatabaseConnector::class));
$builder->add('redis.cache',             pooled('redis.cache', RedisConnector::class));

// config/database.php
return [
    'host' => env('DB_HOST'),
    'pool' => ['min' => 5, 'max' => 100],
];

// config/redis.php
return [
    'pools' => [
        'cache'   => ['host' => env('REDIS_CACHE_HOST'),   'pool' => ['max' => 20]],
        'session' => ['host' => env('REDIS_SESSION_HOST'), 'pool' => ['max' => 5]],
    ],
];

#[ServerListener]
final class DrainPools
{
    public function __construct(private readonly PoolRegistry $pools) {}

    public function __invoke(WorkerExiting $event): void
    {
        $this->pools->suspendAll();
    }
}