pub struct Client { /* private fields */ }
Expand description
Redis client implementation.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self>
pub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<Self>
Sourcepub async fn ping(&mut self, msg: Option<&[u8]>) -> Result<Vec<u8>>
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 successfulErr(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();
}
Sourcepub async fn get(&mut self, key: &str) -> Result<Option<Vec<u8>>>
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 existsOk(None)
if the key to GET does not existErr(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?;
}
Sourcepub async fn get_ex(
&mut self,
key: &str,
seconds: i64,
) -> Result<Option<Vec<u8>>>
pub async fn get_ex( &mut self, key: &str, seconds: i64, ) -> Result<Option<Vec<u8>>>
Sends a GETEX command to the Redis server.
Sourcepub async fn mget(&mut self, keys: Vec<&str>) -> Result<Option<Vec<Vec<u8>>>>
pub async fn mget(&mut self, keys: Vec<&str>) -> Result<Option<Vec<Vec<u8>>>>
Sends a MGET command to the Redis server.
Sourcepub async fn set(&mut self, key: &str, val: &[u8]) -> Result<Option<Vec<u8>>>
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 setval
- A required value to set
§Returns
Ok(Some(String))
if the key is set successfullyOk(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?;
}
Sourcepub async fn set_ex(
&mut self,
key: &str,
val: &[u8],
seconds: i64,
) -> Result<Option<Vec<u8>>>
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.
Sourcepub async fn set_nx(&mut self, key: &str, val: &[u8]) -> Result<Option<Vec<u8>>>
pub async fn set_nx(&mut self, key: &str, val: &[u8]) -> Result<Option<Vec<u8>>>
Sends a SETNX command to the Redis server.
Sourcepub async fn del(&mut self, keys: Vec<&str>) -> Result<u64>
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?;
}
Sourcepub async fn exists(&mut self, keys: Vec<&str>) -> Result<u64>
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?;
}
Sourcepub async fn expire(&mut self, key: &str, seconds: i64) -> Result<u64>
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 timeoutseconds
- A required number of seconds to set the timeout
§Returns
Ok(1)
if the key is set successfullyOk(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?;
}
Sourcepub async fn ttl(&mut self, key: &str) -> Result<i64>
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 existOk(-1)
if the key exists but has no expire setOk(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?;
}
Sourcepub async fn incr(&mut self, key: &str) -> Result<i64>
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 incrementErr(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?;
}
Sourcepub async fn incr_by(&mut self, key: &str, increment: i64) -> Result<i64>
pub async fn incr_by(&mut self, key: &str, increment: i64) -> Result<i64>
Sends an INCRBY command to the Redis server.
Sourcepub async fn incr_by_float(&mut self, key: &str, increment: f64) -> Result<f64>
pub async fn incr_by_float(&mut self, key: &str, increment: f64) -> Result<f64>
Sends an INCRBYFLOAT command to the Redis server.
Sourcepub async fn decr(&mut self, key: &str) -> Result<i64>
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 decrementErr(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?;
}
Sourcepub async fn decr_by(&mut self, key: &str, decrement: i64) -> Result<i64>
pub async fn decr_by(&mut self, key: &str, decrement: i64) -> Result<i64>
Sends a DECRBY command to the Redis server.
Sourcepub async fn decr_by_float(&mut self, key: &str, decrement: f64) -> Result<f64>
pub async fn decr_by_float(&mut self, key: &str, decrement: f64) -> Result<f64>
Sends a DECRBYFLOAT command to the Redis server.
Sourcepub async fn lpush(&mut self, key: &str, values: Vec<&[u8]>) -> Result<u64>
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 valuesvalues
- A required vector of values to insert
§Returns
Ok(u64)
the length of the list after the push operationErr(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?;
}
Sourcepub async fn rpush(&mut self, key: &str, values: Vec<&[u8]>) -> Result<u64>
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 valuesvalues
- 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?;
}
Sourcepub async fn lpop(&mut self, key: &str) -> Result<Option<Vec<u8>>>
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 valuescount
- An optional number of elements to remove
§Returns
Ok(Some(String))
if the key exists and the elements are removedOk(None)
if the key does not existErr(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?;
}
pub async fn lpop_n( &mut self, key: &str, count: u64, ) -> Result<Option<Vec<Vec<u8>>>>
Sourcepub async fn rpop(&mut self, key: &str) -> Result<Option<Vec<u8>>>
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 valuescount
- An optional number of elements to remove
§Returns
Ok(Some(String))
if the key exists and the elements are removedOk(None)
if the key does not existErr(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?;
}
pub async fn rpop_n( &mut self, key: &str, count: u64, ) -> Result<Option<Vec<Vec<u8>>>>
Sourcepub async fn lrange(
&mut self,
key: &str,
start: i64,
end: i64,
) -> Result<Vec<Vec<u8>>>
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 valuesstart
- A required start indexend
- A required end index
§Returns
Ok(Some(String))
if the key exists and the elements are returnedOk(None)
if the key does not existErr(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?;
}
Sourcepub async fn hget(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>
pub async fn hget(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>
Sends an HGET command to the Redis server.
Sourcepub async fn hmget(
&mut self,
key: &str,
fields: Vec<&str>,
) -> Result<Option<Vec<Vec<u8>>>>
pub async fn hmget( &mut self, key: &str, fields: Vec<&str>, ) -> Result<Option<Vec<Vec<u8>>>>
Sends an HMGET command to the Redis server.
Sourcepub async fn hget_all(
&mut self,
key: &str,
) -> Result<Option<HashMap<String, Vec<u8>>>>
pub async fn hget_all( &mut self, key: &str, ) -> Result<Option<HashMap<String, Vec<u8>>>>
Sends an HGETALL command to the Redis server.
Sourcepub async fn hkeys(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
pub async fn hkeys(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
Sends an HKEYS command to the Redis server.
Sourcepub async fn hvals(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
pub async fn hvals(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
Sends an HVALS command to the Redis server.
Sourcepub async fn hlen(&mut self, key: &str) -> Result<Option<u64>>
pub async fn hlen(&mut self, key: &str) -> Result<Option<u64>>
Sends an HLEN command to the Redis server.
Sourcepub async fn hset(
&mut self,
key: &str,
field: &str,
value: &[u8],
) -> Result<Option<Vec<u8>>>
pub async fn hset( &mut self, key: &str, field: &str, value: &[u8], ) -> Result<Option<Vec<u8>>>
Sends an HSET command to the Redis server.
Sourcepub async fn hset_nx(
&mut self,
key: &str,
field: &str,
value: &[u8],
) -> Result<Option<Vec<u8>>>
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.
Sourcepub async fn hmset(
&mut self,
key: &str,
fields: HashMap<String, Vec<u8>>,
) -> Result<Option<Vec<u8>>>
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.
Sourcepub async fn hdel(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>
pub async fn hdel(&mut self, key: &str, field: &str) -> Result<Option<Vec<u8>>>
Sends an HDEL command to the Redis server.
Sourcepub async fn sadd(
&mut self,
key: &str,
members: Vec<&[u8]>,
) -> Result<Option<Vec<u8>>>
pub async fn sadd( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>
Sends an SADD command to the Redis server.
Sourcepub async fn srem(
&mut self,
key: &str,
members: Vec<&[u8]>,
) -> Result<Option<Vec<u8>>>
pub async fn srem( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>
Sends an SREM command to the Redis server.
Sourcepub async fn sismember(
&mut self,
key: &str,
member: &[u8],
) -> Result<Option<Vec<u8>>>
pub async fn sismember( &mut self, key: &str, member: &[u8], ) -> Result<Option<Vec<u8>>>
Sends an SISMEMBER command to the Redis server.
Sourcepub async fn smembers(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
pub async fn smembers(&mut self, key: &str) -> Result<Option<Vec<Vec<u8>>>>
Sends an SMEMBERS command to the Redis server.
Sourcepub async fn spop(&mut self, key: &str) -> Result<Option<Vec<u8>>>
pub async fn spop(&mut self, key: &str) -> Result<Option<Vec<u8>>>
Sends an SPOP command to the Redis server.
Sourcepub async fn zadd(
&mut self,
key: &str,
members: HashMap<String, f64>,
) -> Result<Option<Vec<u8>>>
pub async fn zadd( &mut self, key: &str, members: HashMap<String, f64>, ) -> Result<Option<Vec<u8>>>
Sends a ZADD command to the Redis server.
Sourcepub async fn zrem(
&mut self,
key: &str,
members: Vec<&[u8]>,
) -> Result<Option<Vec<u8>>>
pub async fn zrem( &mut self, key: &str, members: Vec<&[u8]>, ) -> Result<Option<Vec<u8>>>
Sends a ZREM command to the Redis server.
Sourcepub async fn zrange(
&mut self,
key: &str,
start: i64,
end: i64,
) -> Result<Option<Vec<Vec<u8>>>>
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.
Sourcepub async fn zrevrange(
&mut self,
key: &str,
start: i64,
end: i64,
) -> Result<Option<Vec<Vec<u8>>>>
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.
Sourcepub async fn zrank(&mut self, key: &str, member: &[u8]) -> Result<Option<u64>>
pub async fn zrank(&mut self, key: &str, member: &[u8]) -> Result<Option<u64>>
Sends a ZRANK command to the Redis server.
Sourcepub async fn zrevrank(
&mut self,
key: &str,
member: &[u8],
) -> Result<Option<u64>>
pub async fn zrevrank( &mut self, key: &str, member: &[u8], ) -> Result<Option<u64>>
Sends a ZREVRANK command to the Redis server.
Sourcepub async fn zscore(&mut self, key: &str, member: &[u8]) -> Result<Option<f64>>
pub async fn zscore(&mut self, key: &str, member: &[u8]) -> Result<Option<f64>>
Sends a ZSCORE command to the Redis server.
Sourcepub async fn zcard(&mut self, key: &str) -> Result<Option<u64>>
pub async fn zcard(&mut self, key: &str) -> Result<Option<u64>>
Sends a ZCARD command to the Redis server.