feat(redis_client): add ttl to set (#1298)
This commit is contained in:
@@ -170,8 +170,19 @@ class RedisClient {
|
||||
/// Set the value of a key.
|
||||
/// Equivalent to the `SET` command.
|
||||
/// https://redis.io/commands/set
|
||||
Future<void> set({required String key, required String value}) {
|
||||
return execute(['SET', key, value]);
|
||||
///
|
||||
/// If [ttl] is provided, the key will expire after the specified duration.
|
||||
Future<void> set({
|
||||
required String key,
|
||||
required String value,
|
||||
Duration? ttl,
|
||||
}) {
|
||||
return execute([
|
||||
'SET',
|
||||
key,
|
||||
value,
|
||||
if (ttl != null) ...['EX', ttl.inSeconds],
|
||||
]);
|
||||
}
|
||||
|
||||
/// Gets the value of a key.
|
||||
|
||||
@@ -183,6 +183,8 @@ void main() {
|
||||
test('completes', () async {
|
||||
const key = 'key';
|
||||
const value = 'value';
|
||||
const ttl = Duration(seconds: 1);
|
||||
|
||||
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)));
|
||||
@@ -194,6 +196,13 @@ void main() {
|
||||
await expectLater(client.get(key: key), completion(equals(value)));
|
||||
await expectLater(client.unlink(key: key), completes);
|
||||
await expectLater(client.get(key: key), completion(isNull));
|
||||
await expectLater(
|
||||
client.set(key: key, value: value, ttl: ttl),
|
||||
completes,
|
||||
);
|
||||
await expectLater(client.get(key: key), completion(equals(value)));
|
||||
await Future<void>.delayed(ttl);
|
||||
await expectLater(client.get(key: key), completion(isNull));
|
||||
});
|
||||
|
||||
test(
|
||||
|
||||
Reference in New Issue
Block a user