ntex_redis/lib.rs
1//! Redis client for ntex framework.
2//!
3//! # Example
4//!
5//! ```rust
6//! use ntex_redis::{cmd, RedisConnector};
7//! # use rand::{thread_rng, Rng};
8//! # use rand::distributions::Alphanumeric;
9//! # fn gen_random_key() -> String {
10//! # let key: String = thread_rng().sample_iter(&Alphanumeric).take(12).map(char::from).collect();
11//! # key
12//! # }
13//!
14//! #[ntex::main]
15//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! let redis = RedisConnector::new("127.0.0.1:6379").connect().await?;
17//! let key = gen_random_key();
18//!
19//! // create list with one value
20//! redis.exec(cmd::LPush(&key, "value")).await?;
21//!
22//! // get value by index
23//! let value = redis.exec(cmd::LIndex(&key, 0)).await?;
24//! assert_eq!(value.unwrap(), "value");
25//!
26//! // remove key
27//! redis.exec(cmd::Del(&key)).await?;
28//!
29//! Ok(())
30//! }
31//! ```
32#![allow(clippy::return_self_not_must_use)]
33
34mod client;
35pub mod cmd;
36pub mod codec;
37mod connector;
38pub mod errors;
39mod simple;
40
41pub use self::client::Client;
42pub use self::connector::RedisConnector;
43pub use self::simple::{SimpleClient, SubscriptionClient};
44
45/// Macro to create a request array, useful for preparing commands to send. Elements can be any type, or a mixture
46/// of types, that satisfy `Into<Request>`.
47///
48/// # Examples
49///
50/// ```rust
51/// #[macro_use]
52/// extern crate ntex_redis;
53///
54/// fn main() {
55/// let value = format!("something_{}", 123);
56/// array!["SET", "key_name", value];
57/// }
58/// ```
59///
60/// For variable length Redis commands:
61///
62/// ```rust
63/// #[macro_use]
64/// extern crate ntex_redis;
65///
66/// fn main() {
67/// let data = vec!["data", "from", "somewhere", "else"];
68/// let command = array!["RPUSH", "mykey"].extend(data);
69/// }
70/// ```
71#[macro_export]
72macro_rules! array {
73 ($($e:expr),*) => {{
74 $crate::codec::Request::Array(vec![$($e.into(),)*])
75 }}
76}
77
78#[cfg(test)]
79pub fn gen_random_key() -> String {
80 use rand::distributions::Alphanumeric;
81 use rand::{thread_rng, Rng};
82
83 let key: String = thread_rng()
84 .sample_iter(&Alphanumeric)
85 .take(12)
86 .map(char::from)
87 .collect();
88 key
89}