Skip to main content

SchemaCache

Struct SchemaCache 

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

数据表字段缓存(对齐 PHP think\db\Fetch 字段缓存机制)

基于 Cache facade 实现表字段元数据的缓存,支持:

  • 按 key 读写单表字段缓存
  • remember_schema 缓存未命中时自动回源加载
  • 按表名清除单表缓存
  • 按标签批量清除所有表字段缓存

§线程安全

SchemaCache 自身状态在构造后不可变(key_prefixtag_name), 所有并发安全由底层 Cache 保证。SchemaCacheSend + Sync

§缓存 key 格式

默认格式:schema_cache:<table_name>

可通过 SchemaCache::with_prefix 自定义前缀。

§标签批量清除

所有通过 SchemaCache::set_schema 写入的缓存 key 自动注册到标签 (标签名 = key 前缀),SchemaCache::clear 通过标签一次性清除所有 表字段缓存,不影响 Cache 中的其他缓存。

Implementations§

Source§

impl SchemaCache

Source

pub fn new(cache: Arc<Cache>) -> Self

创建 SchemaCache(使用默认前缀 schema_cache

§参数
  • cache: Cache facade 实例(Arc<Cache>
Source

pub fn with_prefix(cache: Arc<Cache>, prefix: impl Into<String>) -> Self

创建 SchemaCache(使用自定义缓存 key 前缀)

用于多实例隔离场景,如不同数据库连接使用不同前缀: schema_cache_db1:usersschema_cache_db2:users

§参数
  • cache: Cache facade 实例
  • prefix: 缓存 key 前缀(同时用作标签名)
Source

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

构造完整缓存 key({prefix}:{table_name}

对齐 PHP Fetch::getFieldCacheKey($tableName)

§参数
  • table_name: 表名
Source

pub fn get_schema( &self, table_name: &str, ) -> Result<Option<TableSchema>, SchemaCacheError>

从缓存获取表字段元数据(对齐 PHP Fetch::getFields 缓存读取)

§参数
  • table_name: 表名
§返回
  • Ok(Some(schema)): 缓存命中
  • Ok(None): 缓存未命中或已过期
Source

pub fn remember_schema<F>( &self, table_name: &str, loader: F, ) -> Result<TableSchema, SchemaCacheError>

缓存未命中时调用 loader 加载并缓存(对齐 PHP Fetch::getFields 回源逻辑)

  1. 先从缓存读取,命中则直接返回
  2. 未命中时调用 loader(table_name) 加载字段元数据
  3. 将 loader 返回的 schema 写入缓存(TTL = None,永不过期)
  4. 返回 schema
§参数
  • table_name: 表名
  • loader: 回源加载闭包,接收表名,返回 Result<TableSchema, SchemaCacheError>
Source

pub fn set_schema( &self, table_name: &str, schema: &TableSchema, ttl: Option<Duration>, ) -> Result<(), SchemaCacheError>

写入表字段缓存(对齐 PHP Fetch::setFieldCache

缓存 key 自动注册到标签,支持 SchemaCache::clear 批量清除。

§参数
  • table_name: 表名
  • schema: 表字段元数据
  • ttl: 过期时间(None 永不过期,对齐 PHP 默认行为)
Source

pub fn forget_schema(&self, table_name: &str) -> Result<(), SchemaCacheError>

清除单表字段缓存(对齐 PHP Cache::delete($key)

§参数
  • table_name: 表名
Source

pub fn clear(&self) -> Result<(), SchemaCacheError>

清除所有表字段缓存(对齐 PHP Cache::clear() 批量语义)

通过标签批量清除所有 set_schema 写入的缓存 key,不影响 Cache 中的 其他缓存(如业务缓存、Session 等)。

Source

pub fn has_schema(&self, table_name: &str) -> Result<bool, SchemaCacheError>

判断表字段缓存是否存在(对齐 PHP Cache::has($key)

§参数
  • table_name: 表名

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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