RedisManager.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Redis;
  3. use InvalidArgumentException;
  4. use Illuminate\Contracts\Redis\Factory;
  5. /**
  6. * @mixin \Illuminate\Redis\Connections\Connection
  7. */
  8. class RedisManager implements Factory
  9. {
  10. /**
  11. * The name of the default driver.
  12. *
  13. * @var string
  14. */
  15. protected $driver;
  16. /**
  17. * The Redis server configurations.
  18. *
  19. * @var array
  20. */
  21. protected $config;
  22. /**
  23. * The Redis connections.
  24. *
  25. * @var mixed
  26. */
  27. protected $connections;
  28. /**
  29. * Create a new Redis manager instance.
  30. *
  31. * @param string $driver
  32. * @param array $config
  33. * @return void
  34. */
  35. public function __construct($driver, array $config)
  36. {
  37. $this->driver = $driver;
  38. $this->config = $config;
  39. }
  40. /**
  41. * Get a Redis connection by name.
  42. *
  43. * @param string|null $name
  44. * @return \Illuminate\Redis\Connections\Connection
  45. */
  46. public function connection($name = null)
  47. {
  48. $name = $name ?: 'default';
  49. if (isset($this->connections[$name])) {
  50. return $this->connections[$name];
  51. }
  52. return $this->connections[$name] = $this->resolve($name);
  53. }
  54. /**
  55. * Resolve the given connection by name.
  56. *
  57. * @param string|null $name
  58. * @return \Illuminate\Redis\Connections\Connection
  59. *
  60. * @throws \InvalidArgumentException
  61. */
  62. public function resolve($name = null)
  63. {
  64. $name = $name ?: 'default';
  65. $options = $this->config['options'] ?? [];
  66. if (isset($this->config[$name])) {
  67. return $this->connector()->connect($this->config[$name], $options);
  68. }
  69. if (isset($this->config['clusters'][$name])) {
  70. return $this->resolveCluster($name);
  71. }
  72. throw new InvalidArgumentException("Redis connection [{$name}] not configured.");
  73. }
  74. /**
  75. * Resolve the given cluster connection by name.
  76. *
  77. * @param string $name
  78. * @return \Illuminate\Redis\Connections\Connection
  79. */
  80. protected function resolveCluster($name)
  81. {
  82. $clusterOptions = $this->config['clusters']['options'] ?? [];
  83. return $this->connector()->connectToCluster(
  84. $this->config['clusters'][$name], $clusterOptions, $this->config['options'] ?? []
  85. );
  86. }
  87. /**
  88. * Get the connector instance for the current driver.
  89. *
  90. * @return \Illuminate\Redis\Connectors\PhpRedisConnector|\Illuminate\Redis\Connectors\PredisConnector
  91. */
  92. protected function connector()
  93. {
  94. switch ($this->driver) {
  95. case 'predis':
  96. return new Connectors\PredisConnector;
  97. case 'phpredis':
  98. return new Connectors\PhpRedisConnector;
  99. }
  100. }
  101. /**
  102. * Return all of the created connections.
  103. *
  104. * @return array
  105. */
  106. public function connections()
  107. {
  108. return $this->connections;
  109. }
  110. /**
  111. * Pass methods onto the default Redis connection.
  112. *
  113. * @param string $method
  114. * @param array $parameters
  115. * @return mixed
  116. */
  117. public function __call($method, $parameters)
  118. {
  119. return $this->connection()->{$method}(...$parameters);
  120. }
  121. }