simple_redis

crates.io Build Status Build status Crates.io Documentation Crates.io

Simple and resilient redis client for rust.

Overview

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 and resilient 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 internally managed 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.
In redis-rs, connections are no longer usable in case the connection is broken and if operations are invoked on the client directly, it will basically open a new connection for every operation which is very costly.

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.
redis-rs doesn't provide any such automatic resiliency and resubscription capabilities.

This library is still in initial development stage and many more features will come soon.

Usage

In order to use this library, you need to first include the crate as follows:

rust extern crate simple_redis;

Afterwards create a redis client using a connection string:

rust match simple_redis::create("redis://127.0.0.1:6379/") { Ok(mut client) => println!("Created Redis Client"), Err(error) => println!("Unable to create Redis client: {}", error) }

Once you have a redis client, you can invoke any of the available commands directly or use the run_command function to invoke operations that were not implemented by the library.

````rust match client.set("mykey", "myvalue") { Err(error) => println!("Unable to set value in Redis: {}", error), _ => println!("Value set in Redis") };

match client.getstring("mykey") { Ok(value) => println!("Read value from Redis: {}", value), Err(error) => println!("Unable to get value from Redis: {}", error) };

match client.set("mynumerickey", 255.5) { Err(error) => println!("Unable to set value in Redis: {}", error), _ => println!("Value set in Redis") };

match client.get::("mynumerickey") { 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.runcommand::("ECHO", vec!["testing"]) { Ok(value) => asserteq!(value, "testing"), _ => panic!("test error"), };

/// publish messages let mut result = client.publish("newschannel", "test message"); assert!(result.isok());

/// subscribe to channels result = client.subscribe("importantnotifications"); assert!(result.isok()); result = client.psubscribe("*notifications"); assert!(result.isok());

loop { /// fetch next message match client.getmessage() { Ok(message) => { let payload: String = message.getpayload().unwrap(); assert_eq!(payload, "my important message") } _ => panic!("test error"), } } ````

Installation

In order to use this library, just add it as a dependency:

ini [dependencies] simple_redis = "*"

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

| Date | Version | Description | | ----------- | ------- | ----------- | | 2017-06-04 | v0.2.2 | Added more data type support | | 2017-06-04 | v0.1.13 | Support more data types | | 2017-06-03 | v0.1.10 | More commands added | | 2017-06-03 | v0.1.7 | pubsub support added | | 2017-06-02 | v0.1.6 | Initial release. |

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.