pub struct PlanCache { /* private fields */ }Expand description
查询计划缓存
双缓存架构:
parse_cache:SQL → AST(解析结果缓存)optimize_cache:SQL → 优化分析(优化结果缓存)
锁顺序约定:parse_cache → optimize_cache → access_order → table_index → stats (按此顺序加锁,避免死锁)
Implementations§
Source§impl PlanCache
impl PlanCache
Sourcepub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self
pub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self
创建新的查询计划缓存
max_size:最大缓存条目数(LRU 淘汰)default_ttl:默认 TTL(None 表示永不过期)
Sourcepub fn get_or_parse(&self, sql: &str) -> Result<Arc<Statement>, String>
pub fn get_or_parse(&self, sql: &str) -> Result<Arc<Statement>, String>
获取或解析 SQL
命中缓存时返回 AST + stats.parse_hits++ + LRU touch。 未命中时解析 SQL + 存入缓存 + stats.parse_misses++。
Sourcepub fn get_or_optimize(&self, sql: &str) -> Option<Arc<String>>
pub fn get_or_optimize(&self, sql: &str) -> Option<Arc<String>>
获取或优化 SQL
命中缓存时返回优化分析 + stats.optimize_hits++ + LRU touch。
未命中时返回 None + stats.optimize_misses++(调用方应执行优化后调用 store_optimize)。
Sourcepub fn store_optimize(&self, sql: &str, analysis: Arc<String>)
pub fn store_optimize(&self, sql: &str, analysis: Arc<String>)
存储优化结果
在 get_or_optimize 返回 None 后,调用方执行优化并存储结果。
Sourcepub fn invalidate_table(&self, table: &str) -> usize
pub fn invalidate_table(&self, table: &str) -> usize
表级精确失效
失效所有引用指定表的缓存条目,返回失效条目数。
Sourcepub fn invalidate_all(&self)
pub fn invalidate_all(&self)
全量清空缓存
Sourcepub fn stats(&self) -> PlanCacheStatsSnapshot
pub fn stats(&self) -> PlanCacheStatsSnapshot
获取统计快照
Source§impl PlanCache
impl PlanCache
Sourcepub fn eviction_count(&self) -> u64
pub fn eviction_count(&self) -> u64
v7.3.0 任务 1.5:淘汰数原子计数器
Sourcepub fn with_config(config: &PlanCacheConfig) -> Self
pub fn with_config(config: &PlanCacheConfig) -> Self
v7.3.0 任务 1.5:从 PlanCacheConfig 创建缓存
Sourcepub fn get_or_parse_with_types(
&self,
sql: &str,
param_types: &[DbType],
) -> Result<Arc<Statement>, String>
pub fn get_or_parse_with_types( &self, sql: &str, param_types: &[DbType], ) -> Result<Arc<Statement>, String>
v7.3.0 任务 1.5:带参数类型指纹的 get_or_parse
相同 SQL 不同参数类型生成不同指纹,避免错误命中。
Sourcepub fn access_count(&self, hash: u64) -> u32
pub fn access_count(&self, hash: u64) -> u32
v7.4.0 任务 3.3:获取 key 的访问次数(LRU-K 统计)
Sourcepub fn parse_hit_rate(&self) -> f64
pub fn parse_hit_rate(&self) -> f64
v7.4.0 任务 3.3:解析缓存命中率
Sourcepub fn adjust_capacity(&mut self) -> bool
pub fn adjust_capacity(&mut self) -> bool
v7.4.0 任务 3.3:自适应容量调整
根据命中率动态调整缓存大小:
- 命中率 > 80%:扩容(上限 max_capacity)
- 命中率 < 30%:缩容(下限 min_capacity)
- 其他:保持不变
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PlanCache
impl !RefUnwindSafe for PlanCache
impl Send for PlanCache
impl Sync for PlanCache
impl Unpin for PlanCache
impl UnsafeUnpin for PlanCache
impl UnwindSafe for PlanCache
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 more