diff --git a/src/Illuminate/Redis/Connectors/PhpRedisConnector.php b/src/Illuminate/Redis/Connectors/PhpRedisConnector.php index 447353221b54..1b8921f7fa1c 100644 --- a/src/Illuminate/Redis/Connectors/PhpRedisConnector.php +++ b/src/Illuminate/Redis/Connectors/PhpRedisConnector.php @@ -92,8 +92,45 @@ protected function createClient(array $config) */ protected function establishConnection($client, array $config) { - $client->{($config['persistent'] ?? false) === true ? 'pconnect' : 'connect'}( - $config['host'], $config['port'], Arr::get($config, 'timeout', 0) + if ($config['persistent'] ?? false) { + $this->establishPersistentConnection($client, $config); + } else { + $this->establishRegularConnection($client, $config); + } + } + + /** + * Establish a persistent connection with the Redis host. + * + * @param \Redis $client + * @param array $config + * @return void + */ + protected function establishPersistentConnection($client, array $config) + { + $client->pconnect( + $config['host'], + $config['port'], + Arr::get($config, 'timeout', 0.0), + Arr::get($config, 'persistent_id', null) + ); + } + + /** + * Establish a regular connection with the Redis host. + * + * @param \Redis $client + * @param array $config + * @return void + */ + protected function establishRegularConnection($client, array $config) + { + $client->connect( + $config['host'], + $config['port'], + Arr::get($config, 'timeout', 0.0), + Arr::get($config, 'reserved', null), + Arr::get($config, 'retry_interval', 0) ); } diff --git a/tests/Redis/RedisConnectionTest.php b/tests/Redis/RedisConnectionTest.php index 9226787abc98..c8acdae1c5f3 100644 --- a/tests/Redis/RedisConnectionTest.php +++ b/tests/Redis/RedisConnectionTest.php @@ -573,6 +573,21 @@ public function it_runs_raw_command() } } + /** + * @test + */ + public function it_persists_connection() + { + if (PHP_ZTS) { + $this->markTestSkipped('PhpRedis does not support persistent connections with PHP_ZTS enabled.'); + } + + $this->assertEquals( + 'laravel', + $this->connections()['persistent']->getPersistentID() + ); + } + public function connections() { $connections = [ @@ -595,7 +610,21 @@ public function connections() ], ]); + $persistentPhpRedis = new RedisManager('phpredis', [ + 'cluster' => false, + 'default' => [ + 'host' => $host, + 'port' => $port, + 'database' => 6, + 'options' => ['prefix' => 'laravel:'], + 'timeout' => 0.5, + 'persistent' => true, + 'persistent_id' => 'laravel', + ], + ]); + $connections[] = $prefixedPhpredis->connection(); + $connections['persistent'] = $persistentPhpRedis->connection(); } return $connections;