1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Backend-agnostic counter trait.
use async_trait::async_trait;
use crate::Result;
/// Atomic counter operations for permanent cumulative totals.
#[async_trait]
pub trait Counter: Send + Sync {
/// Increment counter by delta and return new value.
///
/// Creates counter with initial value if it doesn't exist.
async fn incr(&self, key: &str, delta: i64) -> Result<i64>;
/// Decrement counter by delta and return new value.
async fn decr(&self, key: &str, delta: i64) -> Result<i64>;
/// Get current counter value. Returns 0 if counter doesn't exist.
async fn get(&self, key: &str) -> Result<i64>;
/// Set counter to specific value.
async fn set(&self, key: &str, value: i64) -> Result<()>;
/// Delete a counter. Returns true if counter existed.
async fn del(&self, key: &str) -> Result<bool>;
/// Get multiple counter values atomically.
///
/// Returns (key, value) pairs, with 0 for non-existent keys.
async fn mget(&self, keys: &[&str]) -> Result<Vec<(String, i64)>>;
/// Increment multiple counters atomically.
///
/// Returns new values for all counters.
async fn mincr(&self, entries: &[(&str, i64)]) -> Result<Vec<(String, i64)>>;
/// Increment counter only if the result would not exceed a maximum.
///
/// Returns `Some(new_value)` if succeeded, `None` if would exceed max.
async fn incr_if_below(
&self,
key: &str,
delta: i64,
max_value: i64,
) -> Result<Option<i64>>;
/// Find keys matching a pattern (SQL LIKE syntax).
async fn keys(&self, pattern: &str, limit: i32) -> Result<Vec<String>>;
}