From dc98d9757cfea1f699a9ce421b85cea9d44e3af6 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Mon, 9 Jun 2025 15:08:02 -0400 Subject: [PATCH] feat(redis_client): add support for KEYS (#3159) --- .../redis_client/lib/src/redis_client.dart | 14 +++++ .../test/src/redis_client_test.dart | 53 +++++++++++++++---- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/packages/redis_client/lib/src/redis_client.dart b/packages/redis_client/lib/src/redis_client.dart index c2137a45..2253c0d9 100644 --- a/packages/redis_client/lib/src/redis_client.dart +++ b/packages/redis_client/lib/src/redis_client.dart @@ -170,6 +170,20 @@ class RedisClient { return execute(['AUTH', username, password]); } + /// Returns all keys matching the given pattern. + /// Equivalent to the `KEYS` command. + /// https://redis.io/commands/keys + Future> keys({required String pattern}) async { + final rawResult = await execute(['KEYS', pattern]) as List; + return rawResult + .whereType() + .map( + (result) => result.payload, + ) + .whereType() + .toList(); + } + /// Set the value of a key. /// Equivalent to the `SET` command. /// https://redis.io/commands/set diff --git a/packages/redis_client/test/src/redis_client_test.dart b/packages/redis_client/test/src/redis_client_test.dart index 0867aeed..01c5b7c7 100644 --- a/packages/redis_client/test/src/redis_client_test.dart +++ b/packages/redis_client/test/src/redis_client_test.dart @@ -118,6 +118,44 @@ void main() { }); }); + group('KEYS', () { + setUp(() async { + await client.connect(); + }); + + tearDown(() async { + try { + await client.execute(['FLUSHALL', 'SYNC']); + } on Exception { + // ignore + } + }); + + group('when no keys match the pattern', () { + test('returns an empty list', () async { + final keys = await client.keys(pattern: 'foo'); + expect(keys, isEmpty); + }); + }); + + group('when keys match the pattern', () { + setUp(() async { + await client.set(key: 'foo', value: 'bar'); + await client.set( + key: 'foofoo', // cspell:disable-line + value: 'barbar', // cspell:disable-line + ); + await client.set(key: 'bar', value: 'baz'); + }); + + test('returns the keys', () async { + final keys = await client.keys(pattern: 'foo*'); + expect(keys.length, equals(2)); + expect(keys, containsAll(['foo', 'foofoo'])); // cspell:disable-line + }); + }); + }); + group('AUTH', () { setUp(() async { await client.connect(); @@ -177,7 +215,7 @@ void main() { tearDown(() async { try { - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore } @@ -227,7 +265,7 @@ void main() { tearDown(() async { try { - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore } @@ -261,10 +299,7 @@ void main() { tearDown(() async { try { - for (final pair in kvPairs) { - await expectLater(client.delete(key: pair.key), completes); - } - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore } @@ -291,7 +326,7 @@ void main() { tearDown(() async { try { - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore } @@ -408,7 +443,7 @@ void main() { tearDown(() async { try { - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore } @@ -672,7 +707,7 @@ void main() { tearDown(() async { try { - await client.execute(['FLUSHALL SYNC']); + await client.execute(['FLUSHALL', 'SYNC']); } on Exception { // ignore }