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
#[macro_use]
extern crate failure;
extern crate actix;
pub extern crate redis;
use actix::prelude::*;
use std::marker::PhantomData;
mod error;
pub use self::error::*;
pub type ActixRedisClientResult<T> = Result<T, ActixRedisClientError>;
pub struct Command<T> {
cmd: redis::Cmd,
_marker: PhantomData<T>,
}
impl<T> Command<T> {
pub fn new(cmd: redis::Cmd) -> Self {
Command {
cmd,
_marker: PhantomData::default(),
}
}
}
impl<T: 'static> Message for Command<T> {
type Result = ActixRedisClientResult<T>;
}
pub struct RedisExecutorSync(redis::Client);
impl RedisExecutorSync {
fn new(client: redis::Client) -> Self {
RedisExecutorSync(client)
}
pub fn start<F>(threads: usize, client_factory: F) -> Addr<Self>
where
F: Fn() -> redis::Client + Send + Sync + 'static,
{
SyncArbiter::start(threads, move || Self::new(client_factory()))
}
pub fn get_connection(&self) -> Result<redis::Connection, ActixRedisClientError> {
match self.0.get_connection() {
Ok(v) => Ok(v),
Err(e) => Err(e.into()),
}
}
pub fn get_pubsub(&self) -> Result<redis::PubSub, ActixRedisClientError> {
match self.0.get_pubsub() {
Ok(v) => Ok(v),
Err(e) => Err(e.into()),
}
}
}
impl Actor for RedisExecutorSync {
type Context = SyncContext<Self>;
}
impl<T: redis::FromRedisValue + 'static> Handler<Command<T>> for RedisExecutorSync {
type Result = ActixRedisClientResult<T>;
fn handle(&mut self, cmd: Command<T>, _: &mut Self::Context) -> Self::Result {
match cmd.cmd.query(&self.0) {
Ok(v) => Ok(v),
Err(e) => Err(e.into()),
}
}
}