forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.rs
409 lines (347 loc) · 10 KB
/
keys.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use super::*;
use crate::error::*;
use crate::modules::inner::RedisClientInner;
use crate::protocol::types::*;
use crate::protocol::utils as protocol_utils;
use crate::types::*;
use crate::utils;
use std::convert::TryInto;
use std::sync::Arc;
value_cmd!(randomkey, Randomkey);
pub async fn get<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_values_cmd(inner, RedisCommandKind::Get, key.into().into()).await
}
pub async fn set(
inner: &Arc<RedisClientInner>,
key: RedisKey,
value: RedisValue,
expire: Option<Expiration>,
options: Option<SetOptions>,
get: bool,
) -> Result<RedisValue, RedisError> {
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(6);
args.push(key.into());
args.push(value);
if let Some(expire) = expire {
let (k, v) = expire.into_args();
args.push(k.into());
if let Some(v) = v {
args.push(v.into());
}
}
if let Some(options) = options {
args.push(options.to_str().into());
}
if get {
args.push(static_val!(GET));
}
Ok((RedisCommandKind::Set, args))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn del<K>(inner: &Arc<RedisClientInner>, keys: K) -> Result<RedisValue, RedisError>
where
K: Into<MultipleKeys>,
{
let keys = keys.into();
utils::check_empty_keys(&keys)?;
let args: Vec<RedisValue> = keys.inner().drain(..).map(|k| k.into()).collect();
let frame = utils::request_response(inner, move || Ok((RedisCommandKind::Del, args))).await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn incr<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Incr, key.into().into()).await
}
pub async fn decr<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Decr, key.into().into()).await
}
pub async fn incr_by<K>(inner: &Arc<RedisClientInner>, key: K, val: i64) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::IncrBy, vec![key.into(), val.into()]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn decr_by<K>(inner: &Arc<RedisClientInner>, key: K, val: i64) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::DecrBy, vec![key.into(), val.into()]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn incr_by_float<K>(inner: &Arc<RedisClientInner>, key: K, val: f64) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let (key, val) = (key.into(), val.try_into()?);
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::IncrByFloat, vec![key.into(), val]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn ttl<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Ttl, key.into().into()).await
}
pub async fn pttl<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Pttl, key.into().into()).await
}
pub async fn persist<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Persist, key.into().into()).await
}
pub async fn expire<K>(inner: &Arc<RedisClientInner>, key: K, seconds: i64) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::Expire, vec![key.into(), seconds.into()]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn expire_at<K>(inner: &Arc<RedisClientInner>, key: K, timestamp: i64) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::ExpireAt, vec![key.into(), timestamp.into()]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn exists<K>(inner: &Arc<RedisClientInner>, keys: K) -> Result<RedisValue, RedisError>
where
K: Into<MultipleKeys>,
{
let keys = keys.into();
utils::check_empty_keys(&keys)?;
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(keys.len());
for key in keys.inner().into_iter() {
args.push(key.into());
}
Ok((RedisCommandKind::Exists, args))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn dump<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_values_cmd(inner, RedisCommandKind::Dump, key.into().into()).await
}
pub async fn restore<K>(
inner: &Arc<RedisClientInner>,
key: K,
ttl: i64,
serialized: RedisValue,
replace: bool,
absttl: bool,
idletime: Option<i64>,
frequency: Option<i64>,
) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(9);
args.push(key.into());
args.push(ttl.into());
args.push(serialized);
if replace {
args.push(static_val!(REPLACE));
}
if absttl {
args.push(static_val!(ABSTTL));
}
if let Some(idletime) = idletime {
args.push(static_val!(IDLE_TIME));
args.push(idletime.into());
}
if let Some(frequency) = frequency {
args.push(static_val!(FREQ));
args.push(frequency.into());
}
Ok((RedisCommandKind::Restore, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn getrange<K>(
inner: &Arc<RedisClientInner>,
key: K,
start: usize,
end: usize,
) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((
RedisCommandKind::GetRange,
vec![key.into(), start.try_into()?, end.try_into()?],
))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn setrange<K>(
inner: &Arc<RedisClientInner>,
key: K,
offset: u32,
value: RedisValue,
) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
let key = key.into();
let frame = utils::request_response(inner, move || {
Ok((RedisCommandKind::Setrange, vec![key.into(), offset.into(), value]))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn getset<K>(inner: &Arc<RedisClientInner>, key: K, value: RedisValue) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
args_values_cmd(inner, RedisCommandKind::GetSet, vec![key.into().into(), value]).await
}
pub async fn getdel<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_values_cmd(inner, RedisCommandKind::GetDel, key.into().into()).await
}
pub async fn strlen<K>(inner: &Arc<RedisClientInner>, key: K) -> Result<RedisValue, RedisError>
where
K: Into<RedisKey>,
{
one_arg_value_cmd(inner, RedisCommandKind::Strlen, key.into().into()).await
}
pub async fn mget(inner: &Arc<RedisClientInner>, keys: MultipleKeys) -> Result<RedisValue, RedisError> {
let keys = keys.into();
utils::check_empty_keys(&keys)?;
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(keys.len());
for key in keys.inner().into_iter() {
args.push(key.into());
}
Ok((RedisCommandKind::Mget, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn mset<V>(inner: &Arc<RedisClientInner>, values: V) -> Result<RedisValue, RedisError>
where
V: Into<RedisMap>,
{
let values = values.into();
if values.len() == 0 {
return Err(RedisError::new(
RedisErrorKind::InvalidArgument,
"Values cannot be empty.",
));
}
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(values.len() * 2);
for (key, value) in values.inner().into_iter() {
args.push(key.into());
args.push(value);
}
Ok((RedisCommandKind::Mset, args))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn msetnx<V>(inner: &Arc<RedisClientInner>, values: V) -> Result<RedisValue, RedisError>
where
V: Into<RedisMap>,
{
let values = values.into();
if values.len() == 0 {
return Err(RedisError::new(
RedisErrorKind::InvalidArgument,
"Values cannot be empty.",
));
}
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(values.len() * 2);
for (key, value) in values.inner().into_iter() {
args.push(key.into());
args.push(value);
}
Ok((RedisCommandKind::Msetnx, args))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn copy<S, D>(
inner: &Arc<RedisClientInner>,
source: S,
destination: D,
db: Option<u8>,
replace: bool,
) -> Result<RedisValue, RedisError>
where
S: Into<RedisKey>,
D: Into<RedisKey>,
{
let (source, destination) = (source.into(), destination.into());
let frame = utils::request_response(inner, move || {
let mut args = Vec::with_capacity(5);
args.push(source.into());
args.push(destination.into());
if let Some(db) = db {
args.push(static_val!(DB));
args.push(db.into());
}
if replace {
args.push(static_val!(REPLACE));
}
Ok((RedisCommandKind::Copy, args))
})
.await?;
protocol_utils::frame_to_single_result(frame)
}
pub async fn watch<K>(inner: &Arc<RedisClientInner>, keys: K) -> Result<(), RedisError>
where
K: Into<MultipleKeys>,
{
let args = keys.into().inner().into_iter().map(|k| k.into()).collect();
args_ok_cmd(inner, RedisCommandKind::Watch, args).await
}
ok_cmd!(unwatch, Unwatch);