Crate simple_redis [] [src]

simple_redis

Simple redis client based on redis-rs with internal connection and subscription handling.

This library provides a very basic, simple API for the most common redis operations.
While not as comprehensive or flexiable as redis-rs, it does provide a simpler api for most common use cases and operations as well as automatic internal connection and subscription (pubsub) handling.
In addition, the entire API is accessible via redis client and there is no need to manage connection or pubsub instances in parallel.

Connection resiliency is managed by verifying the connection before every operation against the redis server.
In case of any connection issue, a new connection will be allocated to ensure the operation is invoked on a valid connection only.
However, this comes at a small performance cost of PING operation to the redis server.

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

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)
    }
}

Modules

client

Functions

create

Constructs a new redis client.
The redis connection string must be in the following format: redis://[:<passwd>@]<hostname>[:port][/<db>]

Type Definitions

Message

PubSub message

RedisBoolResult

Holds bool result or error

RedisEmptyResult

Holds empty result or error

RedisError

Error Type

RedisMessageResult

Holds pubsub message result or error

RedisResult

Redis result which either holds a value or a Redis error

RedisStringResult

Holds string result or error