Skip to content

Commit

Permalink
add redis multi function (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion authored Nov 16, 2023
1 parent ec61d4c commit 1859c8e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
53 changes: 40 additions & 13 deletions lib/src/redis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:ioredis/ioredis.dart';
import 'package:ioredis/src/default.dart';
import 'package:ioredis/src/redis_connection_pool.dart';
import 'package:ioredis/src/redis_multi_command.dart';
import 'package:ioredis/src/redis_response.dart';

class Redis {
Expand Down Expand Up @@ -54,15 +55,8 @@ class Redis {
/// ```
Future<dynamic> set(String key, String value,
[String? option, dynamic optionValue]) async {
List<String> commands = <String>[
'SET',
_setPrefixInKeys(<String>[key]).first,
value.toString()
];
if (option != null && optionValue != null) {
commands.addAll(<String>[option.toUpperCase(), optionValue.toString()]);
}
String? val = await sendCommand(commands);
String? val =
await sendCommand(getCommandToSetData(key, value, option, optionValue));
if (!RedisResponse.ok(val)) {
throw Exception(val);
}
Expand All @@ -73,10 +67,7 @@ class Redis {
/// await redis.get('foo');
/// ```
Future<String?> get(String key) async {
return await sendCommand(<String>[
'GET',
_setPrefixInKeys(<String>[key]).first
]);
return await sendCommand(getCommandToGetData(key));
}

/// Get value of a key
Expand Down Expand Up @@ -114,6 +105,19 @@ class Redis {
await sendCommand(<String>['FLUSHDB']);
}

/// multi commands
/// ```
/// List<dynamic> result = await redis.multi()
/// .set('foo', 'bar')
/// .set('bar', 'foo')
/// .get('foo')
/// .get('bar')
/// .exec();
/// ```
RedisMulti multi() {
return RedisMulti(this);
}

/// Subscribe to channel
/// ```
/// RedisSubscriber subscriber = await redis.subscribe('channel');
Expand Down Expand Up @@ -161,6 +165,29 @@ class Redis {
return pool.sendCommand(commandList);
}

/// get command to set data to redis
List<String> getCommandToSetData(String key, String value,
[String? option, dynamic optionValue]) {
List<String> command = <String>[
'SET',
_setPrefixInKeys(<String>[key]).first,
value.toString()
];
if (option != null && optionValue != null) {
command.addAll(<String>[option.toUpperCase(), optionValue.toString()]);
}
return command;
}

/// get command to get data from redis
List<String> getCommandToGetData(String key) {
List<String> command = <String>[
'GET',
_setPrefixInKeys(<String>[key]).first
];
return command;
}

/// setting keys prefix before setting or getting values
List<String> _setPrefixInKeys(List<String> keys) {
return keys
Expand Down
31 changes: 31 additions & 0 deletions lib/src/redis_multi_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:ioredis/ioredis.dart';

class RedisMulti {
final Redis _redis;

final List<List<String>> _commands = <List<String>>[];

RedisMulti(this._redis);

/// set value
RedisMulti set(String key, String value,
[String? option, dynamic optionValue]) {
_commands.add(_redis.getCommandToSetData(key, value, option, optionValue));
return this;
}

/// get value
RedisMulti get(String key) {
_commands.add(_redis.getCommandToGetData(key));
return this;
}

/// exec multi command
Future<List<dynamic>> exec() async {
await _redis.sendCommand(<String>['MULTI']);
for (List<String> command in _commands) {
await _redis.sendCommand(command);
}
return await _redis.sendCommand(<String>['EXEC']);
}
}
13 changes: 13 additions & 0 deletions test/ioredis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ void main() {
expect(data, 'မင်္ဂလာပါ');
});

test('multi', () async {
Redis redis = Redis();
List<dynamic> result = await redis
.multi()
.set('foo', 'bar')
.set('bar', 'foo')
.get('foo')
.get('bar')
.exec();

expect(<String>['OK', 'OK', 'bar', 'foo'], result);
});

test('custom socket', () async {
Redis redis = Redis();
redis.setSocket(await Socket.connect('127.0.0.1', 6379));
Expand Down

0 comments on commit 1859c8e

Please sign in to comment.