redis_async/lib.rs
1/*
2 * Copyright 2017-2022 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 * option. This file may not be copied, modified, or distributed
8 * except according to those terms.
9 */
10
11//! A client for Redis using Tokio and Futures.
12//!
13//! Three interfaces are provided: one low-level, that makes no assumptions about how Redis is used; a high-level client,
14//! suitable for the vast majority of use-cases; a PUBSUB client specifically for Redis's PUBSUB functionality.
15//!
16//! ## Low-level
17//!
18//! [`client::connect`](client/connect/fn.connect.html) returns a pair of `Sink` and `Stream` (see [futures](https://github.com/alexcrichton/futures-rs)) which
19//! both transport [`resp::RespValue`](resp/enum.RespValue.html)s between client and Redis, these work independently of one another
20//! to allow pipelining. It is the responsibility of the caller to match responses to requests. It is also the
21//! responsibility of the client to convert application data into instances of [`resp::RespValue`](resp/enum.RespValue.html) and
22//! back (there are conversion traits available for common examples).
23//!
24//! This is a very low-level API compared to most Redis clients, but is done so intentionally, for two reasons: 1) it is
25//! the common demoniator between a functional Redis client (i.e. is able to support all types of requests, including those
26//! that block and have streaming responses), and 2) it results in clean `Sink`s and `Stream`s which will be composable
27//! with other Tokio-based libraries.
28//!
29//! For most practical purposes this low-level interface will not be used, the only exception possibly being the
30//! [`MONITOR`](https://redis.io/commands/monitor) command.
31//!
32//! ## High-level
33//!
34//! [`client::paired_connect`](client/paired/fn.paired_connect.html) is used for most Redis commands (those for which one command
35//! returns one response, it's not suitable for PUBSUB, `MONITOR` or other similar commands). It allows a Redis command to
36//! be sent and a Future returned for each command.
37//!
38//! Commands will be sent in the order that [`send`](client/paired/struct.PairedConnection.html#method.send) is called, regardless
39//! of how the future is realised. This is to allow us to take advantage of Redis's features by implicitly pipelining
40//! commands where appropriate. One side-effect of this is that for many commands, e.g. `SET` we don't need to realise the
41//! future at all, it can be assumed to be fire-and-forget; but, the final future of the final command does need to be
42//! realised (at least) to ensure that the correct behaviour is observed.
43//!
44//! ## PUBSUB
45//!
46//! PUBSUB in Redis works differently. A connection will subscribe to one or more topics, then receive all messages that
47//! are published to that topic. As such the single-request/single-response model of
48//! [`paired_connect`](client/paired/fn.paired_connect.html) will not work. A specific
49//! [`client::pubsub_connect`](client/pubsub/fn.pubsub_connect.html) is provided for this purpose.
50//!
51//! It returns a future which resolves to a [`PubsubConnection`](client/pubsub/struct.PubsubConnection.html), this provides a
52//! [`subscribe`](client/pubsub/struct.PubsubConnection.html#method.subscribe) function that takes a topic as a parameter and
53//! returns a future which, once the subscription is confirmed, resolves to a stream that contains all messages published
54//! to that topic.
55
56#[macro_use]
57pub mod resp;
58
59#[macro_use]
60pub mod client;
61
62pub mod error;
63
64pub(crate) mod reconnect;
65
66// Ensure that exclusive features cannot be selected together.
67#[cfg(all(feature = "with-rustls", feature = "with-native-tls"))]
68compile_error!("Only one TLS backend can be selected at a time");