Skip to main content

PlanCache

Struct PlanCache 

Source
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

Source

pub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self

创建新的查询计划缓存

  • max_size:最大缓存条目数(LRU 淘汰)
  • default_ttl:默认 TTL(None 表示永不过期)
Source

pub fn get_or_parse(&self, sql: &str) -> Result<Arc<Statement>, String>

获取或解析 SQL

命中缓存时返回 AST + stats.parse_hits++ + LRU touch。 未命中时解析 SQL + 存入缓存 + stats.parse_misses++。

Source

pub fn get_or_optimize(&self, sql: &str) -> Option<Arc<String>>

获取或优化 SQL

命中缓存时返回优化分析 + stats.optimize_hits++ + LRU touch。 未命中时返回 None + stats.optimize_misses++(调用方应执行优化后调用 store_optimize)。

Source

pub fn store_optimize(&self, sql: &str, analysis: Arc<String>)

存储优化结果

get_or_optimize 返回 None 后,调用方执行优化并存储结果。

Source

pub fn invalidate_table(&self, table: &str) -> usize

表级精确失效

失效所有引用指定表的缓存条目,返回失效条目数。

Source

pub fn invalidate_all(&self)

全量清空缓存

Source

pub fn stats(&self) -> PlanCacheStatsSnapshot

获取统计快照

Source

pub fn size(&self) -> usize

当前缓存大小

Trait Implementations§

Source§

impl Default for PlanCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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