pub struct PreparedStatementCache { /* private fields */ }Expand description
Connection-aware prepared statement cache
This cache tracks prepared statements per connection, ensuring that statement metadata is associated with the correct connection.
§Thread Safety
The cache uses RwLock for interior mutability, allowing concurrent
reads and exclusive writes. All operations are thread-safe.
§Connection Lifecycle
When a connection is returned to the pool or closed, its cache should
be cleared by calling clear_connection().
Implementations§
Source§impl PreparedStatementCache
impl PreparedStatementCache
Sourcepub fn with_config(config: PreparedCacheConfig) -> Self
pub fn with_config(config: PreparedCacheConfig) -> Self
Create a cache with custom configuration
Sourcepub fn get_connection_id(&self) -> ConnectionId
pub fn get_connection_id(&self) -> ConnectionId
Get or create a connection ID for a connection
This generates a unique ID for tracking the connection in the cache. The ID should be stored alongside the connection and used for all cache operations with that connection.
Sourcepub fn record_hit(&self, conn_id: ConnectionId, sql: &str)
pub fn record_hit(&self, conn_id: ConnectionId, sql: &str)
Record a cache hit for a statement
This should be called when a statement is found in SQLite’s internal cache or when the application determines a statement was reused.
§Arguments
conn_id- Connection identifiersql- SQL statement that was hit
Sourcepub fn record_miss(
&self,
conn_id: ConnectionId,
sql: &str,
prepare_time_us: u64,
)
pub fn record_miss( &self, conn_id: ConnectionId, sql: &str, prepare_time_us: u64, )
Record a cache miss for a statement
This should be called when a statement needs to be prepared.
§Arguments
conn_id- Connection identifiersql- SQL statement that was missedprepare_time_us- Time taken to prepare the statement (microseconds)
Sourcepub fn is_cached(&self, conn_id: ConnectionId, sql: &str) -> bool
pub fn is_cached(&self, conn_id: ConnectionId, sql: &str) -> bool
Sourcepub async fn get_or_prepare(
&self,
conn: &Connection,
sql: &str,
) -> Result<Statement, Error>
pub async fn get_or_prepare( &self, conn: &Connection, sql: &str, ) -> Result<Statement, Error>
Get a prepared statement or prepare it if not cached
This is a convenience method that generates a new connection ID for each call.
For proper connection-aware caching, use get_connection_id() and the
connection-specific methods instead.
§Arguments
conn- Database connection to prepare onsql- SQL statement to prepare
§Returns
The prepared statement
§Errors
Returns error if statement preparation fails
Sourcepub fn clear_connection(&self, conn_id: ConnectionId) -> usize
pub fn clear_connection(&self, conn_id: ConnectionId) -> usize
Sourcepub fn stats(&self) -> PreparedCacheStats
pub fn stats(&self) -> PreparedCacheStats
Get current cache statistics
Sourcepub fn total_size(&self) -> usize
pub fn total_size(&self) -> usize
Get current total cache size (across all connections)
Sourcepub fn connection_size(&self, conn_id: ConnectionId) -> usize
pub fn connection_size(&self, conn_id: ConnectionId) -> usize
Get cache size for a specific connection
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Get number of tracked connections
Sourcepub fn remove(&self, conn_id: ConnectionId, sql: &str) -> bool
pub fn remove(&self, conn_id: ConnectionId, sql: &str) -> bool
Remove a specific statement from a connection’s cache
Sourcepub fn cleanup_idle_connections(&self, max_idle_duration: Duration) -> usize
pub fn cleanup_idle_connections(&self, max_idle_duration: Duration) -> usize
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PreparedStatementCache
impl !RefUnwindSafe for PreparedStatementCache
impl Send for PreparedStatementCache
impl Sync for PreparedStatementCache
impl Unpin for PreparedStatementCache
impl UnsafeUnpin for PreparedStatementCache
impl UnwindSafe 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
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>
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>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.