pub struct RedisCacheDriver { /* private fields */ }Expand description
Redis 缓存驱动(对齐 PHP think\cache\driver\Redis)
基于 RedisBackend trait,提供 PHP think-orm Redis 驱动的等价实现。
§PHP 行为对齐
-
set行为(PHP 第 133-152 行):expire > 0→SETEX(key, expire, value)expire = 0→SET(key, value)
-
inc/dec行为(PHP 第 161-182 行):- 直接
INCRBY/DECRBY,不经 serialize - key 不存在时 Redis 初始化为 0
- 返回新值(i64)
- 直接
-
has行为(PHP 第 100-103 行):EXISTS(key)? true : false
-
delete行为(PHP 第 190-197 行):DEL(key)> 0 返回 true
-
clear行为(PHP 第 204-209 行):FLUSHDB()
-
append(tag)行为(PHP 第 230-234 行):SADD(key, value)— 用 Set 存储 tag 成员
-
getTagItems行为(PHP 第 242-247 行):SMEMBERS(key)— 返回 Set 全部成员
-
clearTag行为(PHP 第 217-221 行):DEL(keys...)— 批量删除
§key 构造
getCacheKey(name)=prefix + name(对齐 PHPDriver::getCacheKey)getTagKey(tag)=tag_prefix + md5(tag)(对齐 PHPDriver::getTagKey)
§使用示例
use sz_rust_cache_facade::{RedisCacheDriver, RedisConfig, Cache};
let driver = RedisCacheDriver::new(RedisConfig::default());
let cache = Cache::new();
cache.register_store("redis", Box::new(driver));
cache.set_default_store("redis").unwrap();
cache.set("key", "value", None).unwrap();
assert_eq!(cache.get::<String>("key").unwrap(), Some("value".to_string()));Implementations§
Source§impl RedisCacheDriver
impl RedisCacheDriver
Sourcepub fn new(config: RedisConfig) -> Self
pub fn new(config: RedisConfig) -> Self
创建 Redis 缓存驱动(用 Mock backend)
对齐 PHP new \think\cache\driver\Redis($options)。
Sourcepub fn with_backend(config: RedisConfig, backend: Box<dyn RedisBackend>) -> Self
pub fn with_backend(config: RedisConfig, backend: Box<dyn RedisBackend>) -> Self
创建 Redis 缓存驱动(自定义 backend)
应用层可注入真实 Redis backend(如 redis::Connection 包装)。
Sourcepub fn config(&self) -> &RedisConfig
pub fn config(&self) -> &RedisConfig
获取配置引用
Sourcepub fn backend(&self) -> &dyn RedisBackend
pub fn backend(&self) -> &dyn RedisBackend
获取 backend 引用(用于高级操作)
Sourcepub fn append(&self, name: &str, value: &str) -> Result<(), CacheError>
pub fn append(&self, name: &str, value: &str) -> Result<(), CacheError>
追加 TagSet 数据(对齐 PHP Redis::append)
PHP Redis::append(name, value) 第 230-234 行:
public function append(string $name, $value): void
{
$key = $this->getCacheKey($name);
$this->handler->sAdd($key, $value);
}注意:PHP think\cache\driver\Redis 重写了父类 Driver::append
(父类用 push,Redis 驱动用 SADD)。
Sourcepub fn get_tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
pub fn get_tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
获取标签包含的缓存标识(对齐 PHP Redis::getTagItems)
PHP Redis::getTagItems(tag) 第 242-247 行:
public function getTagItems(string $tag): array
{
$name = $this->getTagKey($tag);
$key = $this->getCacheKey($name);
return $this->handler->sMembers($key);
}Trait Implementations§
Source§impl CacheDriver for RedisCacheDriver
impl CacheDriver for RedisCacheDriver
Source§fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError>
fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError>
重写 inc(对齐 PHP Redis::inc,直接 INCRBY,不经 serialize)
PHP Redis::inc(name, step) 第 161-167 行:
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->incrby($key, $step);
}关键差异:File 驱动读取 → 加减 → 写回(经 serialize 层); Redis 驱动直接 INCRBY(不经 serialize)。Redis 自身处理 key 不存在 的情况(初始化为 0)。
Source§fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError>
fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError>
重写 dec(对齐 PHP Redis::dec,直接 DECRBY,不经 serialize)
Source§fn get_cache_key(&self, name: &str) -> String
fn get_cache_key(&self, name: &str) -> String
重写 getCacheKey(对齐 PHP Driver::getCacheKey)
PHP: return $this->options['prefix'] . $name;
Source§fn get_tag_key(&self, tag: &str) -> String
fn get_tag_key(&self, tag: &str) -> String
重写 getTagKey(对齐 PHP Driver::getTagKey)
PHP: return $this->options['tag_prefix'] . md5($tag);
Source§fn tag_append(&self, tag_key: &str, cache_key: &str) -> Result<(), CacheError>
fn tag_append(&self, tag_key: &str, cache_key: &str) -> Result<(), CacheError>
重写 tag_append(对齐 PHP Redis::append,使用 sAdd 而非 push)
PHP Redis::append(name, value) 第 230-234 行:
public function append(string $name, $value): void
{
$key = $this->getCacheKey($name);
$this->handler->sAdd($key, $value);
}关键差异:PHP think\cache\driver\Redis 重写了父类 Driver::append
(父类用 push = get→append→set,Redis 驱动用 SADD = 原子 Set 操作)。
Source§fn tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
fn tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>
重写 tag_items(对齐 PHP Redis::getTagItems,使用 sMembers)
PHP Redis::getTagItems(tag) 第 242-247 行:
public function getTagItems(string $tag): array
{
$name = $this->getTagKey($tag);
$key = $this->getCacheKey($name);
return $this->handler->sMembers($key);
}Source§fn tag_clear(&self, keys: &[String]) -> Result<(), CacheError>
fn tag_clear(&self, keys: &[String]) -> Result<(), CacheError>
重写 tag_clear(对齐 PHP Redis::clearTag,raw del 不应用前缀)
PHP Redis::clearTag(keys) 第 217-221 行:
public function clearTag(array $keys): void
{
$this->handler->del($keys);
}关键:keys 已是 prefix + name 格式(来自 tag_items 返回值),
因此直接 del_many,不得再次应用 getCacheKey。
Auto Trait Implementations§
impl !RefUnwindSafe for RedisCacheDriver
impl !UnwindSafe for RedisCacheDriver
impl Freeze for RedisCacheDriver
impl Send for RedisCacheDriver
impl Sync for RedisCacheDriver
impl Unpin for RedisCacheDriver
impl UnsafeUnpin for RedisCacheDriver
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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> ⓘ
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