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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures::compat::Future01CompatExt;
use redis::{aio::Connection as RedisConnection, FromRedisValue, RedisResult, ToRedisArgs};
use crate::Connection;
pub struct Cmd {
pub(crate) cmd: redis::Cmd,
}
impl Cmd {
pub fn new() -> Cmd {
Cmd {
cmd: redis::Cmd::new(),
}
}
pub fn arg<T: ToRedisArgs>(&mut self, arg: T) -> &mut Cmd {
self.cmd.arg(arg);
self
}
pub fn cursor_arg(&mut self, cursor: u64) -> &mut Cmd {
self.cmd.cursor_arg(cursor);
self
}
pub fn get_packed_command(&self) -> Vec<u8> {
self.cmd.get_packed_command()
}
pub fn in_scan_mode(&self) -> bool {
self.cmd.in_scan_mode()
}
pub async fn query<T: FromRedisValue + Send>(&self, conn: &mut Connection) -> RedisResult<T> {
let rconn = conn._take_conn()?;
let (rconn, result) = self.cmd.query_async(rconn).compat().await?;
conn._replace_conn(rconn);
Ok(FromRedisValue::from_redis_value(&result)?)
}
pub async fn execute(&self, conn: &mut Connection) -> RedisResult<()> {
let rconn = conn._take_conn()?;
let (rconn, _) = self
.cmd
.query_async::<RedisConnection, ()>(rconn)
.compat()
.await?;
conn._replace_conn(rconn);
Ok(())
}
}
impl From<redis::Cmd> for Cmd {
fn from(cmd: redis::Cmd) -> Self {
Cmd { cmd }
}
}
impl Into<redis::Cmd> for Cmd {
fn into(self) -> redis::Cmd {
self.cmd
}
}
pub fn cmd(name: &str) -> Cmd {
let mut cmd = Cmd::new();
cmd.arg(name);
cmd
}