coroutine-redis.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. include '../vendor/autoload.php';
  3. use Smf\ConnectionPool\ConnectionPool;
  4. use Smf\ConnectionPool\Connectors\CoroutineRedisConnector;
  5. use Swoole\Coroutine\Redis;
  6. go(function () {
  7. // All Redis connections: [10, 30]
  8. $pool = new ConnectionPool(
  9. [
  10. 'minActive' => 10,
  11. 'maxActive' => 30,
  12. 'maxWaitTime' => 5,
  13. 'maxIdleTime' => 20,
  14. 'idleCheckInterval' => 10,
  15. ],
  16. new CoroutineRedisConnector,
  17. [
  18. 'host' => '127.0.0.1',
  19. 'port' => '6379',
  20. 'database' => 0,
  21. 'password' => null,
  22. 'options' => [
  23. 'connect_timeout' => 1,
  24. 'timeout' => 5,
  25. ],
  26. ]
  27. );
  28. echo "Initializing connection pool\n";
  29. $pool->init();
  30. defer(function () use ($pool) {
  31. echo "Close connection pool\n";
  32. $pool->close();
  33. });
  34. echo "Borrowing the connection from pool\n";
  35. /**@var Redis $connection */
  36. $connection = $pool->borrow();
  37. $connection->set('test', uniqid());
  38. $test = $connection->get('test');
  39. echo "Return the connection to pool as soon as possible\n";
  40. $pool->return($connection);
  41. var_dump($test);
  42. });