PoolFactory.php 938 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Redis\Pool;
  12. use Hyperf\Di\Container;
  13. use Psr\Container\ContainerInterface;
  14. class PoolFactory
  15. {
  16. /**
  17. * @var RedisPool[]
  18. */
  19. protected array $pools = [];
  20. public function __construct(protected ContainerInterface $container)
  21. {
  22. }
  23. public function getPool(string $name): RedisPool
  24. {
  25. if (isset($this->pools[$name])) {
  26. return $this->pools[$name];
  27. }
  28. if ($this->container instanceof Container) {
  29. $pool = $this->container->make(RedisPool::class, ['name' => $name]);
  30. } else {
  31. $pool = new RedisPool($this->container, $name);
  32. }
  33. return $this->pools[$name] = $pool;
  34. }
  35. }