Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

Redis client implementation.

Implementations§

Source§

impl Client

Source

pub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self>

Establish a connection to the Redis server.

§Examples
use async_redis::Client;

#[tokio::main]
async fn main() {
    let mut c = Client::connect("127.0.0.1:6379").await.unwrap();
}
Source

pub async fn hello( &mut self, proto: Option<u8>, ) -> Result<HashMap<String, Vec<u8>>>

Sends a HELLO command to the Redis server.

§Arguments
  • proto - An optional protocol version to use
§Returns
  • Ok(HashMap<String, Vec<u8>>) if the HELLO command is successful
  • Err(RedisError) if an error occurs
Source

pub async fn ping(&mut self, msg: Option<&[u8]>) -> Result<Vec<u8>>

Sends a PING command to the Redis server, optionally with a message.

§Arguments
  • msg - An optional message to send to the server
§Returns
  • Ok(String) if the PING command is successful
  • Err(RedisError) if an error occurs
§Examples
use async_redis::Client;

#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.ping(Some("Hello Redis".to_string())).await.unwrap();
}
Source

pub async fn get(&mut self, key: &str) -> Result<Option<Vec<u8>>>

Sends a GET command to the Redis server.

§Description

The GET command retrieves the value of a key stored on the Redis server.

§Arguments
  • key - A required key to send to the server
§Returns
  • Ok(Some(String)) if the key to GET exists
  • Ok(None) if the key to GET does not exist
  • Err(RedisError) if an error occurs
§Examples
use async_redis::Client;

#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.get("mykey").await?;
}
Source

pub async fn get_ex( &mut self, key: &str, seconds: i64, ) -> Result<Option<Vec<u8>>>

Sends a GETEX command to the Redis server.

Source

pub async fn mget(&mut self, keys: Vec<&str>) -> Result<Option<Vec<Vec<u8>>>>

Sends a MGET command to the Redis server.

Source

pub async fn set(&mut self, key: &str, val: &[u8]) -> Result<Option<Vec<u8>>>

Sends a SET command to the Redis server.

§Description

The SET command sets the value of a key in the Redis server.

§Arguments
  • key - A required key to set
  • val - A required value to set
§Returns
  • Ok(Some(String)) if the key is set successfully
  • Ok(None) if the key is not set
§Examples
use async_redis::Client;

#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.set("mykey", "myvalue").await?;
}
Source

pub async fn set_ex( &mut self, key: &str, val: &[u8], seconds: i64, ) -> Result<Option<Vec<u8>>>

Sends a SETEX command to the Redis server.

Source

pub async fn set_nx(&mut self, key: &str, val: &[u8]) -> Result<Option<Vec<u8>>>

Sends a SETNX command to the Redis server.

Source

pub async fn del(&mut self, keys: Vec<&str>) -> Result<u64>

Sends a DEL command to the Redis server.

§Description

The DEL command deletes a key from the Redis server.

§Arguments
  • keys - A required vector of keys to delete
§Returns
  • Ok(u64) the number of keys deleted
§Examples

use async_redis::Client;

#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.del(vec!["foo", "bar", "baz"]).await?;
}
Source

pub async fn exists(&mut self, keys: Vec<&str>) -> Result<u64>

Sends an EXISTS command to the Redis server.

§Description

The EXISTS command checks if a key exists in the Redis server.

§Arguments
  • keys - A required vector of keys to check
§Returns
  • Ok(u64) the number of keys that exist
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.exists(vec!["foo", "bar", "baz"]).await?;
}
Source

pub async fn expire(&mut self, key: &str, seconds: i64) -> Result<u64>

Sends an EXPIRE command to the Redis server.

§Description

The EXPIRE command sets a timeout on a key. After the timeout has expired, the key will be deleted.

§Arguments
  • key - A required key to set the timeout
  • seconds - A required number of seconds to set the timeout
§Returns
  • Ok(1) if the key is set successfully
  • Ok(0) if the key is not set
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.expire("mykey", 1).await?;
}
Source

pub async fn ttl(&mut self, key: &str) -> Result<i64>

Sends a TTL command to the Redis server.

§Description

The TTL command returns the remaining time to live of a key that has an expire set.

§Arguments
  • key - A required key to check ttl
§Returns
  • Ok(-2) if the key does not exist
  • Ok(-1) if the key exists but has no expire set
  • Ok(other) if the key exists and has an expire set
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.ttl("mykey").await?;
}
Source

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

Sends an INCR command to the Redis server.

§Description

The INCR command increments the integer value of a key by one.

§Arguments
  • key - A required key to increment
§Returns
  • Ok(i64) the new value of the key after increment
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.incr("mykey").await?;
}
Source

pub async fn incr_by(&mut self, key: &str, increment: i64) -> Result<i64>

Sends an INCRBY command to the Redis server.

Source

pub async fn incr_by_float(&mut self, key: &str, increment: f64) -> Result<f64>

Sends an INCRBYFLOAT command to the Redis server.

Source

pub async fn decr(&mut self, key: &str) -> Result<i64>

Sends a DECR command to the Redis server.

§Description

The DECR command decrements the integer value of a key by one.

§Arguments
  • key - A required key to decrement
§Returns
  • Ok(i64) the new value of the key after decrement
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.decr("mykey").await?;
}
Source

pub async fn decr_by(&mut self, key: &str, decrement: i64) -> Result<i64>

Sends a DECRBY command to the Redis server.

Source

pub async fn decr_by_float(&mut self, key: &str, decrement: f64) -> Result<f64>

Sends a DECRBYFLOAT command to the Redis server.

Source

pub async fn lpush(&mut self, key: &str, values: Vec<&[u8]>) -> Result<u64>

Sends an LPUSH command to the Redis server.

§Description

The LPUSH command inserts all the specified values at the head of the list stored at key.

§Arguments
  • key - A required key to insert values
  • values - A required vector of values to insert
§Returns
  • Ok(u64) the length of the list after the push operation
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.lpush("mykey", vec!["foo", "bar", "baz"]).await?;
}
Source

pub async fn rpush(&mut self, key: &str, values: Vec<&[u8]>) -> Result<u64>

Sends an RPUSH command to the Redis server.

§Description

The RPUSH command inserts all the specified values at the tail of the list stored at key.

§Arguments
  • key - A required key to insert values
  • values - A required vector of values to insert
§Returns
  • Ok(u64) the length of the list after the push operation
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.rpush("mykey", vec!["foo", "bar", "baz"]).await?;
}
Source

pub async fn lpop(&mut self, key: &str) -> Result<Option<Vec<u8>>>

Sends an LPOP command to the Redis server.

§Description

The LPOP command removes and returns the removed elements from the head of the list stored at key.

§Arguments
  • key - A required key to remove values
  • count - An optional number of elements to remove
§Returns
  • Ok(Some(String)) if the key exists and the elements are removed
  • Ok(None) if the key does not exist
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.lpop("mykey", 1).await?;
}
Source

pub async fn lpop_n( &mut self, key: &str, count: u64, ) -> Result<Option<Vec<Vec<u8>>>>

Source

pub async fn rpop(&mut self, key: &str) -> Result<Option<Vec<u8>>>

Sends an RPOP command to the Redis server.

§Description

The RPOP command removes and returns the removed elements from the tail of the list stored at key.

§Arguments
  • key - A required key to remove values
  • count - An optional number of elements to remove
§Returns
  • Ok(Some(String)) if the key exists and the elements are removed
  • Ok(None) if the key does not exist
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.rpop("mykey", 1).await?;
}
Source

pub async fn rpop_n( &mut self, key: &str, count: u64, ) -> Result<Option<Vec<Vec<u8>>>>

Source

pub async fn lrange( &mut self, key: &str, start: i64, end: i64, ) -> Result<Vec<Vec<u8>>>

Sends an LRANGE command to the Redis server.

§Description

The LRANGE command returns the specified elements of the list stored at key.

§Arguments
  • key - A required key to get values
  • start - A required start index
  • end - A required end index
§Returns
  • Ok(Some(String)) if the key exists and the elements are returned
  • Ok(None) if the key does not exist
  • Err(RedisError) if an error occurs
§Examples
#[tokio::main]
async fn main() {
    let mut client = Client::connect("127.0.0.1:6379").await.unwrap();
    let resp = client.lrange("mykey", 0, -1).await?;
}
Source

pub async fn hget(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>

Sends an HGET command to the Redis server.

Source

pub async fn hmget( &mut self, key: &str, fields: Vec<&str>, ) -> Result<Option<Vec<Vec<u8>>>>

Sends an HMGET command to the Redis server.

Source

pub async fn hget_all( &mut self, key: &str, ) -> Result<Option<HashMap<String, Vec<u8>>>>

Sends an HGETALL command to the Redis server.

Source

pub async fn hkeys(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>

Sends an HKEYS command to the Redis server.

Source

pub async fn hvals(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>

Sends an HVALS command to the Redis server.

Source

pub async fn hlen(&mut self, key: &str) -> Result<Option<u64>>

Sends an HLEN command to the Redis server.

Source

pub async fn hset( &mut self, key: &str, field: &str, value: &[u8], ) -> Result<Option<Vec<u8>>>

Sends an HSET command to the Redis server.

Source

pub async fn hset_nx( &mut self, key: &str, field: &str, value: &[u8], ) -> Result<Option<Vec<u8>>>

Sends an HSETNX command to the Redis server.

Source

pub async fn hmset( &mut self, key: &str, fields: HashMap<String, Vec<u8>>, ) -> Result<Option<Vec<u8>>>

Sends an HMSET command to the Redis server.

Source

pub async fn hdel(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>

Sends an HDEL command to the Redis server.

Source

pub async fn sadd( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>

Sends an SADD command to the Redis server.

Source

pub async fn srem( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>

Sends an SREM command to the Redis server.

Source

pub async fn sismember( &mut self, key: &str, member: &[u8], ) -> Result<Option<Vec<u8>>>

Sends an SISMEMBER command to the Redis server.

Source

pub async fn smembers(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>

Sends an SMEMBERS command to the Redis server.

Source

pub async fn spop(&mut self, key: &str) -> Result<Option<Vec<u8>>>

Sends an SPOP command to the Redis server.

Source

pub async fn zadd( &mut self, key: &str, members: HashMap<String, f64>, ) -> Result<Option<Vec<u8>>>

Sends a ZADD command to the Redis server.

Source

pub async fn zrem( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>

Sends a ZREM command to the Redis server.

Source

pub async fn zrange( &mut self, key: &str, start: i64, end: i64, ) -> Result<Option<Vec<Vec<u8>>>>

Sends a ZRANGE command to the Redis server.

Source

pub async fn zrevrange( &mut self, key: &str, start: i64, end: i64, ) -> Result<Option<Vec<Vec<u8>>>>

Sends a ZREVRANGE command to the Redis server.

Source

pub async fn zrank(&mut self, key: &str, member: &[u8]) -> Result<Option<u64>>

Sends a ZRANK command to the Redis server.

Source

pub async fn zrevrank( &mut self, key: &str, member: &[u8], ) -> Result<Option<u64>>

Sends a ZREVRANK command to the Redis server.

Source

pub async fn zscore(&mut self, key: &str, member: &[u8]) -> Result<Option<f64>>

Sends a ZSCORE command to the Redis server.

Source

pub async fn zcard(&mut self, key: &str) -> Result<Option<u64>>

Sends a ZCARD command to the Redis server.

Source

pub async fn zcount( &mut self, key: &str, min: f64, max: f64, ) -> Result<Option<u64>>

Sends a ZCOUNT command to the Redis server.

Source

pub async fn zincr_by( &mut self, key: &str, increment: f64, member: &[u8], ) -> Result<Option<f64>>

Sends a ZINCRBY command to the Redis server.

Auto Trait Implementations§

§

impl !Freeze for Client

§

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>,

Source§

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>,

Source§

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.