fred 10.1.0

An async client for Redis and Valkey.
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use crate::{
  commands,
  interfaces::{ClientLike, FredResult},
  types::{FromValue, Key, MultipleKeys, MultipleStrings, SetOptions},
};
use bytes_utils::Str;
use fred_macros::rm_send_if;
use futures::Future;
use serde_json::Value;

/// The client commands in the [RedisJSON](https://redis.io/docs/data-types/json/) interface.
///
/// ## String Values
///
/// This interface uses [serde_json::Value](serde_json::Value) as the baseline type and will convert non-string values
/// to RESP bulk strings via [to_string](serde_json::to_string).
///
/// Some of the RedisJSON commands include the following notice in the documentation:
///
/// > To specify a string as an array value to append, wrap the quoted string with an additional set of single quotes.
/// > Example: '"silver"'.
///
/// The [serde_json::to_string](serde_json::to_string) functions are often the easiest way to do
/// this. The [json_quote](crate::json_quote) macro can also help.
///
/// For example:
///
/// ```rust
/// use fred::{json_quote, prelude::*};
/// use serde_json::json;
/// async fn example(client: &Client) -> Result<(), Error> {
///   let _: () = client.json_set("foo", "$", json!(["a", "b"]), None).await?;
///
///   // need to double quote string values in this context
///   let size: i64 = client
///     .json_arrappend("foo", Some("$"), vec![
///       json!("c").to_string(),
///       // or
///       serde_json::to_string(&json!("d"))?,
///       // or
///       json_quote!("e"),
///     ])
///     .await?;
///   assert_eq!(size, 5);
///   Ok(())
/// }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
#[rm_send_if(feature = "glommio")]
pub trait RedisJsonInterface: ClientLike + Sized {
  /// Append the json values into the array at path after the last element in it.
  ///
  /// <https://redis.io/commands/json.arrappend>
  fn json_arrappend<R, K, P, V>(&self, key: K, path: P, values: Vec<V>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path);
      let values = values.into_iter().map(|v| v.into()).collect();
      commands::redis_json::json_arrappend(self, key, path, values)
        .await?
        .convert()
    }
  }

  /// Search for the first occurrence of a JSON value in an array.
  ///
  /// <https://redis.io/commands/json.arrindex/>
  fn json_arrindex<R, K, P, V>(
    &self,
    key: K,
    path: P,
    value: V,
    start: Option<i64>,
    stop: Option<i64>,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path, value);
      commands::redis_json::json_arrindex(self, key, path, value, start, stop)
        .await?
        .convert()
    }
  }

  /// Insert the json values into the array at path before the index (shifts to the right).
  ///
  /// <https://redis.io/commands/json.arrinsert/>
  fn json_arrinsert<R, K, P, V>(
    &self,
    key: K,
    path: P,
    index: i64,
    values: Vec<V>,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path);
      let values = values.into_iter().map(|v| v.into()).collect();
      commands::redis_json::json_arrinsert(self, key, path, index, values)
        .await?
        .convert()
    }
  }

  /// Report the length of the JSON array at path in key.
  ///
  /// <https://redis.io/commands/json.arrlen/>
  fn json_arrlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_arrlen(self, key, path).await?.convert()
    }
  }

  /// Remove and return an element from the index in the array
  ///
  /// <https://redis.io/commands/json.arrpop/>
  fn json_arrpop<R, K, P>(
    &self,
    key: K,
    path: Option<P>,
    index: Option<i64>,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_arrpop(self, key, path, index)
        .await?
        .convert()
    }
  }

  /// Trim an array so that it contains only the specified inclusive range of elements
  ///
  /// <https://redis.io/commands/json.arrtrim/>
  fn json_arrtrim<R, K, P>(
    &self,
    key: K,
    path: P,
    start: i64,
    stop: i64,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key, path);
      commands::redis_json::json_arrtrim(self, key, path, start, stop)
        .await?
        .convert()
    }
  }

  /// Clear container values (arrays/objects) and set numeric values to 0
  ///
  /// <https://redis.io/commands/json.clear/>
  fn json_clear<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_clear(self, key, path).await?.convert()
    }
  }

  /// Report a value's memory usage in bytes
  ///
  /// <https://redis.io/commands/json.debug-memory/>
  fn json_debug_memory<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_debug_memory(self, key, path)
        .await?
        .convert()
    }
  }

  /// Delete a value.
  ///
  /// <https://redis.io/commands/json.del/>
  fn json_del<R, K, P>(&self, key: K, path: P) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key, path);
      commands::redis_json::json_del(self, key, path).await?.convert()
    }
  }

  /// Return the value at path in JSON serialized form.
  ///
  /// <https://redis.io/commands/json.get/>
  fn json_get<R, K, I, N, S, P>(
    &self,
    key: K,
    indent: Option<I>,
    newline: Option<N>,
    space: Option<S>,
    paths: P,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    I: Into<Str> + Send,
    N: Into<Str> + Send,
    S: Into<Str> + Send,
    P: Into<MultipleStrings> + Send,
  {
    async move {
      into!(key, paths);
      let indent = indent.map(|v| v.into());
      let newline = newline.map(|v| v.into());
      let space = space.map(|v| v.into());
      commands::redis_json::json_get(self, key, indent, newline, space, paths)
        .await?
        .convert()
    }
  }

  /// Merge a given JSON value into matching paths.
  ///
  /// <https://redis.io/commands/json.merge/>
  fn json_merge<R, K, P, V>(&self, key: K, path: P, value: V) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path, value);
      commands::redis_json::json_merge(self, key, path, value)
        .await?
        .convert()
    }
  }

  /// Return the values at path from multiple key arguments.
  ///
  /// <https://redis.io/commands/json.mget/>
  fn json_mget<R, K, P>(&self, keys: K, path: P) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<MultipleKeys> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(keys, path);
      commands::redis_json::json_mget(self, keys, path).await?.convert()
    }
  }

  /// Set or update one or more JSON values according to the specified key-path-value triplets.
  ///
  /// <https://redis.io/commands/json.mset/>
  fn json_mset<R, K, P, V>(&self, values: Vec<(K, P, V)>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      let values = values
        .into_iter()
        .map(|(k, p, v)| (k.into(), p.into(), v.into()))
        .collect();
      commands::redis_json::json_mset(self, values).await?.convert()
    }
  }

  /// Increment the number value stored at path by number
  ///
  /// <https://redis.io/commands/json.numincrby/>
  fn json_numincrby<R, K, P, V>(&self, key: K, path: P, value: V) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path, value);
      commands::redis_json::json_numincrby(self, key, path, value)
        .await?
        .convert()
    }
  }

  /// Return the keys in the object that's referenced by path.
  ///
  /// <https://redis.io/commands/json.objkeys/>
  fn json_objkeys<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_objkeys(self, key, path).await?.convert()
    }
  }

  /// Report the number of keys in the JSON object at path in key.
  ///
  /// <https://redis.io/commands/json.objlen/>
  fn json_objlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_objlen(self, key, path).await?.convert()
    }
  }

  /// Return the JSON in key in Redis serialization protocol specification form.
  ///
  /// <https://redis.io/commands/json.resp/>
  fn json_resp<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_resp(self, key, path).await?.convert()
    }
  }

  /// Set the JSON value at path in key.
  ///
  /// <https://redis.io/commands/json.set/>
  fn json_set<R, K, P, V>(
    &self,
    key: K,
    path: P,
    value: V,
    options: Option<SetOptions>,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, path, value);
      commands::redis_json::json_set(self, key, path, value, options)
        .await?
        .convert()
    }
  }

  /// Append the json-string values to the string at path.
  ///
  /// <https://redis.io/commands/json.strappend/>
  fn json_strappend<R, K, P, V>(
    &self,
    key: K,
    path: Option<P>,
    value: V,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
    V: Into<Value> + Send,
  {
    async move {
      into!(key, value);
      let path = path.map(|p| p.into());
      commands::redis_json::json_strappend(self, key, path, value)
        .await?
        .convert()
    }
  }

  /// Report the length of the JSON String at path in key.
  ///
  /// <https://redis.io/commands/json.strlen/>
  fn json_strlen<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_strlen(self, key, path).await?.convert()
    }
  }

  /// Toggle a Boolean value stored at path.
  ///
  /// <https://redis.io/commands/json.toggle/>
  fn json_toggle<R, K, P>(&self, key: K, path: P) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key, path);
      commands::redis_json::json_toggle(self, key, path).await?.convert()
    }
  }

  /// Report the type of JSON value at path.
  ///
  /// <https://redis.io/commands/json.type/>
  fn json_type<R, K, P>(&self, key: K, path: Option<P>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    P: Into<Str> + Send,
  {
    async move {
      into!(key);
      let path = path.map(|p| p.into());
      commands::redis_json::json_type(self, key, path).await?.convert()
    }
  }
}