Skip to main content

RedisCacheDriver

Struct RedisCacheDriver 

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

Redis 缓存驱动(对齐 PHP think\cache\driver\Redis

基于 RedisBackend trait,提供 PHP think-orm Redis 驱动的等价实现。

§PHP 行为对齐

  1. set 行为(PHP 第 133-152 行):

    • expire > 0SETEX(key, expire, value)
    • expire = 0SET(key, value)
  2. inc/dec 行为(PHP 第 161-182 行):

    • 直接 INCRBY/DECRBY不经 serialize
    • key 不存在时 Redis 初始化为 0
    • 返回新值(i64)
  3. has 行为(PHP 第 100-103 行):

    • EXISTS(key) ? true : false
  4. delete 行为(PHP 第 190-197 行):

    • DEL(key) > 0 返回 true
  5. clear 行为(PHP 第 204-209 行):

    • FLUSHDB()
  6. append(tag)行为(PHP 第 230-234 行):

    • SADD(key, value) — 用 Set 存储 tag 成员
  7. getTagItems 行为(PHP 第 242-247 行):

    • SMEMBERS(key) — 返回 Set 全部成员
  8. clearTag 行为(PHP 第 217-221 行):

    • DEL(keys...) — 批量删除

§key 构造

  • getCacheKey(name) = prefix + name(对齐 PHP Driver::getCacheKey
  • getTagKey(tag) = tag_prefix + md5(tag)(对齐 PHP Driver::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

Source

pub fn new(config: RedisConfig) -> Self

创建 Redis 缓存驱动(用 Mock backend)

对齐 PHP new \think\cache\driver\Redis($options)

Source

pub fn with_backend(config: RedisConfig, backend: Box<dyn RedisBackend>) -> Self

创建 Redis 缓存驱动(自定义 backend)

应用层可注入真实 Redis backend(如 redis::Connection 包装)。

Source

pub fn config(&self) -> &RedisConfig

获取配置引用

Source

pub fn backend(&self) -> &dyn RedisBackend

获取 backend 引用(用于高级操作)

Source

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)。

Source

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);
}
Source

pub fn clear_tag(&self, keys: &[&str]) -> Result<(), CacheError>

删除缓存标签(对齐 PHP Redis::clearTag

PHP Redis::clearTag(keys) 第 217-221 行:

public function clearTag(array $keys): void
{
    $this->handler->del($keys);
}
Source

pub fn tag_key(&self, tag: &str) -> String

获取 tag key(公开接口,用于测试和调试)

Source

pub fn cache_key(&self, name: &str) -> String

获取 cache key(公开接口,用于测试和调试)

Trait Implementations§

Source§

impl CacheDriver for RedisCacheDriver

Source§

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>

重写 dec(对齐 PHP Redis::dec,直接 DECRBY,不经 serialize)

Source§

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

重写 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>

重写 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>

重写 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>

重写 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

Source§

fn get_raw(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>

读取缓存原始字节
Source§

fn set_raw( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<(), CacheError>

写入缓存原始字节
Source§

fn delete(&self, key: &str) -> Result<(), CacheError>

删除缓存
Source§

fn has(&self, key: &str) -> Result<bool, CacheError>

判断键是否存在(对齐 PHP has,含 TTL 过期检查)
Source§

fn clear(&self) -> Result<(), CacheError>

清空所有缓存(对齐 PHP clear

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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