RedisSentinelFactory.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 RedisSentinel;
  13. class RedisSentinelFactory
  14. {
  15. protected bool $isOlderThan6 = false;
  16. public function __construct()
  17. {
  18. $this->isOlderThan6 = version_compare(phpversion('redis'), '6.0.0', '<');
  19. }
  20. public function create(array $options = []): RedisSentinel
  21. {
  22. if ($this->isOlderThan6) {
  23. return new RedisSentinel(
  24. $options['host'],
  25. (int) $options['port'],
  26. (float) $options['connectTimeout'],
  27. $options['persistent'],
  28. (int) $options['retryInterval'],
  29. (float) $options['readTimeout'],
  30. ...(isset($options['auth']) ? [$options['auth']] : []),
  31. );
  32. }
  33. // https://github.com/phpredis/phpredis/blob/develop/sentinel.md#examples-for-version-60-or-later
  34. return new RedisSentinel($options); /* @phpstan-ignore-line */
  35. }
  36. }