feat(redis_client): add UNLINK and JSON.MERGE (#1280)

This commit is contained in:
Felix Angelov
2023-09-18 16:30:51 -05:00
committed by GitHub
parent d19372955f
commit 470177fa22
2 changed files with 32 additions and 0 deletions
@@ -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(
@@ -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 {
@@ -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));
});