pub struct Counter { /* private fields */ }Expand description
A counter at one key.
Sugar over crate::keyspace::Strings, and the kind of sugar that is worth
having: counting is the commonest thing a string key is used for, and a
handle that holds the key means the key is spelled once instead of at every
call site, which is one fewer place for a typo to live.
A counter that has never been written reads as zero, the way Redis’s does,
so there is no create step and nothing to check before the first incr.
let db = yo::open(yo::MEMORY)?;
let hits = db.counter("hits");
assert_eq!(hits.get()?, 0);
assert_eq!(hits.incr()?, 1);
assert_eq!(hits.add(41)?, 42);Implementations§
Source§impl Counter
impl Counter
Sourcepub fn get(&self) -> Result<i64>
pub fn get(&self) -> Result<i64>
What the counter reads, which is zero if nothing has written it yet.
§Errors
Code::Invalid if the key holds something that is not an integer.
Sourcepub fn incr(&self) -> Result<i64>
pub fn incr(&self) -> Result<i64>
Add one and hand back the result. INCR.
§Errors
Code::Invalid if the key holds something that is not an integer, or
if the result would leave the range of an i64.
Sourcepub fn add(&self, by: i64) -> Result<i64>
pub fn add(&self, by: i64) -> Result<i64>
Add by and hand back the result, which is DECRBY for a negative
by. INCRBY.
§Errors
As Counter::incr.