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
use errors::*;
use redis::{self, Commands};
use std::error::Error;
use PersistentCache;
#[allow(unused_imports)]
use PREFIX;
pub struct RedisStorage {
con: redis::Connection,
}
impl RedisStorage {
pub fn new(host: &str) -> Result<Self> {
let client = redis::Client::open(host)?;
let con = client.get_connection()?;
Ok(RedisStorage { con })
}
}
impl PersistentCache for RedisStorage {
fn get(&mut self, name: &str) -> Result<Vec<u8>> {
match self.con.get(name) {
Ok(res) => Ok(res),
Err(e) => Err(e.into()),
}
}
fn set(&mut self, name: &str, val: &[u8]) -> Result<()> {
let r: Result<()> = self.con.set(name, val).map_err(|e| e.into());
r?;
Ok(())
}
fn flush(&mut self) -> Result<()> {
let iter: redis::Iter<String> = redis::cmd("KEYS")
.arg(format!("{}_*", PREFIX))
.iter(&self.con)?;
let cmd: &mut redis::Cmd = &mut redis::cmd("DEL");
let mut flushed_vars = 0;
for bla in iter {
flushed_vars += 1;
cmd.arg(bla);
}
if flushed_vars > 0 {
let r: Result<()> = cmd.query(&self.con).map_err(|e| e.into());
r?;
}
Ok(())
}
}