PhpRedisConnector.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Illuminate\Redis\Connectors;
  3. use Redis;
  4. use RedisCluster;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Redis\Connections\PhpRedisConnection;
  7. use Illuminate\Redis\Connections\PhpRedisClusterConnection;
  8. class PhpRedisConnector
  9. {
  10. /**
  11. * Create a new clustered PhpRedis connection.
  12. *
  13. * @param array $config
  14. * @param array $options
  15. * @return \Illuminate\Redis\Connections\PhpRedisConnection
  16. */
  17. public function connect(array $config, array $options)
  18. {
  19. return new PhpRedisConnection($this->createClient(array_merge(
  20. $config, $options, Arr::pull($config, 'options', [])
  21. )));
  22. }
  23. /**
  24. * Create a new clustered PhpRedis connection.
  25. *
  26. * @param array $config
  27. * @param array $clusterOptions
  28. * @param array $options
  29. * @return \Illuminate\Redis\Connections\PhpRedisClusterConnection
  30. */
  31. public function connectToCluster(array $config, array $clusterOptions, array $options)
  32. {
  33. $options = array_merge($options, $clusterOptions, Arr::pull($config, 'options', []));
  34. return new PhpRedisClusterConnection($this->createRedisClusterInstance(
  35. array_map([$this, 'buildClusterConnectionString'], $config), $options
  36. ));
  37. }
  38. /**
  39. * Build a single cluster seed string from array.
  40. *
  41. * @param array $server
  42. * @return string
  43. */
  44. protected function buildClusterConnectionString(array $server)
  45. {
  46. return $server['host'].':'.$server['port'].'?'.http_build_query(Arr::only($server, [
  47. 'database', 'password', 'prefix', 'read_timeout',
  48. ]));
  49. }
  50. /**
  51. * Create the Redis client instance.
  52. *
  53. * @param array $config
  54. * @return \Redis
  55. */
  56. protected function createClient(array $config)
  57. {
  58. return tap(new Redis, function ($client) use ($config) {
  59. $this->establishConnection($client, $config);
  60. if (! empty($config['password'])) {
  61. $client->auth($config['password']);
  62. }
  63. if (! empty($config['database'])) {
  64. $client->select($config['database']);
  65. }
  66. if (! empty($config['prefix'])) {
  67. $client->setOption(Redis::OPT_PREFIX, $config['prefix']);
  68. }
  69. if (! empty($config['read_timeout'])) {
  70. $client->setOption(Redis::OPT_READ_TIMEOUT, $config['read_timeout']);
  71. }
  72. });
  73. }
  74. /**
  75. * Establish a connection with the Redis host.
  76. *
  77. * @param \Redis $client
  78. * @param array $config
  79. * @return void
  80. */
  81. protected function establishConnection($client, array $config)
  82. {
  83. $client->{($config['persistent'] ?? false) === true ? 'pconnect' : 'connect'}(
  84. $config['host'], $config['port'], Arr::get($config, 'timeout', 0)
  85. );
  86. }
  87. /**
  88. * Create a new redis cluster instance.
  89. *
  90. * @param array $servers
  91. * @param array $options
  92. * @return \RedisCluster
  93. */
  94. protected function createRedisClusterInstance(array $servers, array $options)
  95. {
  96. return new RedisCluster(
  97. null,
  98. array_values($servers),
  99. $options['timeout'] ?? 0,
  100. $options['read_timeout'] ?? 0,
  101. isset($options['persistent']) && $options['persistent']
  102. );
  103. }
  104. }