feat(redis_client): add ttl to set (#1298)

This commit is contained in:
Felix Angelov
2023-09-20 17:01:07 -05:00
committed by GitHub
parent 771d5ac99d
commit 2e8aa53bc5
2 changed files with 22 additions and 2 deletions
@@ -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(