-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathexpire.rs
More file actions
31 lines (27 loc) · 868 Bytes
/
expire.rs
File metadata and controls
31 lines (27 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::time::Duration;
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::{valkey_module, Context, NextArg, ValkeyError, ValkeyResult, ValkeyString};
fn expire_cmd(ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult {
if args.len() < 3 {
return Err(ValkeyError::WrongArity);
}
let mut args = args.into_iter().skip(1);
let key_name = args.next_arg()?;
let ttl_sec = args.next_i64()?;
let key = ctx.open_key_writable(&key_name);
if ttl_sec >= 0 {
key.set_expire(Duration::new(ttl_sec as u64, 0))
} else {
key.remove_expire()
}
}
//////////////////////////////////////////////////////
valkey_module! {
name: "expire",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["expire.cmd", expire_cmd, "write fast deny-oom", 1, 1, 1],
],
}