pub struct SchemaCache { /* private fields */ }Expand description
数据表字段缓存(对齐 PHP think\db\Fetch 字段缓存机制)
基于 Cache facade 实现表字段元数据的缓存,支持:
- 按 key 读写单表字段缓存
remember_schema缓存未命中时自动回源加载- 按表名清除单表缓存
- 按标签批量清除所有表字段缓存
§线程安全
SchemaCache 自身状态在构造后不可变(key_prefix、tag_name),
所有并发安全由底层 Cache 保证。SchemaCache 是 Send + Sync。
§缓存 key 格式
默认格式:schema_cache:<table_name>
可通过 SchemaCache::with_prefix 自定义前缀。
§标签批量清除
所有通过 SchemaCache::set_schema 写入的缓存 key 自动注册到标签
(标签名 = key 前缀),SchemaCache::clear 通过标签一次性清除所有
表字段缓存,不影响 Cache 中的其他缓存。
Implementations§
Source§impl SchemaCache
impl SchemaCache
Sourcepub fn with_prefix(cache: Arc<Cache>, prefix: impl Into<String>) -> Self
pub fn with_prefix(cache: Arc<Cache>, prefix: impl Into<String>) -> Self
创建 SchemaCache(使用自定义缓存 key 前缀)
用于多实例隔离场景,如不同数据库连接使用不同前缀:
schema_cache_db1:users、schema_cache_db2:users。
§参数
cache: Cache facade 实例prefix: 缓存 key 前缀(同时用作标签名)
Sourcepub fn get_schema(
&self,
table_name: &str,
) -> Result<Option<TableSchema>, SchemaCacheError>
pub fn get_schema( &self, table_name: &str, ) -> Result<Option<TableSchema>, SchemaCacheError>
Sourcepub fn remember_schema<F>(
&self,
table_name: &str,
loader: F,
) -> Result<TableSchema, SchemaCacheError>
pub fn remember_schema<F>( &self, table_name: &str, loader: F, ) -> Result<TableSchema, SchemaCacheError>
缓存未命中时调用 loader 加载并缓存(对齐 PHP Fetch::getFields 回源逻辑)
- 先从缓存读取,命中则直接返回
- 未命中时调用
loader(table_name)加载字段元数据 - 将 loader 返回的 schema 写入缓存(TTL = None,永不过期)
- 返回 schema
§参数
table_name: 表名loader: 回源加载闭包,接收表名,返回Result<TableSchema, SchemaCacheError>
Sourcepub fn set_schema(
&self,
table_name: &str,
schema: &TableSchema,
ttl: Option<Duration>,
) -> Result<(), SchemaCacheError>
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 默认行为)
Sourcepub fn forget_schema(&self, table_name: &str) -> Result<(), SchemaCacheError>
pub fn forget_schema(&self, table_name: &str) -> Result<(), SchemaCacheError>
Sourcepub fn clear(&self) -> Result<(), SchemaCacheError>
pub fn clear(&self) -> Result<(), SchemaCacheError>
清除所有表字段缓存(对齐 PHP Cache::clear() 批量语义)
通过标签批量清除所有 set_schema 写入的缓存 key,不影响 Cache 中的
其他缓存(如业务缓存、Session 等)。
Sourcepub fn has_schema(&self, table_name: &str) -> Result<bool, SchemaCacheError>
pub fn has_schema(&self, table_name: &str) -> Result<bool, SchemaCacheError>
Auto Trait Implementations§
impl !RefUnwindSafe for SchemaCache
impl !UnwindSafe for SchemaCache
impl Freeze for SchemaCache
impl Send for SchemaCache
impl Sync for SchemaCache
impl Unpin for SchemaCache
impl UnsafeUnpin for SchemaCache
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
Mutably borrows from an owned value. Read more
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> ⓘ
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 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> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.