Skip to content

Commit

Permalink
[6.x] Adds missing options for PhpRedis (#31182)
Browse files Browse the repository at this point in the history
* Added missing options for PhpRedis
  • Loading branch information
Wizofgoz authored and taylorotwell committed Jan 21, 2020
1 parent 3ffab1b commit e720b86
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/Redis/Connectors/PhpRedisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ protected function createClient(array $config)
if (! empty($config['read_timeout'])) {
$client->setOption(Redis::OPT_READ_TIMEOUT, $config['read_timeout']);
}

if (! empty($options['serializer'])) {
$client->setOption(Redis::OPT_SERIALIZER, $options['serializer']);
}

if (! empty($config['scan'])) {
$client->setOption(Redis::OPT_SCAN, $config['scan']);
}
});
}

Expand Down Expand Up @@ -151,6 +159,18 @@ protected function createRedisClusterInstance(array $servers, array $options)
if (! empty($options['prefix'])) {
$client->setOption(RedisCluster::OPT_PREFIX, $options['prefix']);
}

if (! empty($options['serializer'])) {
$client->setOption(RedisCluster::OPT_SERIALIZER, $options['serializer']);
}

if (! empty($config['scan'])) {
$client->setOption(RedisCluster::OPT_SCAN, $config['scan']);
}

if (! empty($config['failover'])) {
$client->setOption(RedisCluster::OPT_SLAVE_FAILOVER, $config['failover']);
}
});
}
}
82 changes: 82 additions & 0 deletions tests/Redis/RedisConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Redis\RedisManager;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Redis;

class RedisConnectionTest extends TestCase
{
Expand Down Expand Up @@ -547,6 +549,62 @@ public function testItPersistsConnection()
);
}

public function testItScansForKeys()
{
foreach ($this->connections() as $redis) {
$initialKeys = ['test:scan:1', 'test:scan:2'];

foreach ($initialKeys as $k => $key) {
$redis->set($key, 'test');
$initialKeys[$k] = $this->getPrefix($redis->client()).$key;
}

$iterator = null;

do {
[$cursor, $returnedKeys] = $redis->scan($iterator);

if (! is_array($returnedKeys)) {
$returnedKeys = [$returnedKeys];
}

foreach ($returnedKeys as $returnedKey) {
$this->assertTrue(in_array($returnedKey, $initialKeys));
}
} while ($iterator > 0);

$redis->flushAll();
}
}

public function testPhpRedisScanOption()
{
foreach ($this->connections() as $redis) {
if ($redis->client() instanceof Client) {
continue;
}

$iterator = null;

do {
$returned = $redis->scan($iterator);

if ($redis->client()->getOption(Redis::OPT_SCAN) === Redis::SCAN_RETRY) {
$this->assertEmpty($returned);
}
} while ($iterator > 0);
}
}

private function getPrefix($client)
{
if ($client instanceof Redis) {
return $client->getOption(Redis::OPT_PREFIX);
}

return $client->getOptions()->prefix;
}

public function testMacroable()
{
Connection::macro('foo', function () {
Expand Down Expand Up @@ -596,7 +654,31 @@ public function connections()
],
]);

$serializerPhpRedis = new RedisManager(new Application, 'phpredis', [
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 7,
'options' => ['serializer' => Redis::SERIALIZER_JSON],
'timeout' => 0.5,
],
]);

$scanRetryPhpRedis = new RedisManager(new Application, 'phpredis', [
'cluster' => false,
'default' => [
'host' => $host,
'port' => $port,
'database' => 8,
'options' => ['scan' => Redis::SCAN_RETRY],
'timeout' => 0.5,
],
]);

$connections[] = $prefixedPhpredis->connection();
$connections[] = $serializerPhpRedis->connection();
$connections[] = $scanRetryPhpRedis->connection();
$connections['persistent'] = $persistentPhpRedis->connection();

return $connections;
Expand Down

0 comments on commit e720b86

Please sign in to comment.