Skip to main content

CacheDriver

Trait CacheDriver 

Source
pub trait CacheDriver: Send + Sync {
    // Required methods
    fn get_raw(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>;
    fn set_raw(
        &self,
        key: &str,
        value: Vec<u8>,
        ttl: Option<Duration>,
    ) -> Result<(), CacheError>;
    fn delete(&self, key: &str) -> Result<(), CacheError>;
    fn has(&self, key: &str) -> Result<bool, CacheError>;
    fn clear(&self) -> Result<(), CacheError>;

    // Provided methods
    fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError> { ... }
    fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError> { ... }
    fn get_cache_key(&self, name: &str) -> String { ... }
    fn get_tag_key(&self, tag: &str) -> String { ... }
    fn tag_append(
        &self,
        tag_key: &str,
        cache_key: &str,
    ) -> Result<(), CacheError> { ... }
    fn tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError> { ... }
    fn tag_clear(&self, keys: &[String]) -> Result<(), CacheError> { ... }
}
Expand description

缓存驱动 trait(对齐 PHP think\cache\Driver 抽象基类)

PHP think\cache\Driver(359 行)定义了缓存驱动的核心 API: get/set/delete/has/inc/dec/remember/pull/push/tag/clear

本 trait 提供等价的 Rust 抽象,底层由具体驱动(如 MemoryCacheDriver) 实现,上层 Cache facade 委托到 trait 方法。

§sz_rust_orm_facade::Cache 的关系

sz_rust_orm_facade::Cache trait 提供底层 KV 操作(get/set/delete/exists), 本 trait 在其之上扩展 PHP think-orm 行为:

  • inc/dec:自增/自减(不经序列化层)
  • has:键是否存在(对齐 PHP has,含 TTL 过期检查)
  • clear:清空所有缓存

Required Methods§

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

Provided Methods§

Source

fn inc(&self, key: &str, step: i64) -> Result<i64, CacheError>

自增(对齐 PHP inc,不经序列化层)

PHP Redis 驱动直接 INCRBY;File 驱动读取 → 加减 → 写回。 本 trait 默认实现采用 File 驱动行为。

§行为
  • 键不存在:初始化为 step,返回 step
  • 键存在:解析为 i64 → 加 step → 写回 → 返回新值
  • 解析失败:返回 CacheError
Source

fn dec(&self, key: &str, step: i64) -> Result<i64, CacheError>

自减(对齐 PHP dec,不经序列化层)

默认实现委托到 inc(key, -step)

Source

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

构造缓存 key(对齐 PHP Driver::getCacheKey

PHP Driver::getCacheKey(name) 第 86-89 行:

return $this->options['prefix'] . $name;

默认实现:无前缀(对齐 MemoryCacheDriver / MultiLevelCacheDriver)。 RedisCacheDriver 重写为 prefix + name

Source

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

构造 tag key(对齐 PHP Driver::getTagKey

PHP Driver::getTagKey(tag) 第 226-229 行:

return $this->options['tag_prefix'] . md5($tag);

默认实现:format!("tag:{}", compute_md5(tag))(PHP 默认 tag_prefix = "tag:")。

Source

fn tag_append(&self, tag_key: &str, cache_key: &str) -> Result<(), CacheError>

追加 cache_key 到标签集合(对齐 PHP Driver::append / Driver::push

PHP Driver::append(name, value) 调用 push(name, value)(第 140-143 行), push 实现(第 114-131 行):

public function push(string $name, $value): void
{
    $item = $this->get($name, []);
    if (!is_array($item)) {
        throw new InvalidArgumentException('only array cache can be push');
    }
    $item[] = $value;
    if (count($item) > 1000) {
        array_shift($item);
    }
    $item = array_unique($item);
    $this->set($name, $item);
}
§参数
  • tag_key:标签 key(getTagKey(tag) 返回值,应用 getCacheKey 前缀)
  • cache_key:缓存 key(getCacheKey(name) 返回值,应用前缀)
§行为

默认实现采用 File 驱动语义(push):

  1. 读取 getCacheKey(tag_key) → 反序列化为 Vec<String>
  2. 追加 cache_key
  3. 长度 > 1000 → 丢弃最旧(FIFO,对齐 array_shift
  4. 去重(对齐 array_unique,保留首次出现)
  5. 写回 getCacheKey(tag_key)

RedisCacheDriver 重写为 sAdd(Redis Set 语义)。

Source

fn tag_items(&self, tag: &str) -> Result<Vec<String>, CacheError>

获取标签包含的所有缓存 key(对齐 PHP Driver::getTagItems

PHP Driver::getTagItems(tag) 第 214-218 行:

public function getTagItems(string $tag): array
{
    $name = $this->getTagKey($tag);
    return $this->get($name, []);
}
§参数
  • tag:标签名(方法内部计算 getTagKey + 应用 getCacheKey 前缀)
§返回

返回该标签下所有缓存 key(已应用前缀)的列表。

Source

fn tag_clear(&self, keys: &[String]) -> Result<(), CacheError>

批量删除缓存 key(对齐 PHP Redis::clearTag

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

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

keys已应用前缀的缓存 key(来自 tag_items 返回值), 因此本方法不得再次应用 getCacheKey

默认实现:对每个 key 调用 delete(适用于无前缀驱动)。 RedisCacheDriver 重写为 del_many(raw delete,不应用前缀)。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§