diff --git a/packages/redis_client/lib/src/redis_client.dart b/packages/redis_client/lib/src/redis_client.dart index f399c938..fb57d2fb 100644 --- a/packages/redis_client/lib/src/redis_client.dart +++ b/packages/redis_client/lib/src/redis_client.dart @@ -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 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 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 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 execute(List command) async { return _runWithRetry(() async { diff --git a/packages/redis_client/test/src/redis_client_test.dart b/packages/redis_client/test/src/redis_client_test.dart index b22eec57..f126707c 100644 --- a/packages/redis_client/test/src/redis_client_test.dart +++ b/packages/redis_client/test/src/redis_client_test.dart @@ -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 {