Skip to main content

RedisBackend

Struct RedisBackend 

Source
pub struct RedisBackend { /* private fields */ }
Expand description

Redis 分布式缓存后端

基于 redis crate 0.27 + tokio-comp 异步 IO + connection-manager 自动重连。

§实现要点

  • 连接管理:使用 redis::aio::ConnectionManager(内部自动重连的连接池)
  • getredis::cmd("GET") 异步执行
  • setSET key value + 可选 EX seconds(合并为单次 SET 命令,原子性保证)
  • deleteredis::cmd("DEL")
  • invalidate_prefixSCAN + 批量 DEL(避免 KEYS 阻塞 Redis 主线程)
    • 使用 COUNT 100 分批扫描,避免单次 SCAN 拉取过多 key 导致阻塞
    • 多次 DEL 调用合并为单次 pipeline 批量执行,减少 RTT 开销

§错误处理

  • 连接失败 → CacheError::Internal,由调用方决定是否重试
  • Redis 命令错误 → 原始错误字符串包装为 CacheError::Internal

§启用方式

Cargo.toml 中启用 redis feature:

[dependencies]
sz-orm-core = { version = "1.0", features = ["redis"] }

§使用示例

let backend = RedisBackend::new("redis://127.0.0.1:6379/0").await?;
backend.set("user:1", b"alice", Some(Duration::from_secs(60))).await?;
let val = backend.get("user:1").await?;
assert_eq!(val, Some(b"alice".to_vec()));
backend.delete("user:1").await?;

Implementations§

Source§

impl RedisBackend

Source

pub async fn new(url: impl Into<String>) -> Result<Self, CacheError>

创建 Redis 后端

url 格式:redis://[:password@]host:port[/db]

  • redis://127.0.0.1:6379/0 — 默认 DB 0
  • redis://:secret@127.0.0.1:6379/1 — 带密码,DB 1
§错误
  • 连接失败 → CacheError::Internal
Source

pub fn from_manager(manager: ConnectionManager) -> Self

使用已有 ConnectionManager 创建后端(用于复用连接池)

Trait Implementations§

Source§

impl L2CacheBackend for RedisBackend

Available on crate feature redis only.
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, ()>

按前缀批量失效缓存项(用于表级失效)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more