Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Adds missing options for PhpRedis #31182

Merged
merged 12 commits into from
Jan 21, 2020
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'])) {
Copy link
Contributor

@danijelk danijelk Jan 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested and confirm that $options isn't in this scope, as it is within createRedisClusterInstance which is probably what @Wizofgoz uses.

$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']);
}
});
}
}
80 changes: 80 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,60 @@ 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);

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 {
[$cursor, $returnedKeys] = $redis->scan($iterator);

if ($redis->client()->getOption(Redis::OPT_SCAN) === Redis::SCAN_RETRY) {
$this->assertNotFalse($returnedKeys);
} else {
$this->assertFalse($returnedKeys);
}
} 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 +652,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