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
use crate::{
  commands,
  error::Error,
  interfaces::{ClientLike, FredResult},
  types::{FromValue, Key, Map, MultipleKeys, Value},
};
use fred_macros::rm_send_if;
use futures::Future;
use std::convert::TryInto;

#[cfg(feature = "i-hexpire")]
use crate::types::ExpireOptions;

/// Functions that implement the [hashes](https://redis.io/commands#hashes) interface.
#[rm_send_if(feature = "glommio")]
pub trait HashesInterface: ClientLike + Sized {
  /// Returns all fields and values of the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hgetall>
  fn hgetall<R, K>(&self, key: K) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
  {
    async move {
      into!(key);
      commands::hashes::hgetall(self, key).await?.convert()
    }
  }

  /// Removes the specified fields from the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hdel>
  fn hdel<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hdel(self, key, fields).await?.convert()
    }
  }

  /// Returns if `field` is an existing field in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hexists>
  fn hexists<R, K, F>(&self, key: K, field: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
  {
    async move {
      into!(key, field);
      commands::hashes::hexists(self, key, field).await?.convert()
    }
  }

  /// Returns the value associated with `field` in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hget>
  fn hget<R, K, F>(&self, key: K, field: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
  {
    async move {
      into!(key, field);
      commands::hashes::hget(self, key, field).await?.convert()
    }
  }

  /// Increments the number stored at `field` in the hash stored at `key` by `increment`.
  ///
  /// <https://redis.io/commands/hincrby>
  fn hincrby<R, K, F>(&self, key: K, field: F, increment: i64) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
  {
    async move {
      into!(key, field);
      commands::hashes::hincrby(self, key, field, increment).await?.convert()
    }
  }

  /// Increment the specified `field` of a hash stored at `key`, and representing a floating point number, by the
  /// specified `increment`.
  ///
  /// <https://redis.io/commands/hincrbyfloat>
  fn hincrbyfloat<R, K, F>(&self, key: K, field: F, increment: f64) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
  {
    async move {
      into!(key, field);
      commands::hashes::hincrbyfloat(self, key, field, increment)
        .await?
        .convert()
    }
  }

  /// Returns all field names in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hkeys>
  fn hkeys<R, K>(&self, key: K) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
  {
    async move {
      into!(key);
      commands::hashes::hkeys(self, key).await?.convert()
    }
  }

  /// Returns the number of fields contained in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hlen>
  fn hlen<R, K>(&self, key: K) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
  {
    async move {
      into!(key);
      commands::hashes::hlen(self, key).await?.convert()
    }
  }

  /// Returns the values associated with the specified `fields` in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hmget>
  fn hmget<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hmget(self, key, fields).await?.convert()
    }
  }

  /// Sets the specified fields to their respective values in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hmset>
  fn hmset<R, K, V>(&self, key: K, values: V) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    V: TryInto<Map> + Send,
    V::Error: Into<Error> + Send,
  {
    async move {
      into!(key);
      try_into!(values);
      commands::hashes::hmset(self, key, values).await?.convert()
    }
  }

  /// Sets fields in the hash stored at `key` to their provided values.
  ///
  /// <https://redis.io/commands/hset>
  fn hset<R, K, V>(&self, key: K, values: V) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    V: TryInto<Map> + Send,
    V::Error: Into<Error> + Send,
  {
    async move {
      into!(key);
      try_into!(values);
      commands::hashes::hset(self, key, values).await?.convert()
    }
  }

  /// Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
  ///
  /// <https://redis.io/commands/hsetnx>
  fn hsetnx<R, K, F, V>(&self, key: K, field: F, value: V) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
    V: TryInto<Value> + Send,
    V::Error: Into<Error> + Send,
  {
    async move {
      into!(key, field);
      try_into!(value);
      commands::hashes::hsetnx(self, key, field, value).await?.convert()
    }
  }

  /// When called with just the `key` argument, return a random field from the hash value stored at `key`.
  ///
  /// If the provided `count` argument is positive, return an array of distinct fields.
  ///
  /// <https://redis.io/commands/hrandfield>
  fn hrandfield<R, K>(&self, key: K, count: Option<(i64, bool)>) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
  {
    async move {
      into!(key);
      commands::hashes::hrandfield(self, key, count).await?.convert()
    }
  }

  /// Returns the string length of the value associated with `field` in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hstrlen>
  fn hstrlen<R, K, F>(&self, key: K, field: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<Key> + Send,
  {
    async move {
      into!(key, field);
      commands::hashes::hstrlen(self, key, field).await?.convert()
    }
  }

  /// Returns all values in the hash stored at `key`.
  ///
  /// <https://redis.io/commands/hvals>
  fn hvals<R, K>(&self, key: K) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
  {
    async move {
      into!(key);
      commands::hashes::hvals(self, key).await?.convert()
    }
  }

  /// Returns the remaining TTL (time to live) of a hash key's field(s) that have a set expiration.
  ///
  /// <https://redis.io/docs/latest/commands/httl/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn httl<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::httl(self, key, fields).await?.convert()
    }
  }

  /// Set an expiration (TTL or time to live) on one or more fields of a given hash key.
  ///
  /// <https://redis.io/docs/latest/commands/hexpire/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hexpire<R, K, F>(
    &self,
    key: K,
    seconds: i64,
    options: Option<ExpireOptions>,
    fields: F,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hexpire(self, key, seconds, options, fields)
        .await?
        .convert()
    }
  }

  /// HEXPIREAT has the same effect and semantics as HEXPIRE, but instead of specifying the number of seconds for the
  /// TTL (time to live), it takes an absolute Unix timestamp in seconds since Unix epoch.
  ///
  /// <https://redis.io/docs/latest/commands/hexpireat/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hexpire_at<R, K, F>(
    &self,
    key: K,
    time: i64,
    options: Option<ExpireOptions>,
    fields: F,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hexpire_at(self, key, time, options, fields)
        .await?
        .convert()
    }
  }

  /// Returns the absolute Unix timestamp in seconds since Unix epoch at which the given key's field(s) will expire.
  ///
  /// <https://redis.io/docs/latest/commands/hexpiretime/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hexpire_time<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hexpire_time(self, key, fields).await?.convert()
    }
  }

  /// Like HTTL, this command returns the remaining TTL (time to live) of a field that has an expiration set, but in
  /// milliseconds instead of seconds.
  ///
  /// <https://redis.io/docs/latest/commands/hpttl/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hpttl<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hpttl(self, key, fields).await?.convert()
    }
  }

  /// This command works like HEXPIRE, but the expiration of a field is specified in milliseconds instead of seconds.
  ///
  /// <https://redis.io/docs/latest/commands/hpexpire/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hpexpire<R, K, F>(
    &self,
    key: K,
    milliseconds: i64,
    options: Option<ExpireOptions>,
    fields: F,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hpexpire(self, key, milliseconds, options, fields)
        .await?
        .convert()
    }
  }

  /// HPEXPIREAT has the same effect and semantics as HEXPIREAT, but the Unix time at which the field will expire is
  /// specified in milliseconds since Unix epoch instead of seconds.
  ///
  /// <https://redis.io/docs/latest/commands/hpexpireat/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hpexpire_at<R, K, F>(
    &self,
    key: K,
    time: i64,
    options: Option<ExpireOptions>,
    fields: F,
  ) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hpexpire_at(self, key, time, options, fields)
        .await?
        .convert()
    }
  }

  /// HPEXPIRETIME has the same semantics as HEXPIRETIME, but returns the absolute Unix expiration timestamp in
  /// milliseconds since Unix epoch instead of seconds.
  ///
  /// <https://redis.io/docs/latest/commands/hpexpiretime/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hpexpire_time<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hpexpire_time(self, key, fields).await?.convert()
    }
  }

  /// Remove the existing expiration on a hash key's field(s), turning the field(s) from volatile (a field with
  /// expiration set) to persistent (a field that will never expire as no TTL (time to live) is associated).
  ///
  /// <https://redis.io/docs/latest/commands/hpersist/>
  #[cfg(feature = "i-hexpire")]
  #[cfg_attr(docsrs, doc(cfg(feature = "i-hexpire")))]
  fn hpersist<R, K, F>(&self, key: K, fields: F) -> impl Future<Output = FredResult<R>> + Send
  where
    R: FromValue,
    K: Into<Key> + Send,
    F: Into<MultipleKeys> + Send,
  {
    async move {
      into!(key, fields);
      commands::hashes::hpersist(self, key, fields).await?.convert()
    }
  }
}