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
获取统计快照
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