RedisPool.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Collection\Arr;
  13. use Hyperf\Contract\ConfigInterface;
  14. use Hyperf\Contract\ConnectionInterface;
  15. use Hyperf\Pool\Pool;
  16. use Hyperf\Redis\Frequency;
  17. use Hyperf\Redis\RedisConnection;
  18. use InvalidArgumentException;
  19. use Psr\Container\ContainerInterface;
  20. use function Hyperf\Support\make;
  21. class RedisPool extends Pool
  22. {
  23. protected array $config;
  24. public function __construct(ContainerInterface $container, protected string $name)
  25. {
  26. $config = $container->get(ConfigInterface::class);
  27. $key = sprintf('redis.%s', $this->name);
  28. if (! $config->has($key)) {
  29. throw new InvalidArgumentException(sprintf('config[%s] is not exist!', $key));
  30. }
  31. $this->config = $config->get($key);
  32. $options = Arr::get($this->config, 'pool', []);
  33. $this->frequency = make(Frequency::class, [$this]);
  34. parent::__construct($container, $options);
  35. }
  36. public function getName(): string
  37. {
  38. return $this->name;
  39. }
  40. public function getConfig(): array
  41. {
  42. return $this->config;
  43. }
  44. protected function createConnection(): ConnectionInterface
  45. {
  46. return new RedisConnection($this->container, $this, $this->config);
  47. }
  48. }