Skip to content

Commit

Permalink
feat(redis_client): add UNLINK and JSON.MERGE (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Sep 18, 2023
1 parent d193729 commit 470177f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/redis_client/lib/src/redis_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ class RedisClient {
/// https://redis.io/commands/del
Future<void> delete({required String key}) => execute(['DEL', key]);

/// Unlinks the specified key.
/// Equivalent to the `UNLINK` command.
/// https://redis.io/commands/unlink
Future<void> unlink({required String key}) => execute(['UNLINK', key]);

/// Send a command to the Redis server.
Future<dynamic> execute(List<Object?> command) async {
return _runWithRetry(
Expand Down Expand Up @@ -402,6 +407,16 @@ class RedisJson {
Future<void> 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<void> merge({
required String key,
required Map<String, dynamic> value,
}) {
return _client.execute(['JSON.MERGE', key, r'$', json.encode(value)]);
}
}

extension on RedisSocketOptions {
Expand Down
17 changes: 17 additions & 0 deletions packages/redis_client/test/src/redis_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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));
});
Expand Down

0 comments on commit 470177f

Please sign in to comment.