-
Notifications
You must be signed in to change notification settings - Fork 63
/
string.rs
46 lines (38 loc) · 1.28 KB
/
string.rs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use redis_module::{
redis_module, Context, NextArg, RedisError, RedisResult, RedisString, RedisValue,
};
fn string_set(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
if args.len() < 3 {
return Err(RedisError::WrongArity);
}
let mut args = args.into_iter().skip(1);
let key_name = args.next_arg()?;
let value = args.next_arg()?;
let key = ctx.open_key_writable(&key_name);
let mut dma = key.as_string_dma()?;
dma.write(value.as_slice())
.map(|_| RedisValue::SimpleStringStatic("OK"))
}
fn string_get(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
if args.len() < 2 {
return Err(RedisError::WrongArity);
}
let mut args = args.into_iter().skip(1);
let key_name = args.next_arg()?;
let key = ctx.open_key(&key_name);
let res = key
.read()?
.map_or(RedisValue::Null, |v| RedisValue::StringBuffer(Vec::from(v)));
Ok(res)
}
//////////////////////////////////////////////////////
redis_module! {
name: "string",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [
["string.set", string_set, "write fast deny-oom", 1, 1, 1],
["string.get", string_get, "readonly", 1, 1, 1],
],
}