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
impl Strings
Sourcepub fn get(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>>
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.
Sourcepub fn with<R>(
&self,
key: impl AsRef<[u8]>,
f: impl FnOnce(Str<'_>) -> R,
) -> Result<Option<R>>
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.
Sourcepub fn len_of(&self, key: impl AsRef<[u8]>) -> Result<usize>
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.
Sourcepub fn set(&self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>) -> Result<()>
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.
Sourcepub fn set_if_missing(
&self,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Result<bool>
pub fn set_if_missing( &self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>, ) -> Result<bool>
Sourcepub fn set_for(
&self,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
ttl: Duration,
) -> Result<()>
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.
Sourcepub fn ttl(&self, key: impl AsRef<[u8]>) -> Result<Option<Duration>>
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.
Sourcepub fn replace(
&self,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Result<Option<Vec<u8>>>
pub fn replace( &self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>, ) -> Result<Option<Vec<u8>>>
Sourcepub fn set_many<K: AsRef<[u8]>, V: AsRef<[u8]>>(
&self,
pairs: &[(K, V)],
) -> Result<()>
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.
Sourcepub fn incr(&self, key: impl AsRef<[u8]>) -> Result<i64>
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.
Sourcepub fn incr_by_float(&self, key: impl AsRef<[u8]>, by: f64) -> Result<f64>
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.
Sourcepub fn len(&self) -> Result<usize>
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.