PDO connection pool, redis connection pool and mysqli connection pool are used in spool

Time:2021-6-13

Connection pool instructions

  • The implementation of all connection pools is based on the original connection pool of ConnectionPool;
  • The underlying principle of connection pool is automatic scheduling based on channel;
  • Developers need to guarantee that the returned connection is reusable;
  • If the connection is not reusable, call$pool->put(null);Return an empty connection;
  • After returning the empty connection, the original connection pool will re create the connection to ensure the same number of connection pools.

PDO connection pool

SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;

Coroutine\run(function () {
    $config = (new PDOConfig)
        ->withHost('127.0.0.1')
        ->withPort(3306)
        // ->withUnixSocket('/tmp/mysql.sock')
        ->withDbName('test')
        ->withCharset('utf8mb4')
        ->withUsername('root')
        ->withPassword('root');
    //Create a connection pool object. 64 connections are created by default
    $pool = new PDOPool($config);
    
    for ($n = N; $n--;) {
        Coroutine::create(function () use ($pool) {
            //Get a connection
            $pdo = $pool->get();
            $statement = $pdo->prepare('SELECT ? + ?');
            if (!$statement) {
                throw new RuntimeException('Prepare failed');
            }
            $a = mt_rand(1, 100);
            $b = mt_rand(1, 100);
            $result = $statement->execute([$a, $b]);
            if (!$result) {
                throw new RuntimeException('Execute failed');
            }
            $result = $statement->fetchAll();
            if ($a + $b !== (int)$result[0][0]) {
                throw new RuntimeException('Bad result');
            }
            //Recycle connection
            $pool->put($pdo);
        });
    }
});

$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;

Redis connection pool

SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;

Coroutine\run(function () {
    $config = (new RedisConfig)
        ->withHost('127.0.0.1')
        ->withPort(6379)
        ->withAuth('')
        ->withDbIndex(0)
        ->withTimeout(1);
    //Create a connection pool object. 64 connections are created by default
    $pool = new RedisPool($config);
    
    for ($n = N; $n--;) {
        Coroutine::create(function () use ($pool) {
            //Get a connection
            $redis = $pool->get();
            $result = $redis->set('foo', 'bar');
            if (!$result) {
                throw new RuntimeException('Set failed');
            }
            $result = $redis->get('foo');
            if ($result !== 'bar') {
                throw new RuntimeException('Get failed');
            }
            //Recycle connection
            $pool->put($redis);
        });
    }
});

$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . (N * 2) . ' queries' . PHP_EOL;

Mysqli connection pool

SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;

Coroutine\run(function () {
    $config = (new MysqliConfig)
        ->withHost('127.0.0.1')
        ->withPort(3306)
        // ->withUnixSocket('/tmp/mysql.sock')
        ->withDbName('test')
        ->withCharset('utf8mb4')
        ->withUsername('root')
        ->withPassword('root');
    //Create a connection pool object. 64 connections are created by default
    $pool = new MysqliPool($config);
    for ($n = N; $n--;) {
        Coroutine::create(function () use ($pool) {
            //Get a connection
            $mysqli = $pool->get();
            $statement = $mysqli->prepare('SELECT ? + ?');
            if (!$statement) {
                throw new RuntimeException('Prepare failed');
            }
            $a = mt_rand(1, 100);
            $b = mt_rand(1, 100);
            if (!$statement->bind_param('dd', $a, $b)) {
                throw new RuntimeException('Bind param failed');
            }
            if (!$statement->execute()) {
                throw new RuntimeException('Execute failed');
            }
            if (!$statement->bind_result($result)) {
                throw new RuntimeException('Bind result failed');
            }
            if (!$statement->fetch()) {
                throw new RuntimeException('Fetch failed');
            }
            if ($a + $b !== (int)$result) {
                throw new RuntimeException('Bad result');
            }
            while ($statement->fetch()) {
                continue;
            }
            //Recycle connection
            $pool->put($mysqli);
        });
    }
});

$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;

Recommended Today

[database] MySQL exercises and answers (educational administration management system)_ MySQL_ 5.7)

✨ statement The answer to the exercise is not guaranteed to be completely correct, which is for reference only MySQL:5.7 Recommended software: Navicat premium Learning SQL statements requires diligent practice! ✨ Field description of each table course CId Course serial number, primary key PCId Prerequisite course DId Opening department number, foreign key CName Course name […]