Skip to content

Commit 7f597e3

Browse files
committed
feat: implement option redis OPT_SCAN for phpredis. Issue #608
1 parent 3d9061f commit 7f597e3

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

docs/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ framework:
295295
provider: snc_redis.cache
296296
```
297297

298+
### PhpRedis Scan Option ###
299+
300+
To enable the `SCAN_PREFIX` feature for PhpRedis clients, add the `scan` option:
301+
302+
```yaml
303+
snc_redis:
304+
clients:
305+
default:
306+
type: phpredis
307+
alias: default
308+
dsn: redis://localhost
309+
options:
310+
scan: prefix
311+
```
312+
298313
### Complete configuration example ###
299314

300315
``` yaml
@@ -323,6 +338,7 @@ snc_redis:
323338
connection_timeout: 10
324339
connection_persistent: true
325340
read_write_timeout: 30
341+
scan: prefix
326342
iterable_multibulk: false
327343
throw_errors: true
328344
cluster: predis

src/DependencyInjection/SncRedisExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Component\DependencyInjection\Extension\Extension;
3232
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
3333
use Symfony\Component\DependencyInjection\Reference;
34+
use Redis;
3435

3536
use function array_map;
3637
use function assert;
@@ -160,6 +161,10 @@ private function loadPredisClient(array $client, ContainerBuilder $container): v
160161
// fix ssl configuration key name
161162
$client['options']['ssl'] = $client['options']['parameters']['ssl_context'] ?? [];
162163

164+
if (isset($client['options']['scan']) && $client['options']['scan'] === 'prefix') {
165+
$client['options']['scan'] = Redis::SCAN_PREFIX;
166+
}
167+
163168
unset($client['options']['connection_async']);
164169
unset($client['options']['connection_timeout']);
165170
unset($client['options']['connection_persistent']);

src/Factory/PhpredisClientFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ private function createClient(RedisDsn $dsn, string $class, string $alias, array
301301
$client->setOption($class::OPT_PREFIX, $options['prefix']);
302302
}
303303

304+
if (isset($options['scan']) && $options['scan'] === 'prefix') {
305+
$client->setOption($class::OPT_SCAN, $class::SCAN_PREFIX);
306+
}
307+
304308
if (isset($options['read_write_timeout'])) {
305309
$client->setOption($class::OPT_READ_TIMEOUT, (float) $options['read_write_timeout']);
306310
}

tests/Factory/PhpredisClientFactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,19 @@ public function testCreateWithConnectionPersistentFalse(): void
468468
$this->assertInstanceOf(Redis::class, $client);
469469
$this->assertNull($client->getPersistentID());
470470
}
471+
472+
public function testScanOption()
473+
{
474+
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
475+
$options = [
476+
'connection_timeout' => 0.0,
477+
'connection_persistent' => false,
478+
'scan' => 'prefix'
479+
];
480+
481+
$client = $factory->create(Redis::class, ['redis://localhost:6379'], $options, 'default', false);
482+
483+
$this->assertInstanceOf(Redis::class, $client);
484+
$this->assertEquals(Redis::SCAN_PREFIX, $client->getOption(Redis::OPT_SCAN));
485+
}
471486
}

0 commit comments

Comments
 (0)