ppoppo-infra 0.1.0

Backend-agnostic infrastructure traits for caching, queuing, and messaging
Documentation
//! 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>>;
}