Connection

Struct Connection 

Source
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

Source

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.

Source

pub async fn connect_and_auth<A, P>(address: A, password: P) -> Result<Self>
where A: ToSocketAddrs, P: AsRef<[u8]>,

Connect to a Redis instance running at address, and authenticate using password.

Source

pub async fn run_command(&mut self, command: Command<'_>) -> Result<Value>

Run a single command on this connection.

Source

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.

Source

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.

Source

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.

Source

pub async fn hdel<K, F>(&mut self, key: K, field: F) -> Result<bool>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Delete field from the hash set stored at key.

§Return value

true when the field was deleted, false if it didn’t exist

Source

pub async fn hdel_slice<K, F>(&mut self, key: K, fields: &[F]) -> Result<isize>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Delete every field in fields from the hash set stored at key.

§Return value

The number of deleted fields.

Source

pub async fn hexists<K, F>(&mut self, key: K, field: F) -> Result<bool>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Check if field exists in the hash set key.

Source

pub async fn hget<K, F>(&mut self, key: K, field: F) -> Result<Option<Vec<u8>>>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Get the value of field in the hash set at key.

Source

pub async fn hset<K, F, V>( &mut self, key: K, field: F, value: V, ) -> Result<isize>
where K: AsRef<[u8]>, F: AsRef<[u8]>, V: AsRef<[u8]>,

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).

Source

pub async fn hsetnx<K, F, V>( &mut self, key: K, field: F, value: V, ) -> Result<bool>
where K: AsRef<[u8]>, F: AsRef<[u8]>, V: AsRef<[u8]>,

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.

Source

pub async fn hset_many<K>( &mut self, key: K, builder: MSetBuilder<'_>, ) -> Result<isize>
where K: AsRef<[u8]>,

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.

Source

pub async fn hincrby<K, F>( &mut self, key: K, field: F, val: isize, ) -> Result<isize>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Increment field in the hash set key by val.

§Return value

The field value after the increment.

Source

pub async fn hincrbyfloat<K, F>( &mut self, key: K, field: F, val: f64, ) -> Result<f64>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Increment field in the hash set key by val, floating point version.

§Return value

The field value after the increment.

Source

pub async fn hkeys<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
where K: AsRef<[u8]>,

Get the name of each hash field stored at key.

Source

pub async fn hlen<K>(&mut self, key: K) -> Result<isize>
where K: AsRef<[u8]>,

Get the number of fields in the hash stored at key.

Source

pub async fn hstrlen<K, F>(&mut self, key: K, field: F) -> Result<isize>
where K: AsRef<[u8]>, F: AsRef<[u8]>,

Get the number of bytes in field in the hash set key

Source

pub async fn hvals<K>(&mut self, key: K) -> Result<Vec<Value>>
where K: AsRef<[u8]>,

Get the value of each field in the hash field stored at key.

Source

pub async fn ping(&mut self) -> Result<()>

Send a PING to the server, returning Ok(()) on success.

Source

pub async fn subscribe<K>(self, channels: &[K]) -> Result<MessageStream>
where K: AsRef<[u8]>,

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.

Source

pub async fn psubscribe<K>(self, patterns: &[K]) -> Result<PMessageStream>
where K: AsRef<[u8]>,

Exactly like subscribe, but subscribe to channels matching patterns instead.

Source

pub async fn publish<C, M>(&mut self, channel: C, message: M) -> Result<isize>
where C: AsRef<[u8]>, M: AsRef<[u8]>,

Publish message to channel.

§Return Value

Returns how many clients received the message.

Source

pub async fn set<K, D>(&mut self, key: K, value: D) -> Result<()>
where K: AsRef<[u8]>, D: AsRef<[u8]>,

Set key to value.

Source

pub async fn set_and_expire_seconds<K, D>( &mut self, key: K, data: D, seconds: u32, ) -> Result<()>
where K: AsRef<[u8]>, D: AsRef<[u8]>,

Set the key key to data, and set it to expire after seconds seconds.

Source

pub async fn set_and_expire_ms<K, D>( &mut self, key: K, data: D, milliseconds: u32, ) -> Result<()>
where K: AsRef<[u8]>, D: AsRef<[u8]>,

Set the key key to data, and set it to expire after milliseconds ms.

Source

pub async fn expire_seconds<K>(&mut self, key: K, seconds: u32) -> Result<isize>
where K: AsRef<[u8]>,

Set key to expire seconds seconds from now.

Source

pub async fn expire_ms<K>(&mut self, key: K, seconds: u32) -> Result<isize>
where K: AsRef<[u8]>,

Set key to expire milliseconds ms from now.

Source

pub async fn expire_at_seconds<K>( &mut self, key: K, timestamp: u64, ) -> Result<isize>
where K: AsRef<[u8]>,

Set key to expire at Unix timestamp timestamp, measured in seconds.

Source

pub async fn expire_at_ms<K>(&mut self, key: K, timestamp: u64) -> Result<isize>
where K: AsRef<[u8]>,

Set key to expire at Unix timestamp timestamp, measured in milliseconds.

Source

pub async fn del<K>(&mut self, key: K) -> Result<bool>
where K: AsRef<[u8]>,

Delete key.

§Return value

The number of deleted keys.

Source

pub async fn del_slice<K>(&mut self, keys: &[K]) -> Result<isize>
where K: AsRef<[u8]>,

Delete every element in keys.

§Return value

The number of deleted keys.

Source

pub async fn get<K>(&mut self, key: K) -> Result<Option<Vec<u8>>>
where K: AsRef<[u8]>,

Get the value of key.

Source

pub async fn lpush<K, V>(&mut self, list: K, value: V) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Push a value to list from the left.

§Return value

The number of elements in list

Source

pub async fn lpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Like lpush, but push multiple values.

Source

pub async fn rpush<K, V>(&mut self, list: K, value: V) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Push a value to list from the right.

§Return value

The number of elements in list

Source

pub async fn rpush_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Like rpush, but push multiple values through a slice.

Source

pub async fn lpop<K>(&mut self, list: K) -> Result<Option<Vec<u8>>>
where K: AsRef<[u8]>,

Pop a value from a list from the left side.

§Return value

The value popped from list

Source

pub async fn rpop<K>(&mut self, list: K) -> Result<Option<Vec<u8>>>
where K: AsRef<[u8]>,

Pop a value from a list from the right side.

§Return value

The value popped from list

Source

pub async fn blpop<K>( &mut self, lists: &[K], timeout: u32, ) -> Result<Option<(Vec<u8>, Vec<u8>)>>
where K: AsRef<[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 value
  • Ok(None): timeout (no values)
  • Err(err): there was an error
Source

pub async fn brpop<K>( &mut self, lists: &[K], timeout: u32, ) -> Result<Option<(Vec<u8>, Vec<u8>)>>
where K: AsRef<[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 value
  • Ok(None): timeout (no values)
  • Err(err): there was an error
Source

pub async fn lrange<K>( &mut self, list: K, from: isize, to: isize, ) -> Result<Vec<Vec<u8>>>
where K: AsRef<[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.

Source

pub async fn llen<K>(&mut self, list: K) -> Result<Option<isize>>
where K: AsRef<[u8]>,

Get the number of elements in list, or None if the list doesn’t exist.

Source

pub async fn lset<K, V>( &mut self, list: K, index: usize, value: V, ) -> Result<()>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Set the value of the element at index in list to value.

Source

pub async fn ltrim<K>( &mut self, list: K, start: usize, stop: usize, ) -> Result<()>
where K: AsRef<[u8]>,

Trim list from start to stop.

Source

pub async fn incr<K>(&mut self, key: K) -> Result<isize>
where K: AsRef<[u8]>,

Increment key by one.

§Return value

The new value of key.

Source

pub async fn incrby<K>(&mut self, key: K, val: isize) -> Result<isize>
where K: AsRef<[u8]>,

Increment key by val.

§Return value

The new value of key

Source

pub async fn incrbyfloat<K>(&mut self, key: K, val: f64) -> Result<f64>
where K: AsRef<[u8]>,

Increment key by a floating point value val.

§Return value

The new value of key

Source

pub async fn decr<K>(&mut self, key: K) -> Result<isize>
where K: AsRef<[u8]>,

Decrement key by a floating point value val.

§Return value

The new value of key

Source

pub async fn decrby<K>(&mut self, key: K, val: isize) -> Result<isize>
where K: AsRef<[u8]>,

Decrement key by val.

§Return value

The new value of key

Source

pub async fn append<K, V>(&mut self, key: K, val: V) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Append a string val to key.

§Return value

The new size of key

Source

pub async fn mget<K>(&mut self, keys: &[K]) -> Result<Vec<Option<Vec<u8>>>>
where K: AsRef<[u8]>,

Get the string value for every key, or `None`` if it doesn’t exist

Source

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())
     ]
 );
Source

pub async fn exists<K>(&mut self, key: K) -> Result<bool>
where K: AsRef<[u8]>,

Returns true if a key has been previously set.

Source

pub async fn sadd<K, V>(&mut self, key: K, value: V) -> Result<bool>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Adds new value to set specified by key.

Source

pub async fn sadd_slice<K, V>(&mut self, key: K, values: &[V]) -> Result<isize>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Like sadd, but push multiple values.

Source

pub async fn smembers<K>(&mut self, key: K) -> Result<Vec<Vec<u8>>>
where K: AsRef<[u8]>,

Return the members of a set specified by key.

Source

pub async fn sismember<K, V>(&mut self, key: K, value: V) -> Result<bool>
where K: AsRef<[u8]>, V: AsRef<[u8]>,

Returns true if value belongs to a set specified by key.

Source

pub fn sscan<'a, K>(&'a mut self, key: &'a K) -> ScanBuilder<'_>
where K: AsRef<[u8]>,

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()));
Source

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));
Source

pub fn hscan<'a, K>(&'a mut self, key: &'a K) -> HScanBuilder<'a>
where K: AsRef<[u8]>,

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())));
Source

pub async fn key_type<K>(&mut self, key: K) -> Result<Option<DataType>>
where K: AsRef<[u8]>,

Get the Type of key using the TYPE command.

Source

pub async fn scard<K>(&mut self, key: &K) -> Result<isize>
where K: AsRef<[u8]>,

Get the number of members in the set at key.

Source

pub async fn smove<S, D, M>( &mut self, source: S, destination: D, member: M, ) -> Result<bool>
where S: AsRef<[u8]>, M: AsRef<[u8]>, D: AsRef<[u8]>,

Move set member member from source to destination.

§Return value

true if the member was moved.

Source

pub async fn srem<K, M>(&mut self, key: K, member: M) -> Result<bool>
where K: AsRef<[u8]>, M: AsRef<[u8]>,

Remove set member member, from the set key.

§Return value

true if the member was removed.

Source

pub async fn srem_slice<K, M>(&mut self, key: K, members: &[M]) -> Result<isize>
where K: AsRef<[u8]>, M: AsRef<[u8]>,

Remove every member in members from the set at key.

§Return value

The number of members which were removed.

Source

pub async fn sdiff<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
where S: AsRef<[u8]>,

Return the difference in members between the first set and all the other sets.

Source

pub async fn sdiffstore<D, S>( &mut self, destination: D, sets: &[S], ) -> Result<isize>
where D: AsRef<[u8]>, S: AsRef<[u8]>,

Place the difference in members between the sets into destination.

§Return value

The number of elements in destination after the operation.

Source

pub async fn sinter<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
where S: AsRef<[u8]>,

Return the members which are in every set.

Source

pub async fn sinterstore<D, S>( &mut self, destination: D, sets: &[S], ) -> Result<isize>
where D: AsRef<[u8]>, S: AsRef<[u8]>,

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.

Source

pub async fn srandmember<S>( &mut self, set: S, count: isize, ) -> Result<Vec<Vec<u8>>>
where S: AsRef<[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.

Source

pub async fn spop<S>(&mut self, set: S, count: isize) -> Result<Vec<Vec<u8>>>
where S: AsRef<[u8]>,

Pop count random elements out of set.

Source

pub async fn sunion<S>(&mut self, sets: &[S]) -> Result<Vec<Vec<u8>>>
where S: AsRef<[u8]>,

Return the union of every set in sets.

Source

pub async fn sunionstore<D, S>( &mut self, destination: D, sets: &[S], ) -> Result<isize>
where D: AsRef<[u8]>, S: AsRef<[u8]>,

Store the union of sets in destination.

§Return value

The number of elements in destination after the operation.

Trait Implementations§

Source§

impl Clone for Connection

Source§

fn clone(&self) -> Connection

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Connection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.