pub struct Connection { /* private fields */ }
Expand description
A connection to Redis. Copying is cheap as the inner type is a simple, futures-aware, Arc<Mutex>
, and will
not create a new connection. Use a ConnectionPool
if you want to use pooled connections.
Alternatively, there’s the deadpool-darkredis
crate.
Every convenience function can work with any kind of data as long as it can be converted into bytes.
Check the Redis command reference for in-depth explanations of each command.
Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn connect<A>(address: A) -> Result<Self>where
A: ToSocketAddrs,
pub async fn connect<A>(address: A) -> Result<Self>where
A: ToSocketAddrs,
Connect to a Redis instance running at address
. If you wish to name this connection, run the CLIENT SETNAME
command.
Sourcepub async fn connect_and_auth<A, P>(address: A, password: P) -> Result<Self>
pub async fn connect_and_auth<A, P>(address: A, password: P) -> Result<Self>
Connect to a Redis instance running at address
, and authenticate using password
.
Sourcepub async fn run_command(&mut self, command: Command<'_>) -> Result<Value>
pub async fn run_command(&mut self, command: Command<'_>) -> Result<Value>
Run a single command on this connection.
Sourcepub async fn run_command_with_buffer(
&mut self,
command: Command<'_>,
buffer: &mut Vec<u8>,
) -> Result<Value>
pub async fn run_command_with_buffer( &mut self, command: Command<'_>, buffer: &mut Vec<u8>, ) -> Result<Value>
Run a single command on this connection, using buffer
for serialization.
See run_commands_with_buffer
for more details.
Sourcepub async fn run_commands(
&mut self,
command: CommandList<'_>,
) -> Result<ResponseStream>
pub async fn run_commands( &mut self, command: CommandList<'_>, ) -> Result<ResponseStream>
Run a series of commands on this connection, returning a stream of the results.
Sourcepub async fn run_commands_with_buffer(
&mut self,
command: CommandList<'_>,
buf: &mut Vec<u8>,
) -> Result<ResponseStream>
pub async fn run_commands_with_buffer( &mut self, command: CommandList<'_>, buf: &mut Vec<u8>, ) -> Result<ResponseStream>
Run a series of commands on this connection, using buffer
for serialization.
This prevents allocations as long as buffer
is large enough from before. The
buffer will be empty when this function returns.
Sourcepub async fn hdel<K, F>(&mut self, key: K, field: F) -> Result<bool>
pub async fn hdel<K, F>(&mut self, key: K, field: F) -> Result<bool>
Delete field
from the hash set stored at key
.
§Return value
true
when the field was deleted, false
if it didn’t exist
Sourcepub async fn hdel_slice<K, F>(&mut self, key: K, fields: &[F]) -> Result<isize>
pub async fn hdel_slice<K, F>(&mut self, key: K, fields: &[F]) -> Result<isize>
Delete every field in fields
from the hash set stored at key
.
§Return value
The number of deleted fields.
Sourcepub async fn hexists<K, F>(&mut self, key: K, field: F) -> Result<bool>
pub async fn hexists<K, F>(&mut self, key: K, field: F) -> Result<bool>
Check if field
exists in the hash set key
.
Sourcepub async fn hget<K, F>(&mut self, key: K, field: F) -> Result<Option<Vec<u8>>>
pub async fn hget<K, F>(&mut self, key: K, field: F) -> Result<Option<Vec<u8>>>
Get the value of field
in the hash set at key
.
Sourcepub async fn hset<K, F, V>(
&mut self,
key: K,
field: F,
value: V,
) -> Result<isize>
pub async fn hset<K, F, V>( &mut self, key: K, field: F, value: V, ) -> Result<isize>
Set the value of field
in the hash set stored at key
to Value
.
§Return value
The number of added fields(will be 1 if field
was created, 0 if it already existed).
Sourcepub async fn hsetnx<K, F, V>(
&mut self,
key: K,
field: F,
value: V,
) -> Result<bool>
pub async fn hsetnx<K, F, V>( &mut self, key: K, field: F, value: V, ) -> Result<bool>
Set the value of field
in the hash set stored at key
to Value
. If field
already exists, do nothing.
§Return value
true
if field
was set, false
otherwise.
Sourcepub async fn hset_many<K>(
&mut self,
key: K,
builder: MSetBuilder<'_>,
) -> Result<isize>
pub async fn hset_many<K>( &mut self, key: K, builder: MSetBuilder<'_>, ) -> Result<isize>
Set fields in the hash set at key
to their values in builder
. See
Connection::mset
for more information.
§Return value
The number of added fields.
Sourcepub async fn hincrbyfloat<K, F>(
&mut self,
key: K,
field: F,
val: f64,
) -> Result<f64>
pub async fn hincrbyfloat<K, F>( &mut self, key: K, field: F, val: f64, ) -> Result<f64>
Increment field
in the hash set key
by val
, floating point version.
§Return value
The field value after the increment.
Sourcepub async fn hkeys<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
pub async fn hkeys<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
Get the name of each hash field stored at key
.
Sourcepub async fn hlen<K>(&mut self, key: K) -> Result<isize>
pub async fn hlen<K>(&mut self, key: K) -> Result<isize>
Get the number of fields in the hash stored at key
.
Sourcepub async fn hstrlen<K, F>(&mut self, key: K, field: F) -> Result<isize>
pub async fn hstrlen<K, F>(&mut self, key: K, field: F) -> Result<isize>
Get the number of bytes in field
in the hash set key
Sourcepub async fn hvals<K>(&mut self, key: K) -> Result<Vec<Value>>
pub async fn hvals<K>(&mut self, key: K) -> Result<Vec<Value>>
Get the value of each field in the hash field stored at key
.
Sourcepub async fn ping(&mut self) -> Result<()>
pub async fn ping(&mut self) -> Result<()>
Send a PING
to the server, returning Ok(()) on success.
Sourcepub async fn subscribe<K>(self, channels: &[K]) -> Result<MessageStream>
pub async fn subscribe<K>(self, channels: &[K]) -> Result<MessageStream>
Consume self
, and subscribe to channels
, returning a stream of Message's
. As of now, there’s no way to get the connection back, nor change the subscribed topics.
Sourcepub async fn psubscribe<K>(self, patterns: &[K]) -> Result<PMessageStream>
pub async fn psubscribe<K>(self, patterns: &[K]) -> Result<PMessageStream>
Exactly like subscribe
, but subscribe to channels
matching patterns instead.
Sourcepub async fn set_and_expire_seconds<K, D>(
&mut self,
key: K,
data: D,
seconds: u32,
) -> Result<()>
pub async fn set_and_expire_seconds<K, D>( &mut self, key: K, data: D, seconds: u32, ) -> Result<()>
Set the key key
to data
, and set it to expire after seconds
seconds.
Sourcepub async fn set_and_expire_ms<K, D>(
&mut self,
key: K,
data: D,
milliseconds: u32,
) -> Result<()>
pub async fn set_and_expire_ms<K, D>( &mut self, key: K, data: D, milliseconds: u32, ) -> Result<()>
Set the key key
to data
, and set it to expire after milliseconds
ms.
Sourcepub async fn expire_seconds<K>(&mut self, key: K, seconds: u32) -> Result<isize>
pub async fn expire_seconds<K>(&mut self, key: K, seconds: u32) -> Result<isize>
Set key
to expire seconds
seconds from now.
Sourcepub async fn expire_ms<K>(&mut self, key: K, seconds: u32) -> Result<isize>
pub async fn expire_ms<K>(&mut self, key: K, seconds: u32) -> Result<isize>
Set key
to expire milliseconds
ms from now.
Sourcepub async fn expire_at_seconds<K>(
&mut self,
key: K,
timestamp: u64,
) -> Result<isize>
pub async fn expire_at_seconds<K>( &mut self, key: K, timestamp: u64, ) -> Result<isize>
Set key
to expire at Unix timestamp timestamp
, measured in seconds.
Sourcepub async fn expire_at_ms<K>(&mut self, key: K, timestamp: u64) -> Result<isize>
pub async fn expire_at_ms<K>(&mut self, key: K, timestamp: u64) -> Result<isize>
Set key
to expire at Unix timestamp timestamp
, measured in milliseconds.
Sourcepub async fn lpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
pub async fn lpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
Like lpush
, but push multiple values.
Sourcepub async fn rpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
pub async fn rpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
Like rpush
, but push multiple values through a slice.
Sourcepub async fn blpop<K>(
&mut self,
lists: &[K],
timeout: u32,
) -> Result<Option<(Vec<u8>, Vec<u8>)>>
pub async fn blpop<K>( &mut self, lists: &[K], timeout: u32, ) -> Result<Option<(Vec<u8>, Vec<u8>)>>
Pop a value from one of the lists from the left side. Block timeout seconds when there are no values to pop. A zero-value with block infinitely.
§Return value
Ok(Some((list,value)))
: name of the list and corresponding valueOk(None)
: timeout (no values)Err(err)
: there was an error
Sourcepub async fn brpop<K>(
&mut self,
lists: &[K],
timeout: u32,
) -> Result<Option<(Vec<u8>, Vec<u8>)>>
pub async fn brpop<K>( &mut self, lists: &[K], timeout: u32, ) -> Result<Option<(Vec<u8>, Vec<u8>)>>
Pop a value from one of the lists from the right side. Block timeout seconds when there are no values to pop. A zero-value will block infinitely.
§Return value
Ok(Some((list,value)))
: name of the list and corresponding valueOk(None)
: timeout (no values)Err(err)
: there was an error
Sourcepub async fn lrange<K>(
&mut self,
list: K,
from: isize,
to: isize,
) -> Result<Vec<Vec<u8>>>
pub async fn lrange<K>( &mut self, list: K, from: isize, to: isize, ) -> Result<Vec<Vec<u8>>>
Get a series of elements from list
, from index from
to to
. If they are negative, take the
index from the right side of the list.
Sourcepub async fn llen<K>(&mut self, list: K) -> Result<Option<isize>>
pub async fn llen<K>(&mut self, list: K) -> Result<Option<isize>>
Get the number of elements in list
, or None
if the list doesn’t exist.
Sourcepub async fn lset<K, V>(
&mut self,
list: K,
index: usize,
value: V,
) -> Result<()>
pub async fn lset<K, V>( &mut self, list: K, index: usize, value: V, ) -> Result<()>
Set the value of the element at index
in list
to value
.
Sourcepub async fn ltrim<K>(
&mut self,
list: K,
start: usize,
stop: usize,
) -> Result<()>
pub async fn ltrim<K>( &mut self, list: K, start: usize, stop: usize, ) -> Result<()>
Trim list
from start
to stop
.
Sourcepub async fn incrbyfloat<K>(&mut self, key: K, val: f64) -> Result<f64>
pub async fn incrbyfloat<K>(&mut self, key: K, val: f64) -> Result<f64>
Sourcepub async fn mget<K>(&mut self, keys: &[K]) -> Result<Vec<Option<Vec<u8>>>>
pub async fn mget<K>(&mut self, keys: &[K]) -> Result<Vec<Option<Vec<u8>>>>
Get the string value for every key
, or `None`` if it doesn’t exist
Sourcepub async fn mset(&mut self, builder: MSetBuilder<'_>) -> Result<()>
pub async fn mset(&mut self, builder: MSetBuilder<'_>) -> Result<()>
Set every key in builder
to their respective values.
§Example
use darkredis::{Connection, MSetBuilder};
let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
let builder = MSetBuilder::new()
.set(b"multi-key-1", b"foo")
.set(b"multi-key-2", b"bar")
.set(b"multi-key-3", b"baz");
connection.mset(builder).await.unwrap();
let keys = &[b"multi-key-1", b"multi-key-2", b"multi-key-3"];
let results = connection.mget(keys).await.unwrap();
assert_eq!(
results,
vec![
Some(b"foo".to_vec()),
Some(b"bar".to_vec()),
Some(b"baz".to_vec())
]
);
Sourcepub async fn exists<K>(&mut self, key: K) -> Result<bool>
pub async fn exists<K>(&mut self, key: K) -> Result<bool>
Returns true if a key has been previously set.
Sourcepub async fn sadd<K, V>(&mut self, key: K, value: V) -> Result<bool>
pub async fn sadd<K, V>(&mut self, key: K, value: V) -> Result<bool>
Adds new value
to set specified by key
.
Sourcepub async fn sadd_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
pub async fn sadd_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
Like sadd
, but push multiple values.
Sourcepub async fn smembers<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
pub async fn smembers<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
Return the members of a set specified by key
.
Sourcepub async fn sismember<K, V>(&mut self, key: K, value: V) -> Result<bool>
pub async fn sismember<K, V>(&mut self, key: K, value: V) -> Result<bool>
Returns true
if value
belongs to a set specified by key
.
Sourcepub fn sscan<'a, K>(&'a mut self, key: &'a K) -> ScanBuilder<'_>
pub fn sscan<'a, K>(&'a mut self, key: &'a K) -> ScanBuilder<'_>
Scan for elements in a set.
§Return value
Returns a list of the elements which matched in the set.
§Example
use darkredis::Connection;
use futures::StreamExt;
let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
let key = b"test-set".to_vec();
connection.sadd(&key, "foo").await.unwrap();
connection.sadd(&key, "bar").await.unwrap();
let results = connection.sscan(&key).run()
.collect::<Vec<Vec<u8>>>().await;
assert!(results.contains(&b"foo".to_vec()));
assert!(results.contains(&b"bar".to_vec()));
Sourcepub fn scan(&mut self) -> ScanBuilder<'_>
pub fn scan(&mut self) -> ScanBuilder<'_>
Scan for keys in the database.
§Return Value
A stream of the matching keys.
§Example
use darkredis::Connection;
use futures::StreamExt;
let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
let key = b"locate-me".to_vec();
connection.set(&key, "dummy-value").await.unwrap();
let results = connection.scan().pattern(b"locate*").run()
.collect::<Vec<Vec<u8>>>().await;
assert!(results.contains(&key));
Sourcepub fn hscan<'a, K>(&'a mut self, key: &'a K) -> HScanBuilder<'a>
pub fn hscan<'a, K>(&'a mut self, key: &'a K) -> HScanBuilder<'a>
Scan for fields in the hash set at key
.
§Example
use darkredis::Connection;
use futures::StreamExt;
let mut connection = Connection::connect("127.0.0.1:6379").await.unwrap();
let key = b"hscan_test".to_vec();
connection.hset(&key, "one", "1").await.unwrap();
connection.hset(&key, "two", "2").await.unwrap();
connection.hset(&key, "three", "3").await.unwrap();
let results = connection.hscan(&key).run()
.collect::<Vec<(Vec<u8>, Vec<u8>)>>().await;
assert_eq!(results.len(), 3);
assert!(results.contains(&(b"one".to_vec(), b"1".to_vec())));
assert!(results.contains(&(b"two".to_vec(), b"2".to_vec())));
assert!(results.contains(&(b"three".to_vec(), b"3".to_vec())));
Sourcepub async fn key_type<K>(&mut self, key: K) -> Result<Option<DataType>>
pub async fn key_type<K>(&mut self, key: K) -> Result<Option<DataType>>
Get the Type of key
using the TYPE
command.
Sourcepub async fn scard<K>(&mut self, key: &K) -> Result<isize>
pub async fn scard<K>(&mut self, key: &K) -> Result<isize>
Get the number of members in the set at key
.
Sourcepub async fn smove<S, D, M>(
&mut self,
source: S,
destination: D,
member: M,
) -> Result<bool>
pub async fn smove<S, D, M>( &mut self, source: S, destination: D, member: M, ) -> Result<bool>
Sourcepub async fn srem_slice<K, M>(&mut self, key: K, members: &[M]) -> Result<isize>
pub async fn srem_slice<K, M>(&mut self, key: K, members: &[M]) -> Result<isize>
Remove every member in members
from the set at key
.
§Return value
The number of members which were removed.
Sourcepub async fn sdiff<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
pub async fn sdiff<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
Return the difference in members between the first set and all the other sets.
Sourcepub async fn sdiffstore<D, S>(
&mut self,
destination: D,
sets: &[S],
) -> Result<isize>
pub async fn sdiffstore<D, S>( &mut self, destination: D, sets: &[S], ) -> Result<isize>
Place the difference in members between the sets into destination
.
§Return value
The number of elements in destination
after the operation.
Sourcepub async fn sinter<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
pub async fn sinter<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
Return the members which are in every set.
Sourcepub async fn sinterstore<D, S>(
&mut self,
destination: D,
sets: &[S],
) -> Result<isize>
pub async fn sinterstore<D, S>( &mut self, destination: D, sets: &[S], ) -> Result<isize>
Create a new set at destination
containing the members which are part of all sets.
§Return value
The number of elements in destination
after the operation.
Sourcepub async fn srandmember<S>(
&mut self,
set: S,
count: isize,
) -> Result<Vec<Vec<u8>>>
pub async fn srandmember<S>( &mut self, set: S, count: isize, ) -> Result<Vec<Vec<u8>>>
Return a count
random members of set
. If count
is negative, the same element can show up
multiple times. See the Redis documentation for more info.
Sourcepub async fn spop<S>(&mut self, set: S, count: isize) -> Result<Vec<Vec<u8>>>
pub async fn spop<S>(&mut self, set: S, count: isize) -> Result<Vec<Vec<u8>>>
Pop count
random elements out of set
.
Trait Implementations§
Source§impl Clone for Connection
impl Clone for Connection
Source§fn clone(&self) -> Connection
fn clone(&self) -> Connection
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more