skp_cache_core/traits/
backend.rs1use async_trait::async_trait;
4use crate::{CacheEntry, CacheError, CacheOptions, CacheStats};
5
6#[async_trait]
11pub trait CacheBackend: Send + Sync + 'static {
12 async fn get(&self, key: &str) -> Result<Option<CacheEntry<Vec<u8>>>, CacheError>;
16
17 async fn set(
19 &self,
20 key: &str,
21 value: Vec<u8>,
22 options: &CacheOptions,
23 ) -> Result<(), CacheError>;
24
25 async fn delete(&self, key: &str) -> Result<bool, CacheError>;
29
30 async fn exists(&self, key: &str) -> Result<bool, CacheError>;
32
33 async fn delete_many(&self, keys: &[&str]) -> Result<u64, CacheError>;
37
38 async fn get_many(
42 &self,
43 keys: &[&str],
44 ) -> Result<Vec<Option<CacheEntry<Vec<u8>>>>, CacheError>;
45
46 async fn set_many(
48 &self,
49 entries: &[(&str, Vec<u8>, &CacheOptions)],
50 ) -> Result<(), CacheError>;
51
52 async fn clear(&self) -> Result<(), CacheError>;
54
55 async fn stats(&self) -> Result<CacheStats, CacheError>;
57
58 async fn len(&self) -> Result<usize, CacheError>;
60
61 async fn is_empty(&self) -> Result<bool, CacheError> {
63 Ok(self.len().await? == 0)
64 }
65}
66
67#[async_trait]
69pub trait TaggableBackend: CacheBackend {
70 async fn get_by_tag(&self, tag: &str) -> Result<Vec<String>, CacheError>;
72
73 async fn delete_by_tag(&self, tag: &str) -> Result<u64, CacheError>;
75}
76
77#[async_trait]
79pub trait DependencyBackend: CacheBackend {
80 async fn get_dependents(&self, key: &str) -> Result<Vec<String>, CacheError>;
84}
85
86#[async_trait]
88pub trait DistributedBackend: CacheBackend {
89 async fn acquire_lock(&self, key: &str, ttl: std::time::Duration) -> Result<String, CacheError>;
91
92 async fn release_lock(&self, key: &str, token: &str) -> Result<bool, CacheError>;
94
95 async fn publish_invalidation(&self, keys: &[&str]) -> Result<(), CacheError>;
97
98 async fn subscribe_invalidations(&self) -> Result<(), CacheError>;
100}