feat(redis_client): add increment and incrementBy (#3014)

This commit is contained in:
Felix Angelov
2025-03-27 11:32:25 -05:00
committed by GitHub
parent 612ded12c0
commit 58ec678a63
2 changed files with 50 additions and 0 deletions
@@ -1,3 +1,4 @@
// cspell:words INCRBYFLOAT
import 'dart:async';
import 'dart:convert';
import 'dart:io';
@@ -200,6 +201,23 @@ class RedisClient {
/// https://redis.io/commands/unlink
Future<void> unlink({required String key}) => execute(['UNLINK', key]);
/// Increment the floating point number stored at key by one.
/// Returns the newly incremented value.
/// Equivalent to the `INCR` command.
/// https://redis.io/commands/incr
Future<num> increment({required String key}) async {
return await execute(['INCR', key]) as num;
}
/// Increment the floating point number stored at key by the specified value.
/// Returns the newly incremented value.
/// Equivalent to the `INCRBYFLOAT` command.
/// https://redis.io/commands/incrbyfloat
Future<num> incrementBy({required String key, required num value}) async {
final result = await execute(['INCRBYFLOAT', key, value]) as String;
return num.parse(result);
}
/// Send a command to the Redis server.
Future<dynamic> execute(List<Object?> command) async {
return _runWithRetry(() async {
@@ -1,3 +1,4 @@
// cspell:words INCRBYFLOAT
import 'dart:async';
import 'dart:io';
@@ -221,6 +222,37 @@ void main() {
});
});
group('INCR/INCRBYFLOAT', () {
setUp(() async {
await client.connect();
});
tearDown(() async {
try {
await client.execute(['RESET']);
await client.execute(['FLUSHALL']);
} on Exception {
// ignore
}
});
test('completes', () async {
const key = 'key';
const value = '10';
await expectLater(client.increment(key: key), completion(equals(1)));
await expectLater(client.set(key: key, value: value), completes);
await expectLater(
client.incrementBy(key: key, value: 42.2),
completion(equals(52.2)),
);
await expectLater(
client.incrementBy(key: key, value: -52.2),
completion(equals(0.0)),
);
await expectLater(client.delete(key: key), completes);
});
});
group('JSON', () {
group('GET/SET/DEL/MERGE', () {
setUp(() async {