pub trait L2CacheBackend: Send + Sync {
// Required methods
fn get<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, Option<Vec<u8>>>;
fn set<'a>(
&'a self,
key: &'a str,
value: &'a [u8],
ttl: Option<Duration>,
) -> L2CacheFuture<'a, ()>;
fn delete<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, ()>;
fn invalidate_prefix<'a>(&'a self, prefix: &'a str) -> L2CacheFuture<'a, ()>;
}Expand description
L2 缓存后端 trait(分布式抽象)
定义跨进程共享的二级缓存后端接口,支持进程内内存、Redis 等实现。
手动解糖 async 方法(不使用 #[async_trait]),与 Connection trait 风格一致。
§设计要点
- 键值以
&[u8]传输:后端无关的序列化格式(由调用方决定 bincode/json 等) - TTL 可选:
Some(Duration)设置过期时间,None表示永不过期 - 前缀失效:
invalidate_prefix批量失效某前缀的所有键(用于表级失效)
§实现方
InMemoryBackend:进程内内存后端(默认,单机场景)RedisBackend:Redis 分布式后端(stub,需启用redisfeature 并补充依赖)
Required Methods§
Sourcefn set<'a>(
&'a self,
key: &'a str,
value: &'a [u8],
ttl: Option<Duration>,
) -> L2CacheFuture<'a, ()>
fn set<'a>( &'a self, key: &'a str, value: &'a [u8], ttl: Option<Duration>, ) -> L2CacheFuture<'a, ()>
设置缓存值,ttl 为 None 表示永不过期
Sourcefn delete<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, ()>
fn delete<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, ()>
删除单个缓存键
Sourcefn invalidate_prefix<'a>(&'a self, prefix: &'a str) -> L2CacheFuture<'a, ()>
fn invalidate_prefix<'a>(&'a self, prefix: &'a str) -> L2CacheFuture<'a, ()>
按前缀批量失效缓存项(用于表级失效)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl L2CacheBackend for InMemoryBackend
impl L2CacheBackend for RedisBackend
Available on crate feature
redis only.