Skip to main content

PreparedStatementCache

Struct PreparedStatementCache 

Source
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

Source

pub fn new(max_size: usize) -> Self

Create a new connection-aware prepared statement cache

§Arguments
  • max_size - Maximum number of statements to cache per connection
§Example
use do_memory_storage_turso::prepared::PreparedStatementCache;

let cache = PreparedStatementCache::new(100);
Source

pub fn with_config(config: PreparedCacheConfig) -> Self

Create a cache with custom configuration

Source

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.

Source

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 identifier
  • sql - SQL statement that was hit
Source

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 identifier
  • sql - SQL statement that was missed
  • prepare_time_us - Time taken to prepare the statement (microseconds)
Source

pub fn is_cached(&self, conn_id: ConnectionId, sql: &str) -> bool

Check if a statement is cached for a connection

§Arguments
  • conn_id - Connection identifier
  • sql - SQL statement to check
§Returns

true if the statement is cached and doesn’t need refresh

Source

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 on
  • sql - SQL statement to prepare
§Returns

The prepared statement

§Errors

Returns error if statement preparation fails

Source

pub fn clear_connection(&self, conn_id: ConnectionId) -> usize

Clear all cached statements for a specific connection

This should be called when a connection is returned to the pool or closed to prevent memory leaks.

§Arguments
  • conn_id - Connection identifier to clear
§Returns

Number of statements cleared

Source

pub fn clear(&self)

Clear all cached statements across all connections

Source

pub fn stats(&self) -> PreparedCacheStats

Get current cache statistics

Source

pub fn total_size(&self) -> usize

Get current total cache size (across all connections)

Source

pub fn connection_size(&self, conn_id: ConnectionId) -> usize

Get cache size for a specific connection

Source

pub fn is_empty(&self) -> bool

Check if cache is empty

Source

pub fn connection_count(&self) -> usize

Get number of tracked connections

Source

pub fn remove(&self, conn_id: ConnectionId, sql: &str) -> bool

Remove a specific statement from a connection’s cache

Source

pub fn cleanup_idle_connections(&self, max_idle_duration: Duration) -> usize

Clean up idle connection caches

Removes connection caches that haven’t been accessed for longer than the threshold. This helps prevent memory leaks from abandoned connection IDs.

§Arguments
  • max_idle_duration - Maximum idle time before cleanup
§Returns

Number of connections cleaned up

Trait Implementations§

Source§

impl Default for PreparedStatementCache

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<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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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
Source§

impl<T> SendAlias for T

Source§

impl<T> SendAlias for T

Source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

Source§

impl<T> SyncAlias for T

Source§

impl<T> SyncAlias for T