coroutine-runtime-phpredis.php 1.2 KB

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