title: EasySwoole universal connection pool meta:

  • name: description content: EasySwoole universal connection pool,Coroutine connection pool, easyswoole connection pool
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|connection pool|swoole connection pool|universal connection pool

Pool manager

The pool manager can do global connection pool management, for example, register in initialize in EasySwooleEvent.php, and then get the connection pool in the controller to get the connection:

  1. public static function initialize()
  2. {
  3. // TODO: Implement initialize() method.
  4. date_default_timezone_set('Asia/Shanghai');
  5. $config = new \EasySwoole\Pool\Config();
  6. $redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(Config::getInstance()->getConf('REDIS1'));
  7. $redisConfig2 = new \EasySwoole\Redis\Config\RedisConfig(Config::getInstance()->getConf('REDIS2'));
  8. //Register connection pool management object
  9. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig1),'redis1');
  10. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig2),'redis2');
  11. }

The controller gets the connection pool connection:

  1. public function index()
  2. {
  3. //Take out the connection pool management object and getObj
  4. $redis1=\EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
  5. $redis2=\EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
  6. $redis1->set('name','Alan');
  7. var_dump($redis1->get('name'));
  8. $redis2->set('name','Allan');
  9. var_dump($redis2->get('name'));
  10. //Recycling object
  11. \EasySwoole\Pool\Manager::getInstance()->get('redis1')->recycleObj($redis1);
  12. \EasySwoole\Pool\Manager::getInstance()->get('redis2')->recycleObj($redis2);
  13. }