skp_cache_core/traits/
backend.rs

1//! Cache backend trait
2
3use async_trait::async_trait;
4use crate::{CacheEntry, CacheError, CacheOptions, CacheStats};
5
6/// Core trait for all cache storage backends
7///
8/// This trait defines the operations that any cache backend must support.
9/// Implementations include in-memory caches, Redis, and multi-tier backends.
10#[async_trait]
11pub trait CacheBackend: Send + Sync + 'static {
12    /// Get a value from the cache
13    ///
14    /// Returns `None` if the key doesn't exist or has expired.
15    async fn get(&self, key: &str) -> Result<Option<CacheEntry<Vec<u8>>>, CacheError>;
16
17    /// Set a value in the cache
18    async fn set(
19        &self,
20        key: &str,
21        value: Vec<u8>,
22        options: &CacheOptions,
23    ) -> Result<(), CacheError>;
24
25    /// Delete a key from the cache
26    ///
27    /// Returns `true` if the key existed and was deleted.
28    async fn delete(&self, key: &str) -> Result<bool, CacheError>;
29
30    /// Check if a key exists in the cache
31    async fn exists(&self, key: &str) -> Result<bool, CacheError>;
32
33    /// Delete multiple keys
34    ///
35    /// Returns the number of keys that were deleted.
36    async fn delete_many(&self, keys: &[&str]) -> Result<u64, CacheError>;
37
38    /// Get multiple keys at once
39    ///
40    /// Returns a vector of results in the same order as the input keys.
41    async fn get_many(
42        &self,
43        keys: &[&str],
44    ) -> Result<Vec<Option<CacheEntry<Vec<u8>>>>, CacheError>;
45
46    /// Set multiple entries at once
47    async fn set_many(
48        &self,
49        entries: &[(&str, Vec<u8>, &CacheOptions)],
50    ) -> Result<(), CacheError>;
51
52    /// Clear all entries from the cache
53    async fn clear(&self) -> Result<(), CacheError>;
54
55    /// Get cache statistics
56    async fn stats(&self) -> Result<CacheStats, CacheError>;
57
58    /// Get the number of entries in the cache
59    async fn len(&self) -> Result<usize, CacheError>;
60
61    /// Check if the cache is empty
62    async fn is_empty(&self) -> Result<bool, CacheError> {
63        Ok(self.len().await? == 0)
64    }
65}
66
67/// Extended trait for backends that support tag-based operations
68#[async_trait]
69pub trait TaggableBackend: CacheBackend {
70    /// Get all keys with a specific tag
71    async fn get_by_tag(&self, tag: &str) -> Result<Vec<String>, CacheError>;
72
73    /// Delete all entries with a specific tag
74    async fn delete_by_tag(&self, tag: &str) -> Result<u64, CacheError>;
75}
76
77/// Extended trait for backends that support dependency tracking
78#[async_trait]
79pub trait DependencyBackend: CacheBackend {
80    /// Get keys that depend on the given key
81    /// 
82    /// If key `A` depends on key `B`, then `get_dependents("B")` should return `["A"]`.
83    async fn get_dependents(&self, key: &str) -> Result<Vec<String>, CacheError>;
84}
85
86/// Extended trait for distributed backends
87#[async_trait]
88pub trait DistributedBackend: CacheBackend {
89    /// Acquire a distributed lock
90    async fn acquire_lock(&self, key: &str, ttl: std::time::Duration) -> Result<String, CacheError>;
91
92    /// Release a distributed lock
93    async fn release_lock(&self, key: &str, token: &str) -> Result<bool, CacheError>;
94
95    /// Publish an invalidation message
96    async fn publish_invalidation(&self, keys: &[&str]) -> Result<(), CacheError>;
97
98    /// Subscribe to invalidation messages
99    async fn subscribe_invalidations(&self) -> Result<(), CacheError>;
100}