Skip to main content

StatementCache

Struct StatementCache 

Source
pub struct StatementCache { /* private fields */ }
Expand description

Two-tier prepared statement cache.

  • Tier 1 (HashMap): Pre-registered queries. Never evicted. O(1) lookup.
  • Tier 2 (LRU): Ad-hoc queries. Auto-evicted when full. O(1) amortized.

Statements are keyed by SQL text. Each cached statement has a unique server-side name for the PG prepared statement protocol.

Implementations§

Source§

impl StatementCache

Source

pub fn new() -> Self

Create a new statement cache with the default LRU capacity (256).

Source

pub fn with_capacity(lru_capacity: usize) -> Self

Create a new statement cache with a custom LRU capacity.

Source

pub fn register(&mut self, name: &str, statement: Statement)

Register a statement in Tier 1 (permanent, never evicted).

The name is the user-defined name (also used as the server-side name).

Source

pub fn get_registered(&mut self, name: &str) -> Option<&CachedStatement>

Look up a registered statement by name (Tier 1).

Source

pub fn get_adhoc(&mut self, sql: &str) -> Option<&CachedStatement>

Look up an ad-hoc statement by SQL text (Tier 2).

Source

pub fn insert_adhoc( &mut self, sql: String, statement: Statement, ) -> Option<String>

Insert an ad-hoc statement into Tier 2.

Returns the evicted statement’s server-side name if the cache was full, so the caller can send a Close message to the server.

Source

pub fn record_miss(&mut self)

Record a cache miss.

Source

pub fn lookup_or_miss(&mut self, sql: &str) -> Option<&CachedStatement>

Get the server-side name for an ad-hoc query, or generate one.

Checks Tier 2 first. If not found, records a miss and returns None.

Source

pub fn metrics(&self) -> &CacheMetrics

Get cache metrics.

Source

pub fn registered_count(&self) -> usize

Number of registered (Tier 1) statements.

Source

pub fn adhoc_count(&self) -> usize

Number of cached ad-hoc (Tier 2) statements.

Source

pub fn generate_name(&self) -> String

Generate a unique server-side statement name.

Trait Implementations§

Source§

impl Default for StatementCache

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> 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