RedisFactory.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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;
  12. use Hyperf\Contract\ConfigInterface;
  13. use Hyperf\Redis\Exception\InvalidRedisProxyException;
  14. use function Hyperf\Support\make;
  15. class RedisFactory
  16. {
  17. /**
  18. * @var RedisProxy[]
  19. */
  20. protected array $proxies = [];
  21. public function __construct(ConfigInterface $config)
  22. {
  23. $redisConfig = $config->get('redis');
  24. foreach ($redisConfig as $poolName => $item) {
  25. $this->proxies[$poolName] = make(RedisProxy::class, ['pool' => $poolName]);
  26. }
  27. }
  28. public function get(string $poolName): RedisProxy
  29. {
  30. $proxy = $this->proxies[$poolName] ?? null;
  31. if (! $proxy instanceof RedisProxy) {
  32. throw new InvalidRedisProxyException('Invalid Redis proxy.');
  33. }
  34. return $proxy;
  35. }
  36. }