Skip to main content

L2CacheBackend

Trait L2CacheBackend 

Source
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,需启用 redis feature 并补充依赖)

Required Methods§

Source

fn get<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, Option<Vec<u8>>>

获取缓存值,不存在或已过期返回 None

Source

fn set<'a>( &'a self, key: &'a str, value: &'a [u8], ttl: Option<Duration>, ) -> L2CacheFuture<'a, ()>

设置缓存值,ttlNone 表示永不过期

Source

fn delete<'a>(&'a self, key: &'a str) -> L2CacheFuture<'a, ()>

删除单个缓存键

Source

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§

Source§

impl L2CacheBackend for InMemoryBackend

Source§

impl L2CacheBackend for RedisBackend

Available on crate feature redis only.