title: Redis coroutine client meta:

  • name: description content: Redis coroutine client,Implemented by swoole coroutine client,Covers the method of redis 99%
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole redis| Swoole Redis coroutine client|swoole Redis|Redis coroutine

Redis Connection Pool Example

redis-pool component

The connection pool can be implemented by directly installing [redis-pool component] (../redisPool.md):

  1. composer require easyswoole/redis-pool

Install the easyswoole/pool component custom implementation:

  1. composer require easyswoole/pool

Add redisPool manager

Add file /App/Pool/RedisPool.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/10/15 0015
  6. * Time: 14:46
  7. */
  8. namespace App\Pool;
  9. use EasySwoole\Pool\Config;
  10. use EasySwoole\Pool\AbstractPool;
  11. use EasySwoole\Redis\Config\RedisConfig;
  12. use EasySwoole\Redis\Redis;
  13. class RedisPool extends AbstractPool
  14. {
  15. protected $redisConfig;
  16. /**
  17. * Override the constructor in order to pass in the redis configuration
  18. * RedisPool constructor.
  19. * @param Config $conf
  20. * @param RedisConfig $redisConfig
  21. * @throws \EasySwoole\Pool\Exception\Exception
  22. */
  23. public function __construct(Config $conf,RedisConfig $redisConfig)
  24. {
  25. parent::__construct($conf);
  26. $this->redisConfig = $redisConfig;
  27. }
  28. protected function createObject()
  29. {
  30. //New redis based on the incoming redis configuration
  31. $redis = new Redis($this->redisConfig);
  32. return $redis;
  33. }
  34. }

Register to the Manager:

  1. $config = new \EasySwoole\Pool\Config();
  2. $redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS1'));
  3. $redisConfig2 = new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS2'));
  4. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig1),'redis1');
  5. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig2),'redis2');

Call (can be called globally in the controller):

  1. go(function (){
  2. $redis1=\EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
  3. $redis2=\EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
  4. $redis1->set('name','Alan');
  5. var_dump($redis1->get('name'));
  6. $redis2->set('name','Allan');
  7. var_dump($redis2->get('name'));
  8. //Recycling object
  9. \EasySwoole\Pool\Manager::getInstance()->get('redis1')->recycleObj($redis1);
  10. \EasySwoole\Pool\Manager::getInstance()->get('redis2')->recycleObj($redis2);
  11. });

::: warning For detailed usage, see [pool universal connection pool] (../Pool/introduction.md) :::

::: warning This article redis connection pool is based on [pool universal connection pool] (../Pool/introduction.md) :::