pub struct RedisBackend { /* private fields */ }Expand description
Redis 分布式缓存后端
基于 redis crate 0.27 + tokio-comp 异步 IO + connection-manager 自动重连。
§实现要点
- 连接管理:使用
redis::aio::ConnectionManager(内部自动重连的连接池) get→redis::cmd("GET")异步执行set→SET key value+ 可选EX seconds(合并为单次SET命令,原子性保证)delete→redis::cmd("DEL")invalidate_prefix→SCAN+ 批量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
impl RedisBackend
Sourcepub async fn new(url: impl Into<String>) -> Result<Self, CacheError>
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 0redis://:secret@127.0.0.1:6379/1— 带密码,DB 1
§错误
- 连接失败 →
CacheError::Internal
Sourcepub fn from_manager(manager: ConnectionManager) -> Self
pub fn from_manager(manager: ConnectionManager) -> Self
使用已有 ConnectionManager 创建后端(用于复用连接池)
Trait Implementations§
Source§impl L2CacheBackend for RedisBackend
Available on crate feature redis only.
impl L2CacheBackend for RedisBackend
Available on crate feature
redis only.Source§fn 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 表示永不过期Source§fn invalidate_prefix<'a>(&'a self, prefix: &'a str) -> L2CacheFuture<'a, ()>
fn invalidate_prefix<'a>(&'a self, prefix: &'a str) -> L2CacheFuture<'a, ()>
按前缀批量失效缓存项(用于表级失效)
Auto Trait Implementations§
impl !RefUnwindSafe for RedisBackend
impl !UnwindSafe for RedisBackend
impl Freeze for RedisBackend
impl Send for RedisBackend
impl Sync for RedisBackend
impl Unpin for RedisBackend
impl UnsafeUnpin for RedisBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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