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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::time::{Duration, Instant};
use futures::prelude::*;
use redis::{aio::ConnectionLike, cmd, Cmd, ErrorKind, FromRedisValue, RedisFuture};
impl<T> RedisOps for T where T: ConnectionLike + Sized + Send + 'static {}
pub trait RedisOps: Sized + ConnectionLike + Send + 'static {
fn query<'a, T>(&'a mut self, cmd: &'a Cmd) -> RedisFuture<'a, T>
where
T: FromRedisValue + Send + 'static,
{
cmd.query_async(self).boxed()
}
fn execute<'a, T>(&'a mut self, cmd: &'a Cmd) -> RedisFuture<'a, ()>
where
T: FromRedisValue + Send + 'static,
{
cmd.query_async(self).boxed()
}
#[allow(clippy::needless_lifetimes)]
fn ping<'a>(&'a mut self) -> RedisFuture<'a, Duration> {
let started = Instant::now();
async move {
let response = Cmd::new()
.arg("PING")
.query_async::<_, String>(self)
.await?;
if response == "PONG" {
Ok(started.elapsed())
} else {
Err((ErrorKind::IoError, "ping failed").into())
}
}
.boxed()
}
fn flushall<'a>(&'a mut self) -> RedisFuture<'a, ()> {
async move { cmd("FLUSHALL").query_async(self).await }.boxed()
}
fn flushall_async<'a>(&'a mut self) -> RedisFuture<'a, ()> {
async move { cmd("FLUSHALL ASYNC").query_async(self).await }.boxed()
}
fn quit<'a>(&'a mut self) -> RedisFuture<'a, ()> {
async move {
let response = Cmd::new().query_async::<_, String>(self).await?;
if response == "OK" {
Ok(())
} else {
Err((ErrorKind::IoError, "quit failed").into())
}
}
.boxed()
}
fn client_id<'a>(&'a mut self) -> RedisFuture<i64> {
async move { cmd("CLIENT").arg("ID").query_async(self).await }.boxed()
}
fn client_kill_id<'a>(&'a mut self, id: i64) -> RedisFuture<()> {
async move {
cmd("CLIENT")
.arg("KILL")
.arg("ID")
.arg(id)
.query_async(self)
.await
}
.boxed()
}
fn db_size<'a>(&'a mut self) -> RedisFuture<i64> {
async move { cmd("DBSIZE").query_async(self).await }.boxed()
}
}