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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#[cfg(feature = "cluster")]
use redis::cluster::ClusterClient;
use redis::streams::StreamReadOptions;
use redis::{from_redis_value, ToRedisArgs, Value, AsyncCommands};
use std::fmt::{Debug, Formatter};
use redis::aio::ConnectionManager;

#[derive(Clone)]
pub struct RedisClient {
    #[cfg(feature = "cluster")]
    pub client: ClusterClient,
    #[cfg(feature = "single")]
    pub client: ConnectionManager,
}

impl Debug for RedisClient {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(""))
    }
}

impl RedisClient {
    #[tracing::instrument]
    pub async fn get<K: ToRedisArgs + Debug + Send + Sync>(&mut self, key: K) -> redis::RedisResult<String> {
        #[cfg(feature = "cluster")]
        let data = self.client.get_connection()?.get(key)?;

        #[cfg(feature = "single")]
        let data = self.client.get(key).await?;
        Ok(data)
    }

    #[tracing::instrument]
    pub async fn exists<K: ToRedisArgs + Debug + Send + Sync>(&mut self, key: K) -> redis::RedisResult<u8> {
        #[cfg(feature = "cluster")]
        let data = self.client.get_connection()?.exists(key)?;

        #[cfg(feature = "single")]
        let data = self.client.exists(key).await?;
        Ok(data)
    }

    #[tracing::instrument]
    pub async fn set<K: ToRedisArgs + Debug + Send + Sync, V: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        value: V,
    ) -> redis::RedisResult<bool> {
        #[cfg(feature = "cluster")]
        let ok = self.client.get_connection()?.set(key, value)?;

        #[cfg(feature = "single")]
        let ok =self.client.set(key, value).await?;
        Ok(ok)
    }

    #[tracing::instrument]
    pub async fn expire<K: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        expire: usize,
    ) -> redis::RedisResult<i8> {
        #[cfg(feature = "cluster")]
        let ok = self.client.get_connection()?.expire(key, expire)?;

        #[cfg(feature = "single")]
        let ok =self.client.expire(key, expire).await?;
        Ok(ok)
    }

    #[tracing::instrument]
    pub async fn del<K: ToRedisArgs + Debug + Send + Sync>(&mut self, key: K) -> redis::RedisResult<u8> {
        #[cfg(feature = "cluster")]
        let ok = self.client.get_connection()?.del(key)?;

        #[cfg(feature = "single")]
        let ok =self.client.del(key).await?;
        Ok(ok)
    }

    #[tracing::instrument]
    pub async fn xadd<K: ToRedisArgs + Debug + Send + Sync, F: ToRedisArgs + Debug + Send + Sync, V: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        items: &[(F, V)],
    ) -> redis::RedisResult<String> {
        #[cfg(feature = "cluster")]
        let ok = self.client.get_connection()?.xadd(key, "*", items)?;

        #[cfg(feature = "single")]
        let ok =self.client.xadd(key, "*", items).await?;
        Ok(ok)
    }

    /// return OK
    #[tracing::instrument]
    pub async fn xgroup_create<K: ToRedisArgs + Debug + Send + Sync, G: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        group: G,
    ) -> redis::RedisResult<()> {
        let ok = self.is_exist_group_name(&key, &group).await?;

        if !ok {
            #[cfg(feature = "cluster")]
            self
                .client
                .get_connection()?
                .xgroup_create_mkstream(key, group, 0)?;

            #[cfg(feature = "single")]
            self.client.xgroup_create_mkstream(key, group, 0).await?;


        }
        Ok(())
    }

    /// return OK
    #[tracing::instrument]
    pub async fn xread_group(
        &mut self,
        key: &str,
        group: &str,
        consumer: &str,
    ) -> redis::RedisResult<Value> {
        let keys = vec![key];
        let ids = vec![">"];
        let mut opts = StreamReadOptions::default();
        opts = opts.count(1);
        opts = opts.group(group, consumer);
        opts = opts.block(3000);
        // opts = opts.noack();
        #[cfg(feature = "cluster")]
        let data = self
            .client
            .get_connection()?
            .xread_options(&keys, &ids, &opts)?;

        #[cfg(feature = "single")]
        let data = self.client.xread_options(&keys, &ids, &opts).await?;
        Ok(data)
    }

    #[tracing::instrument]
    pub async fn xinfo_groups<K: ToRedisArgs + Debug + Send + Sync>(&mut self, key: K) -> redis::RedisResult<Value> {
        #[cfg(feature = "cluster")]
        let v = self.client.get_connection()?.xinfo_groups(key)?;

        #[cfg(feature = "single")]
        let v = self.client.xinfo_groups(key).await?;
        Ok(v)
    }

    #[tracing::instrument]
    pub async fn is_exist_group_name<K: ToRedisArgs + Debug + Send + Sync, G: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        group: G,
    ) -> redis::RedisResult<bool> {
        let groups = self.xinfo_groups(&key).await?;
        let groups: Vec<Vec<Value>> = from_redis_value(&groups)?;
        let groupname = group.to_redis_args().get(0).unwrap().to_owned();
        let groupname = String::from_utf8(groupname).unwrap();
        for group in groups {
            let name = group.get(1).unwrap();
            let name: String = from_redis_value(name).unwrap();
            if name == groupname {
                return Ok(true);
            }
        }
        Ok(false)
    }

    #[tracing::instrument]
    pub async fn xinfo_consumers<K: ToRedisArgs + Debug + Send + Sync, G: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        group: G,
    ) -> redis::RedisResult<Value> {
        #[cfg(feature = "cluster")]
        let v = self.client.get_connection()?.xinfo_consumers(key, group)?;

        #[cfg(feature = "single")]
        let v = self.client.xinfo_consumers(key, group).await?;
        Ok(v)
    }

    #[tracing::instrument]
    pub async fn xclaim_auto<
        K: ToRedisArgs + Debug + Send + Sync,
        G: ToRedisArgs + Debug + Send + Sync,
        C: ToRedisArgs + Debug + Send + Sync,
        MIT: ToRedisArgs + Debug + Send + Sync,
    >(
        &mut self,
        key: K,
        group: G,
        consumer: C,
        min_idle_time: MIT,
    ) -> redis::RedisResult<Value> {
        let data = self.xpending_one(&key, &group).await?;
        let id = self.get_id(&data).await?;
        let ids = vec![id];
        let mut opts = redis::streams::StreamClaimOptions::default();
        opts = opts.time(60000);
        opts = opts.idle(60000);

        #[cfg(feature = "cluster")]
        let v = self.client.get_connection()?.xclaim_options(
            key,
            group,
            consumer,
            min_idle_time,
            &ids,
            opts,
        )?;

        #[cfg(feature = "single")]
        let v = self.client.xclaim_options(
            key,
            group,
            consumer,
            min_idle_time,
            &ids,
            opts,
        ).await?;
        Ok(v)
    }

    #[tracing::instrument]
    pub async fn xpending_one<K: ToRedisArgs + Debug + Send + Sync, G: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        group: G,
    ) -> redis::RedisResult<Value> {
        #[cfg(feature = "cluster")]
        let d: Value = self
            .client
            .get_connection()?
            .xpending_count(key, group, "-", "+", 1)?;

        #[cfg(feature = "single")]
        let d: Value = self
            .client
            .xpending_count(key, group, "-", "+", 1).await?;
        Ok(d)
    }

    #[tracing::instrument]
    pub async fn get_id(&self, data: &Value) -> redis::RedisResult<String> {
        let data: Vec<Vec<Value>> = from_redis_value(&data)?;
        if data.len() > 0 {
            let data = data.get(0).unwrap();
            if data.len() > 0 {
                let data = data.get(0).unwrap();
                let data = from_redis_value(data)?;
                return Ok(data);
            }
        }
        Ok(String::new())
    }

    #[tracing::instrument]
    pub async fn xack<K: ToRedisArgs + Debug + Send + Sync, G: ToRedisArgs + Debug + Send + Sync, I: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        group: G,
        ids: &Vec<I>,
    ) -> redis::RedisResult<u64> {
        #[cfg(feature = "cluster")]
        let ack_count = self.client.get_connection()?.xack(key, group, ids)?;

        #[cfg(feature = "single")]
        let ack_count = self.client.xack(key, group, ids).await?;
        Ok(ack_count)
    }

    #[tracing::instrument]
    pub async fn xack2del<
        K: ToRedisArgs + Debug + Send + Sync,
        G: ToRedisArgs + Debug + Send + Sync,
        I: ToRedisArgs + Debug + Send + Sync,
    >(
        &mut self,
        key: K,
        group: G,
        ids: &Vec<I>,
    ) -> redis::RedisResult<(u64, u64)> {
        #[cfg(feature = "cluster")]
        let ack_count = self.client.get_connection()?.xack(&key, group, ids)?;

        #[cfg(feature = "single")]
        let ack_count = self.client.xack(&key, group, ids).await?;
        let del_count = self.xdel(key, ids).await?;
        Ok((ack_count, del_count))
    }

    ///
    #[tracing::instrument]
    pub async fn xdel<K: ToRedisArgs + Debug + Send + Sync, I: ToRedisArgs + Debug + Send + Sync>(
        &mut self,
        key: K,
        ids: &Vec<I>,
    ) -> redis::RedisResult<u64> {
        #[cfg(feature = "cluster")]
        let del_count = self.client.get_connection()?.xdel(key, ids)?;

        #[cfg(feature = "single")]
        let del_count = self.client.xdel(key, ids).await?;
        Ok(del_count)
    }

    #[cfg(feature = "cluster")]
    pub async fn ping(&mut self) -> redis::RedisResult<bool> {
        let ok = self.client.get_connection()?.check_connection();
        Ok(ok)
    }
}