pub struct PreparedStatementCache { /* private fields */ }Expand description
PreparedStatement 句柄缓存
按连接 ID 分桶存储句柄,bucket 级别 LRU 淘汰。 句柄以闭包形式存储,避免在 sz-orm-core 暴露 SQLx 类型。
Implementations§
Source§impl PreparedStatementCache
impl PreparedStatementCache
Sourcepub fn new(max_size_per_conn: usize) -> Self
pub fn new(max_size_per_conn: usize) -> Self
创建新缓存,指定每连接最大缓存数
max_size_per_conn 默认 256(与 design.md 2.1.2 配置项一致)
Sourcepub async fn get_or_prepare(
&self,
conn_id: ConnId,
sql: &str,
params: &[Value],
) -> Result<PreparedLookup, DbError>
pub async fn get_or_prepare( &self, conn_id: ConnId, sql: &str, params: &[Value], ) -> Result<PreparedLookup, DbError>
查找或执行缓存句柄
- 命中:调用
execute_fn(params)返回PreparedLookup::Hit(rows) - 未命中:返回
PreparedLookup::Miss,调用方需 prepare 后调用store_handle - 执行错误:返回
Err(DbError),调用方应降级直接执行
Sourcepub fn store_handle(
&self,
conn_id: ConnId,
sql: &str,
tables: Vec<String>,
execute_fn: ExecuteFn,
)
pub fn store_handle( &self, conn_id: ConnId, sql: &str, tables: Vec<String>, execute_fn: ExecuteFn, )
存储句柄(prepare 后调用)
若 bucket 已满,LRU 淘汰最久未使用的条目。
Sourcepub fn invalidate_table(&self, table: &str) -> usize
pub fn invalidate_table(&self, table: &str) -> usize
表级失效:清除该表关联的所有句柄
返回失效条目数
Sourcepub fn invalidate_conn(&self, conn_id: ConnId) -> usize
pub fn invalidate_conn(&self, conn_id: ConnId) -> usize
连接级失效:清除该连接整个 bucket
连接关闭时调用,返回失效条目数
Sourcepub fn stats(&self) -> PreparedStatementCacheStatsSnapshot
pub fn stats(&self) -> PreparedStatementCacheStatsSnapshot
统计快照
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PreparedStatementCache
impl !RefUnwindSafe for PreparedStatementCache
impl !UnwindSafe for PreparedStatementCache
impl Send for PreparedStatementCache
impl Sync for PreparedStatementCache
impl Unpin for PreparedStatementCache
impl UnsafeUnpin for PreparedStatementCache
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