title: Redis connection pool meta:

  • name: description content: easyswoole Redis connection pool component, implemented by universal connection pool and redis coroutine client package
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole redis|redis connection pool|swoole redis|redis connection pool

Redis-Pool

Redis-Pool is based on [pool universal connection pool] (./Pool/introduction.md), [redis coroutine client] (./Redis/introduction.md) packaged components

Installation

  1. composer require easyswoole/redis-pool

Connection pool registration

Register the Redis connection pool before using the connection:

  1. //Redis connection pool registration (config defaults to 127.0.0.1, port 6379)
  2. \EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
  3. //Redis cluster connection pool registration
  4. \EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
  5. ['172.16.253.156', 9001],
  6. ['172.16.253.156', 9002],
  7. ['172.16.253.156', 9003],
  8. ['172.16.253.156', 9004],
  9. ]
  10. ));

Connection pool configuration

When registered, the poolConf that will return the connection pool is used to configure the connection pool:

  1. $redisPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
  2. //Configure connection pool connections
  3. $redisPoolConfig->setMinObjectNum(5);
  4. $redisPoolConfig->setMaxObjectNum(20);
  5. $redisClusterPoolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
  6. ['172.16.253.156', 9001],
  7. ['172.16.253.156', 9002],
  8. ['172.16.253.156', 9003],
  9. ['172.16.253.156', 9004],
  10. ]
  11. ));
  12. //Configure connection pool connections
  13. $redisPoolConfig->setMinObjectNum(5);
  14. $redisPoolConfig->setMaxObjectNum(20);

Use a connection pool:

  1. //Redis connection pool registration (config defaults to 127.0.0.1, port 6379)
  2. \EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
  3. //Redis cluster connection pool registration
  4. \EasySwoole\RedisPool\Redis::getInstance()->register('redisCluster',new \EasySwoole\Redis\Config\RedisClusterConfig([
  5. ['172.16.253.156', 9001],
  6. ['172.16.253.156', 9002],
  7. ['172.16.253.156', 9003],
  8. ['172.16.253.156', 9004],
  9. ]
  10. ));
  11. go(function () {
  12. //defer mode to get the connection
  13. $redis = \EasySwoole\RedisPool\Redis::defer('redis');
  14. $redisCluster = \EasySwoole\RedisPool\Redis::defer('redisCluster');
  15. $redis->set('a', 1);
  16. $redisCluster->set('a', 1);
  17. //invoke mode to get the connection
  18. \EasySwoole\RedisPool\Redis::invoker('redis', function (\EasySwoole\Redis\Redis $redis) {
  19. var_dump($redis->set('a', 1));
  20. });
  21. \EasySwoole\RedisPool\Redis::invoker('redisCluster', function (\EasySwoole\Redis\Redis $redis) {
  22. var_dump($redis->set('a', 1));
  23. });
  24. //Get the connection pool object
  25. $redisPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redis');
  26. $redisClusterPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redisCluster');
  27. $redis = $redisPool->getObj();
  28. $redisPool->recycleObj($redis);
  29. //Clear the timer in the pool
  30. \EasySwoole\Component\Timer::getInstance()->clearAll();
  31. });