Skip to main content

ferro_rs/cache/
store.rs

1//! Cache store trait definition
2//!
3//! Defines the contract for cache implementations (Redis, InMemory, etc.)
4
5use async_trait::async_trait;
6use std::time::Duration;
7
8use crate::error::FrameworkError;
9
10/// Cache store trait - all cache backends must implement this
11///
12/// This trait uses JSON strings for values to enable dynamic typing.
13/// The `Cache` facade handles serialization/deserialization.
14#[async_trait]
15pub trait CacheStore: Send + Sync {
16    /// Retrieve a raw JSON value from the cache by key
17    async fn get_raw(&self, key: &str) -> Result<Option<String>, FrameworkError>;
18
19    /// Store a raw JSON value in the cache
20    async fn put_raw(
21        &self,
22        key: &str,
23        value: &str,
24        ttl: Option<Duration>,
25    ) -> Result<(), FrameworkError>;
26
27    /// Check if a key exists in the cache
28    async fn has(&self, key: &str) -> Result<bool, FrameworkError>;
29
30    /// Remove an item from the cache
31    async fn forget(&self, key: &str) -> Result<bool, FrameworkError>;
32
33    /// Remove all items from the cache
34    async fn flush(&self) -> Result<(), FrameworkError>;
35
36    /// Increment a numeric value
37    ///
38    /// Returns the new value after incrementing.
39    async fn increment(&self, key: &str, amount: i64) -> Result<i64, FrameworkError>;
40
41    /// Decrement a numeric value
42    ///
43    /// Returns the new value after decrementing.
44    async fn decrement(&self, key: &str, amount: i64) -> Result<i64, FrameworkError>;
45
46    /// Set a TTL on an existing key
47    ///
48    /// Returns `true` if the key existed and TTL was set, `false` if key not found.
49    async fn expire(&self, key: &str, ttl: Duration) -> Result<bool, FrameworkError>;
50}