Struct simple_redis::client::Client

source ·
pub struct Client { /* private fields */ }
Expand description

The redis client which enables to invoke redis operations.

Implementations§

source§

impl Client

source

pub fn is_connection_open(self: &mut Client) -> bool

Returns true if the currently stored connection is valid, otherwise false.
There is no need to call this function as any redis operation invocation will ensure a valid connection is created.

source

pub fn quit(self: &mut Client) -> RedisEmptyResult

Closes the internal connection to redis.
The client can still be reused and any invocation of other operations after this call, will reopen the connection.
See redis QUIT command.

Example
match client.quit() {
    Err(error) => println!("Error: {}", error),
    _ => println!("Connection Closed.")
}
source

pub fn run_command<T: FromRedisValue>( self: &mut Client, command: &str, args: Vec<&str> ) -> RedisResult<T>

Invokes the requested command with the provided arguments (all provided via args) and returns the operation response.
This function ensures that we have a valid connection and it is used internally by all other exposed commands.
This function is also public to enable invoking operations that are not directly exposed by the client.

Arguments
  • command - The Redis command, for example: GET
  • args - Vector of arguments for the given command
Example
match client.run_command::<String>("ECHO", vec!["testing"]) {
    Ok(value) => assert_eq!(value, "testing"),
    _ => panic!("test error"),
}
source

pub fn run_command_from_string_response<T: FromStr>( self: &mut Client, command: &str, args: Vec<&str> ) -> RedisResult<T>

invokes the run_command and returns typed result

source

pub fn run_command_empty_response( self: &mut Client, command: &str, args: Vec<&str> ) -> RedisEmptyResult

invokes the run_command but returns empty result

source

pub fn run_command_string_response( self: &mut Client, command: &str, args: Vec<&str> ) -> RedisStringResult

invokes the run_command but returns string result

source

pub fn run_command_bool_response( self: &mut Client, command: &str, args: Vec<&str> ) -> RedisBoolResult

invokes the run_command but returns bool result

source

pub fn subscribe(self: &mut Client, channel: &str) -> RedisEmptyResult

Subscribes to the provided channel.
Actual subscription only occurs at the first call to get_message.

Arguments
  • channel - The channel name, for example: level_info
Example
client.subscribe("important_notifications");
source

pub fn psubscribe(self: &mut Client, channel: &str) -> RedisEmptyResult

Subscribes to the provided channel pattern.
Actual subscription only occurs at the first call to get_message.

Arguments
  • channel - The channel pattern, for example: level_*
Example
client.psubscribe("important_notifications*");
source

pub fn is_subscribed(self: &mut Client, channel: &str) -> bool

Returns true if subscribed to the provided channel.

source

pub fn is_psubscribed(self: &mut Client, channel: &str) -> bool

Returns true if subscribed to the provided channel pattern.

source

pub fn unsubscribe(self: &mut Client, channel: &str) -> RedisEmptyResult

Unsubscribes from the provided channel.

source

pub fn punsubscribe(self: &mut Client, channel: &str) -> RedisEmptyResult

Unsubscribes from the provided channel pattern.

source

pub fn unsubscribe_all(self: &mut Client) -> RedisEmptyResult

Unsubscribes from all channels.

source

pub fn fetch_messages( self: &mut Client, on_message: &mut dyn FnMut(Message) -> bool, poll_interrupts: &mut dyn FnMut() -> Interrupts ) -> RedisEmptyResult

Fetches the messages from any of the subscribed channels and invokes the provided on_message handler.
This function will return an error in case no subscriptions are defined.
This function will block and continue to listen to all messages, until either the on_message returns true.

Arguments
  • on_message - Invoked on each read message. If returns true, the fetching will stop.
  • poll_interrupts - Returns the interrupts struct, enabling to modify the fetching.
Example
client.subscribe("important_notifications");

// fetch messages from all subscriptions
client.fetch_messages(
    &mut |message: simple_redis::Message| -> bool {
        let payload : String = message.get_payload().unwrap();
        println!("Got message: {}", payload);

        // continue fetching
        false
    },
    &mut || -> Interrupts { Interrupts::new() },
).unwrap();
source§

impl Client

Defines the redis commands exposed by the redis client.

source

pub fn auth(&mut self, password: &str) -> RedisEmptyResult

See redis AUTH command.

Example
          match client.auth("my_password") {
              Err(error) => println!("Auth error: {}", error),
              _ => println!("Authenticated")
          }
source

pub fn echo(&mut self, value: &str) -> RedisStringResult

See redis ECHO command.

source

pub fn publish(&mut self, channel: &str, message: &str) -> RedisEmptyResult

See redis PUBLISH command.

Example
match client.publish("important_notifications", "message text") {
  Err(error) => println!("Publish error: {}", error),
  _ => println!("Message published")
}
source

pub fn get<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T>

See redis GET command.

Example
match client.get::<i64>("my_key") {
    Ok(value) => println!("Read value from Redis: {}", value),
    Err(error) => println!("Unable to get value from Redis: {}", error)
}
source

pub fn get_string(self: &mut Client, key: &str) -> RedisStringResult

See redis GET command.
This function will always return a String response.

Example
match client.get_string("my_key") {
    Ok(value) => println!("Read value from Redis: {}", value),
    Err(error) => println!("Unable to get value from Redis: {}", error)
}
source

pub fn set<T: RedisArg>( self: &mut Client, key: &str, value: T ) -> RedisEmptyResult

See redis SET command.

Example
match client.set("my_key", "my_value") {
    Err(error) => println!("Unable to set value in Redis: {}", error),
    _ => println!("Value set in Redis")
}
source

pub fn setex<T: RedisArg>( &mut self, key: &str, value: T, seconds: usize ) -> RedisEmptyResult

See redis SETEX command.

Example
match client.setex("my_key", "my_value", 10) {
    Err(error) => println!("Unable to set value in Redis: {}", error),
    _ => println!("Value set in Redis and will expire in 10 seconds")
}
source

pub fn setnx<T: RedisArg>(&mut self, key: &str, value: T) -> RedisEmptyResult

See redis SETNX command.

source

pub fn getset<T: RedisArg, V: FromStr>( &mut self, key: &str, value: T ) -> RedisResult<V>

See redis GETSET command.

source

pub fn getset_string<T: RedisArg>( &mut self, key: &str, value: T ) -> RedisStringResult

See redis GETSET command.

source

pub fn del(&mut self, key: &str) -> RedisEmptyResult

See redis DEL command.

source

pub fn exists(&mut self, key: &str) -> RedisBoolResult

See redis EXISTS command.

source

pub fn expire(&mut self, key: &str, seconds: usize) -> RedisEmptyResult

See redis EXPIRE command.

source

pub fn pexpire(&mut self, key: &str, millies: usize) -> RedisEmptyResult

See redis PEXPIRE command.

source

pub fn persist(&mut self, key: &str) -> RedisEmptyResult

See redis PERSIST command.

source

pub fn rename(&mut self, key: &str, new_key: &str) -> RedisEmptyResult

See redis RENAME command.

source

pub fn renamenx(&mut self, key: &str, new_key: &str) -> RedisEmptyResult

See redis RENAMENX command.

source

pub fn append(&mut self, key: &str, value: &str) -> RedisEmptyResult

See redis APPEND command.

source

pub fn incr(&mut self, key: &str) -> RedisResult<i64>

See redis INCR command.

source

pub fn incrby<T: RedisArg>(&mut self, key: &str, value: T) -> RedisResult<i64>

See redis INCRBY command.

source

pub fn incrbyfloat<T: RedisArg>( &mut self, key: &str, value: T ) -> RedisResult<f64>

See redis INCRBYFLOAT command.

source

pub fn strlen(&mut self, key: &str) -> RedisResult<i32>

See redis STRLEN command.

source

pub fn keys(&mut self, pattern: &str) -> RedisResult<Vec<String>>

See redis KEYS command.

source

pub fn hget<T: FromStr>( self: &mut Client, key: &str, field: &str ) -> RedisResult<T>

See redis HGET command.

source

pub fn hget_string( self: &mut Client, key: &str, field: &str ) -> RedisStringResult

See redis HGET command.

source

pub fn hgetall( self: &mut Client, key: &str ) -> RedisResult<HashMap<String, String>>

See redis HGETALL command.

Example
match client.hgetall("my_map") {
    Ok(map) => {
        match map.get("my_field") {
            Some(value) => println!("Got field value from map: {}", value),
            None => println!("Map field is emtpy"),
        }
    },
    Err(error) => println!("Unable to read map from Redis: {}", error),
}
source

pub fn hset<T: RedisArg>( self: &mut Client, key: &str, field: &str, value: T ) -> RedisEmptyResult

See redis HSET command.

source

pub fn hsetnx<T: RedisArg>( self: &mut Client, key: &str, field: &str, value: T ) -> RedisEmptyResult

See redis HSETNX command.

source

pub fn hdel(self: &mut Client, key: &str, field: &str) -> RedisEmptyResult

See redis HDEL command.

source

pub fn hexists(self: &mut Client, key: &str, field: &str) -> RedisBoolResult

See redis HEXISTS command.

source

pub fn hkeys(&mut self, key: &str) -> RedisResult<Vec<String>>

See redis HKEYS command.

source

pub fn hvals(&mut self, key: &str) -> RedisResult<Vec<String>>

See redis HVALS command.

source

pub fn lset<T: RedisArg>( self: &mut Client, key: &str, index: isize, value: T ) -> RedisEmptyResult

See redis LSET command.

source

pub fn lindex<T: FromStr>( self: &mut Client, key: &str, index: isize ) -> RedisResult<T>

See redis HGET command.

source

pub fn lindex_string( self: &mut Client, key: &str, index: isize ) -> RedisStringResult

See redis HGET command.

source

pub fn llen(self: &mut Client, key: &str) -> RedisResult<i32>

See redis LLEN command.

source

pub fn lpop<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T>

See redis LPOP command.

source

pub fn lpush<T: RedisArg>( self: &mut Client, key: &str, value: T ) -> RedisEmptyResult

See redis LPUSH command.

source

pub fn lpushx<T: RedisArg>( self: &mut Client, key: &str, value: T ) -> RedisEmptyResult

See redis LPUSHX command.

source

pub fn lrange( self: &mut Client, key: &str, start: isize, stop: isize ) -> RedisResult<Vec<String>>

See redis LRANGE command.

source

pub fn lrem<T: RedisArg>( self: &mut Client, key: &str, count: isize, value: T ) -> RedisEmptyResult

See redis LREM command.

source

pub fn ltrim( self: &mut Client, key: &str, start: isize, stop: isize ) -> RedisEmptyResult

See redis LTRIM command.

source

pub fn rpop<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T>

See redis RPOP command.

source

pub fn rpush<T: RedisArg>( self: &mut Client, key: &str, value: T ) -> RedisEmptyResult

See redis RPUSH command.

source

pub fn rpushx<T: RedisArg>( self: &mut Client, key: &str, value: T ) -> RedisEmptyResult

See redis RPUSHX command.

source

pub fn sadd(self: &mut Client, key: &str, member: &str) -> RedisResult<i32>

See redis SADD command.

source

pub fn scard(self: &mut Client, key: &str) -> RedisResult<i32>

See redis SCARD command.

source

pub fn sdiff(self: &mut Client, keys: Vec<&str>) -> RedisResult<Vec<String>>

See redis SDIFF command.

source

pub fn sismember(self: &mut Client, key: &str, member: &str) -> RedisBoolResult

See redis SISMEMBER command.

source

pub fn smembers(self: &mut Client, key: &str) -> RedisResult<Vec<String>>

See redis SMEMBERS command.

source

pub fn smove( self: &mut Client, source_key: &str, destination_key: &str, member: &str ) -> RedisEmptyResult

See redis SMOVE command.

source

pub fn srem(self: &mut Client, key: &str, member: &str) -> RedisEmptyResult

See redis SREM command.

source

pub fn zadd( self: &mut Client, key: &str, score: isize, member: &str ) -> RedisResult<i32>

See redis ZADD command.

source

pub fn zrange( self: &mut Client, key: &str, start: isize, stop: isize ) -> RedisResult<Vec<String>>

See redis ZRANGE command.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.