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
#![deny(anonymous_parameters, const_err, dead_code, deprecated, deprecated_attr, exceeding_bitshifts, extra_requirement_in_impl, fat_ptr_transmutes, illegal_floating_point_literal_pattern, improper_ctypes, invalid_type_param_default, legacy_constructor_visibility, legacy_directory_ownership, legacy_imports, missing_copy_implementations, missing_docs, missing_fragment_specifier, mutable_transmutes, no_mangle_const_items, no_mangle_generic_items, non_camel_case_types, non_shorthand_field_patterns, non_snake_case, non_upper_case_globals, overflowing_literals, parenthesized_params_in_types_and_modules, path_statements, patterns_in_fns_without_body, plugin_as_library, private_in_public, private_no_mangle_fns, private_no_mangle_statics, renamed_and_removed_lints, resolve_trait_on_defaulted_unit, safe_extern_statics, stable_features, trivial_numeric_casts, unconditional_recursion, unions_with_drop_fields, unknown_crate_types, unknown_lints, unreachable_code, unreachable_patterns, unsafe_code, unstable_features, unused_allocation, unused_assignments, unused_attributes, unused_comparisons, unused_extern_crates, unused_features, unused_import_braces, unused_imports, unused_macros, unused_must_use, unused_mut, unused_parens, unused_qualifications, unused_unsafe, unused_variables, while_true)] #![warn(unknown_lints)] #![allow(box_pointers, missing_debug_implementations, trivial_casts, unused_results, variant_size_differences, warnings)] #![cfg_attr(feature="clippy", feature(plugin))] //! # simple_redis //! //! Simple [redis](https://redis.io/) client based on [redis-rs](https://crates.io/crates/redis) with internal //! connection and subscription handling. //! //! This library provides a very basic, simple API for the most common redis operations.<br> //! While not as comprehensive or flexiable as [redis-rs](https://crates.io/crates/redis), //! it does provide a simpler api for most common use cases and operations as well as automatic internal connection //! and subscription (pubsub) handling.<br> //! In addition, the entire API is accessible via redis client and there is no need to manage connection or pubsub //! instances in parallel.<br> //! <br> //! Connection resiliency is managed by verifying the connection before every operation against the redis server.<br> //! In case of any connection issue, a new connection will be allocated to ensure the operation is invoked on a valid //! connection only.<br> //! However, this comes at a small performance cost of PING operation to the redis server.<br> //! <br> //! Subscription resiliency is ensured by recreating the internal pubsub and issuing new subscription requests //! automatically in case of any error while fetching a message from the subscribed channels. //! //! # Examples //! //! ## Initialization and Simple Operations //! //! ``` //! extern crate simple_redis; //! //! fn main() { //! match simple_redis::create("redis://127.0.0.1:6379/") { //! Ok(mut client) => { //! println!("Created Redis Client"); //! //! match client.set("my_key", "my_value") { //! Err(error) => println!("Unable to set value in Redis: {}", error), //! _ => println!("Value set in Redis") //! }; //! //! match client.get("my_key") { //! Ok(value) => println!("Read value from Redis: {}", value), //! Err(error) => println!("Unable to get value from Redis: {}", error) //! }; //! //! /// run some command that is not built in the library //! match client.run_command::<String>("ECHO", vec!["testing"]) { //! Ok(value) => assert_eq!(value, "testing"), //! _ => panic!("test error"), //! }; //! //! /// publish messages //! let result = client.publish("news_channel", "test message"); //! assert!(result.is_ok()); //! }, //! Err(error) => println!("Unable to create Redis client: {}", error) //! } //! } //! ``` //! //! ## Subscription Flow //! //! ```rust,no_run //! extern crate simple_redis; //! //! fn main() { //! match simple_redis::create("redis://127.0.0.1:6379/") { //! Ok(mut client) => { //! println!("Created Redis Client"); //! //! let mut result = client.subscribe("important_notifications"); //! assert!(result.is_ok()); //! result = client.psubscribe("*_notifications"); //! assert!(result.is_ok()); //! //! loop { //! /// fetch next message //! match client.get_message() { //! Ok(message) => { //! let payload: String = message.get_payload().unwrap(); //! assert_eq!(payload, "my important message") //! } //! _ => panic!("test error"), //! } //! } //! }, //! Err(error) => println!("Unable to create Redis client: {}", error) //! } //! } //! ``` //! mod types; mod connection; mod subscriber; mod commands; pub mod client; /// Error Type pub type RedisError = types::RedisError; /// PubSub message pub type Message = types::Message; /// Redis result which either holds a value or a Redis error pub type RedisResult<T> = types::RedisResult<T>; /// Holds empty result or error pub type RedisEmptyResult = types::RedisEmptyResult; /// Holds pubsub message result or error pub type RedisMessageResult = types::RedisMessageResult; /// Holds string result or error pub type RedisStringResult = types::RedisStringResult; /// Holds bool result or error pub type RedisBoolResult = types::RedisBoolResult; /// Constructs a new redis client.<br> /// The redis connection string must be in the following format: `redis://[:<passwd>@]<hostname>[:port][/<db>]` /// /// # Examples /// /// ``` /// extern crate simple_redis; /// /// fn main() { /// match simple_redis::create("redis://127.0.0.1:6379/") { /// Ok(client) => println!("Created Redis Client"), /// Err(error) => println!("Unable to create Redis client: {}", error) /// } /// } /// ``` pub fn create(connection_string: &str) -> Result<client::Client, RedisError> { client::create(connection_string) } #[cfg(test)] mod tests { use super::*; #[test] fn create_invalid_url() { let result = create("test/bad/url"); assert!(result.is_err()); } #[test] fn create_valid_url() { let result = create("redis://127.0.0.1:6379/"); assert!(result.is_ok()); match result { Ok(client) => assert!(!client.is_connection_open()), _ => panic!("test error"), }; } }