Skip to main content

Strings

Struct Strings 

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

The keyspace every Redis string command works on.

Cheap to clone, and every clone is the same keyspace. Keys are byte strings the way Redis’s are, so anything that is bytes will do: "hits", a String, a &[u8] or a Vec<u8>.

let db = yo::open(yo::MEMORY)?;
let keys = db.strings();

keys.set("greeting", "hello")?;
assert_eq!(keys.get("greeting")?.as_deref(), Some(&b"hello"[..]));

assert_eq!(keys.incr("hits")?, 1);
assert_eq!(keys.incr_by("hits", 9)?, 10);

Implementations§

Source§

impl Strings

Source

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

Read a value.

Owned, because most callers want the bytes to outlive the call. Strings::with is the same read without the copy.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn with<R>( &self, key: impl AsRef<[u8]>, f: impl FnOnce(Str<'_>) -> R, ) -> Result<Option<R>>

Read a value without copying it, by handing what is in the record to f.

The view is Str, which is either the bytes where they lie or the integer an int encoded value holds. This is the read the G6 budget is about, and it allocates nothing at all.

let db = yo::open(yo::MEMORY)?;
let keys = db.strings();
keys.set("greeting", "hello")?;

assert_eq!(keys.with("greeting", |v| v.len())?, Some(5));
§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn exists(&self, key: impl AsRef<[u8]>) -> Result<bool>

Whether a key is there and has not expired.

§Errors

As Strings::get.

Source

pub fn len_of(&self, key: impl AsRef<[u8]>) -> Result<usize>

The length of a value in bytes, which is zero for a key that is not there. STRLEN.

§Errors

As Strings::get.

Source

pub fn set(&self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) -> Result<()>

Store a value, clearing any deadline the key had. Plain SET.

§Errors

Code::Full for a value past yo_kv::STRING_MAX.

Source

pub fn set_if_missing( &self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>, ) -> Result<bool>

Store a value only if the key is missing, and say whether it was. SET NX.

§Errors

As Strings::set.

Source

pub fn set_for( &self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>, ttl: Duration, ) -> Result<()>

Store a value that expires after ttl. SET PX.

This is the call that turns the clock on for this database, because it is the first moment a deadline can be observed.

§Errors

As Strings::set, and Code::Invalid for a ttl past what fits in a millisecond deadline.

Source

pub fn ttl(&self, key: impl AsRef<[u8]>) -> Result<Option<Duration>>

How long a key has left, or None if it has no deadline or is not there. PTTL.

Keys::ttl is the one that tells those two apart, and it works on a key of any type rather than only on a string.

§Errors

As Strings::get.

Source

pub fn replace( &self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>, ) -> Result<Option<Vec<u8>>>

Store a value and hand back what was there. GETSET.

§Errors

As Strings::set.

Source

pub fn take(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>>

Remove a key and hand back what it held. GETDEL.

§Errors

As Strings::get.

Source

pub fn del(&self, key: impl AsRef<[u8]>) -> Result<bool>

Remove a key, and say whether it was there. DEL.

§Errors

As Strings::get.

Source

pub fn set_many<K: AsRef<[u8]>, V: AsRef<[u8]>>( &self, pairs: &[(K, V)], ) -> Result<()>

Store several values, all of them or none. MSET.

The pairs reach the store as an iterator rather than a Vec, which is the same thing the wire layer does and for the same reason: MSET is on the gate list and an API that forces an allocation to call it is the wrong API.

let db = yo::open(yo::MEMORY)?;
let keys = db.strings();

keys.set_many(&[("a", "1"), ("b", "2")])?;
assert_eq!(keys.get("b")?.as_deref(), Some(&b"2"[..]));
§Errors

As Strings::set, and nothing is written if any pair fails.

Source

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

Read several values in one call. MGET.

§Errors

As Strings::get.

Source

pub fn incr(&self, key: impl AsRef<[u8]>) -> Result<i64>

Add one and hand back the result. INCR.

A key that is not there counts as zero, which is Redis’s rule and not a convenience: it is what makes a counter usable without a create step.

§Errors

Code::Invalid when the value is not an integer, or when adding would leave the range of an i64.

Source

pub fn incr_by(&self, key: impl AsRef<[u8]>, by: i64) -> Result<i64>

Add by and hand back the result. INCRBY, and DECRBY for a negative by.

§Errors

As Strings::incr.

Source

pub fn decr(&self, key: impl AsRef<[u8]>) -> Result<i64>

Subtract one and hand back the result. DECR.

§Errors

As Strings::incr.

Source

pub fn incr_by_float(&self, key: impl AsRef<[u8]>, by: f64) -> Result<f64>

Add by to a float counter and hand back the result. INCRBYFLOAT.

§Errors

Code::Invalid when the value is not a float, or when the result would be infinite or not a number.

Source

pub fn append( &self, key: impl AsRef<[u8]>, tail: impl AsRef<[u8]>, ) -> Result<usize>

Append to a value and hand back its new length. APPEND.

§Errors

As Strings::set.

Source

pub fn len(&self) -> Result<usize>

How many keys the keyspace holds, expired ones that nothing has touched yet included. DBSIZE.

§Errors

As Strings::get.

Source

pub fn is_empty(&self) -> Result<bool>

Whether the keyspace is empty.

§Errors

As Strings::get.

Source

pub fn expired_keys(&self) -> Result<u64>

Keys reclaimed by running into them after their deadline.

§Errors

As Strings::get.

Trait Implementations§

Source§

impl Clone for Strings

Source§

fn clone(&self) -> Strings

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Strings

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.