From 470177fa2272eb078af07fffab070fe199d46ce1 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 18 Sep 2023 16:30:51 -0500 Subject: [PATCH] feat(redis_client): add `UNLINK` and `JSON.MERGE` (#1280) --- packages/redis_client/lib/src/redis_client.dart | 15 +++++++++++++++ .../test/src/redis_client_test.dart | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/packages/redis_client/lib/src/redis_client.dart b/packages/redis_client/lib/src/redis_client.dart index d2f42a48d..84195a1c2 100644 --- a/packages/redis_client/lib/src/redis_client.dart +++ b/packages/redis_client/lib/src/redis_client.dart @@ -187,6 +187,11 @@ class RedisClient { /// https://redis.io/commands/del Future delete({required String key}) => execute(['DEL', key]); + /// Unlinks the specified key. + /// Equivalent to the `UNLINK` command. + /// https://redis.io/commands/unlink + Future unlink({required String key}) => execute(['UNLINK', key]); + /// Send a command to the Redis server. Future execute(List command) async { return _runWithRetry( @@ -402,6 +407,16 @@ class RedisJson { Future delete({required String key}) { return _client.execute(['JSON.DEL', key, r'$']); } + + /// Merges the value of a key with the specified value. + /// Equivalent to the `JSON.MERGE` command. + /// https://redis.io/commands/json.merge + Future merge({ + required String key, + required Map value, + }) { + return _client.execute(['JSON.MERGE', key, r'$', json.encode(value)]); + } } extension on RedisSocketOptions { diff --git a/packages/redis_client/test/src/redis_client_test.dart b/packages/redis_client/test/src/redis_client_test.dart index 2e4ef0dcf..9b39f867a 100644 --- a/packages/redis_client/test/src/redis_client_test.dart +++ b/packages/redis_client/test/src/redis_client_test.dart @@ -186,8 +186,14 @@ void main() { await expectLater(client.get(key: key), completion(isNull)); await expectLater(client.set(key: key, value: value), completes); await expectLater(client.get(key: key), completion(equals(value))); + await expectLater(client.delete(key: key), completes); await expectLater(client.get(key: key), completion(isNull)); + + await expectLater(client.set(key: key, value: value), completes); + await expectLater(client.get(key: key), completion(equals(value))); + await expectLater(client.unlink(key: key), completes); + await expectLater(client.get(key: key), completion(isNull)); }); test( @@ -226,12 +232,23 @@ void main() { 'nested': {'bar': 42}, 'array': [1, 2, 3], }; + const other = { + 'bar': 'baz', + }; await expectLater(client.json.get(key: key), completion(isNull)); await expectLater(client.json.set(key: key, value: value), completes); await expectLater( client.json.get(key: key), completion(equals(value)), ); + await expectLater( + client.json.merge(key: key, value: other), + completes, + ); + await expectLater( + client.json.get(key: key), + completion(equals({...value, ...other})), + ); await expectLater(client.json.delete(key: key), completes); await expectLater(client.json.get(key: key), completion(isNull)); });