redis_universal_client/
lib.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use redis::{cluster::ClusterClient, Client, ErrorKind, RedisError, RedisResult};

/// UniversalClient which is either `redis::Client` or `redis::cluster::ClusterClient`.
#[derive(Clone)]
pub enum UniversalClient {
    Client(Client),
    Cluster(ClusterClient),
}

impl UniversalClient {
    pub fn get_connection(&self) -> redis::RedisResult<UniversalConnection> {
        match self {
            Self::Client(cli) => cli.get_connection().map(UniversalConnection::Client),
            Self::Cluster(cli) => cli.get_connection().map(UniversalConnection::Cluster),
        }
    }

    /// Get Connection when single addr is specified. Get ClusterConnection when multiple addrs are
    /// specified. If you want Cluster with single address, use `UniversalBuilder` to construct
    /// `UniversalClient`.
    pub fn open<T: redis::IntoConnectionInfo + Clone>(
        addrs: Vec<T>,
    ) -> RedisResult<UniversalClient> {
        let mut addrs = addrs;

        if addrs.is_empty() {
            return Err(RedisError::from((
                ErrorKind::InvalidClientConfig,
                "No address specified",
            )));
        }

        if addrs.len() == 1 {
            Client::open(addrs.remove(0)).map(Self::Client)
        } else {
            ClusterClient::open(addrs).map(Self::Cluster)
        }
    }
}

pub struct UniversalBuilder<T> {
    addrs: Vec<T>,
    cluster: bool,
}

impl<T> UniversalBuilder<T> {
    pub fn new(addrs: Vec<T>) -> UniversalBuilder<T> {
        UniversalBuilder {
            addrs,
            cluster: false,
        }
    }

    pub fn cluster(mut self, flag: bool) -> UniversalBuilder<T> {
        self.cluster = flag;
        self
    }

    pub fn build(self) -> RedisResult<UniversalClient>
    where
        T: redis::IntoConnectionInfo + Clone,
    {
        let UniversalBuilder { mut addrs, cluster } = self;

        if addrs.is_empty() {
            return Err(RedisError::from((
                ErrorKind::InvalidClientConfig,
                "No address specified",
            )));
        }

        if cluster {
            ClusterClient::open(addrs).map(UniversalClient::Cluster)
        } else {
            Client::open(addrs.remove(0)).map(UniversalClient::Client)
        }
    }
}

pub enum UniversalConnection {
    Client(redis::Connection),
    Cluster(redis::cluster::ClusterConnection),
}

impl redis::ConnectionLike for UniversalConnection {
    fn req_packed_command(&mut self, cmd: &[u8]) -> RedisResult<redis::Value> {
        match self {
            Self::Client(conn) => conn.req_packed_command(cmd),
            Self::Cluster(conn) => conn.req_packed_command(cmd),
        }
    }

    fn req_packed_commands(
        &mut self,
        cmd: &[u8],
        offset: usize,
        count: usize,
    ) -> RedisResult<Vec<redis::Value>> {
        match self {
            Self::Client(conn) => conn.req_packed_commands(cmd, offset, count),
            Self::Cluster(conn) => conn.req_packed_commands(cmd, offset, count),
        }
    }

    fn get_db(&self) -> i64 {
        match self {
            Self::Client(conn) => conn.get_db(),
            Self::Cluster(conn) => conn.get_db(),
        }
    }

    fn is_open(&self) -> bool {
        match self {
            Self::Client(conn) => conn.is_open(),
            Self::Cluster(conn) => conn.is_open(),
        }
    }

    fn check_connection(&mut self) -> bool {
        match self {
            Self::Client(conn) => conn.check_connection(),
            Self::Cluster(conn) => conn.check_connection(),
        }
    }
}