pub trait RedisBackend: Send + Sync {
// Required methods
fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>;
fn set(&self, key: &str, value: Vec<u8>) -> Result<(), CacheError>;
fn set_ex(
&self,
key: &str,
value: Vec<u8>,
ttl: Duration,
) -> Result<(), CacheError>;
fn del(&self, key: &str) -> Result<i64, CacheError>;
fn del_many(&self, keys: &[&str]) -> Result<i64, CacheError>;
fn exists(&self, key: &str) -> Result<bool, CacheError>;
fn incr_by(&self, key: &str, step: i64) -> Result<i64, CacheError>;
fn decr_by(&self, key: &str, step: i64) -> Result<i64, CacheError>;
fn flush_db(&self) -> Result<(), CacheError>;
fn sadd(&self, key: &str, member: &str) -> Result<i64, CacheError>;
fn smembers(&self, key: &str) -> Result<Vec<String>, CacheError>;
}Expand description
Redis 后端 trait(抽象 Redis 命令)
由于 Rust 端不强制依赖 redis crate,本 trait 抽象 Redis 命令,
允许应用层注入真实 Redis backend(如 redis::Connection 包装)。
§默认实现
MockRedisBackend 用 HashMap 模拟 Redis 行为,用于测试和开发环境。
§命令映射
| Trait 方法 | Redis 命令 | PHP phpredis 调用 |
|---|---|---|
get | GET | $handler->get($key) |
set | SET | $handler->set($key, $value) |
set_ex | SETEX | $handler->setex($key, $ttl, $v) |
del | DEL | $handler->del($key) |
del_many | DEL … | $handler->del($keys) |
exists | EXISTS | $handler->exists($key) |
incr_by | INCRBY | $handler->incrby($key, $step) |
decr_by | DECRBY | $handler->decrby($key, $step) |
flush_db | FLUSHDB | $handler->flushDB() |
sadd | SADD | $handler->sAdd($key, $value) |
smembers | SMEMBERS | $handler->sMembers($key) |
Required Methods§
Sourcefn set_ex(
&self,
key: &str,
value: Vec<u8>,
ttl: Duration,
) -> Result<(), CacheError>
fn set_ex( &self, key: &str, value: Vec<u8>, ttl: Duration, ) -> Result<(), CacheError>
SETEX 命令(带 TTL)
Sourcefn incr_by(&self, key: &str, step: i64) -> Result<i64, CacheError>
fn incr_by(&self, key: &str, step: i64) -> Result<i64, CacheError>
INCRBY 命令(key 不存在时 Redis 初始化为 0 再 INCRBY)
Sourcefn flush_db(&self) -> Result<(), CacheError>
fn flush_db(&self) -> Result<(), CacheError>
FLUSHDB 命令(清空当前 db)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".