feat(redis_client): add support for KEYS (#3159)

This commit is contained in:
Bryan Oltman
2025-06-09 15:08:02 -04:00
committed by GitHub
parent ab93bb35d3
commit dc98d9757c
2 changed files with 58 additions and 9 deletions
@@ -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<List<String>> keys({required String pattern}) async {
final rawResult = await execute(['KEYS', pattern]) as List<RespType>;
return rawResult
.whereType<RespBulkString>()
.map(
(result) => result.payload,
)
.whereType<String>()
.toList();
}
/// Set the value of a key.
/// Equivalent to the `SET` command.
/// https://redis.io/commands/set
@@ -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
}