forked from open-smf/connection-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroutine-runtime-phpredis.php
47 lines (40 loc) · 1.19 KB
/
coroutine-runtime-phpredis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
include '../vendor/autoload.php';
use Smf\ConnectionPool\ConnectionPool;
use Smf\ConnectionPool\Connectors\PhpRedisConnector;
// Enable coroutine for PhpRedis
Swoole\Runtime::enableCoroutine();
go(function () {
// All Redis connections: [10, 30]
$pool = new ConnectionPool(
[
'minActive' => 10,
'maxActive' => 30,
'maxWaitTime' => 5,
'maxIdleTime' => 20,
'idleCheckInterval' => 10,
],
new PhpRedisConnector,
[
'host' => '127.0.0.1',
'port' => '6379',
'database' => 0,
'password' => null,
'timeout' => 5,
]
);
echo "Initializing connection pool\n";
$pool->init();
defer(function () use ($pool) {
echo "Close connection pool\n";
$pool->close();
});
echo "Borrowing the connection from pool\n";
/**@var Redis $connection */
$connection = $pool->borrow();
$connection->set('test', uniqid());
$test = $connection->get('test');
echo "Return the connection to pool as soon as possible\n";
$pool->return($connection);
var_dump($test);
});