pub trait Cache: Send + Sync {
// Required methods
fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>;
fn set(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<(), CacheError>;
fn delete(&self, key: &str) -> Result<(), CacheError>;
fn clear(&self) -> Result<(), CacheError>;
fn exists(&self, key: &str) -> Result<bool, CacheError>;
fn expire(&self, key: &str, ttl: Duration) -> Result<(), CacheError>;
fn ttl(&self, key: &str) -> Result<Option<Duration>, CacheError>;
}Required Methods§
fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>
fn set( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<(), CacheError>
fn delete(&self, key: &str) -> Result<(), CacheError>
fn clear(&self) -> Result<(), CacheError>
fn exists(&self, key: &str) -> Result<bool, CacheError>
fn expire(&self, key: &str, ttl: Duration) -> Result<(), CacheError>
fn ttl(&self, key: &str) -> Result<Option<Duration>, CacheError>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl Cache for L2Cache
为 L2Cache 实现 Cache trait
通过 CacheKey::by_pk("__cache__", key) 将字符串 key 映射到 L2Cache 的 CacheKey 体系,
所有通过 Cache trait 写入的缓存项归入 __cache__ 表,与业务缓存项隔离。
值以 Value::Bytes(Vec<u8>) 存储;若通过 Cache::get 读取到的 Value 非 Bytes 类型
(如直接通过 L2Cache::put 写入的其他类型),则回退为 JSON 序列化。