Skip to main content

StatementCache

Struct StatementCache 

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

Representation of a cache of Statements.

StatementCache is bound to one Client, and Statements generated by that Client must not be used with other Clients.

Preparing the same statement from several tasks at once is coalesced: one of them sends a PREPARE to the database while the others wait for its result, so a burst of concurrent requests for an uncached statement costs a single round trip rather than one per task. If that preparation fails or its task is cancelled, one of the waiting tasks starts a fresh one instead of failing along with it, and nothing is cached until a preparation succeeds.

It can be used like that:

let client = pool.get().await?;
let stmt = client
    .statement_cache
    .prepare(&client, "SELECT 1")
    .await;
let rows = client.query(stmt, &[]).await?;
...

Normally, you probably want to use the ClientWrapper::prepare_cached() and ClientWrapper::prepare_typed_cached() methods instead (or the similar ones on Transaction).

Implementations§

Source§

impl StatementCache

Source

pub fn size(&self) -> usize

Returns current size of this StatementCache.

Source

pub fn clear(&self)

Clears this StatementCache.

Important: This only clears the StatementCache of one Client instance. If you want to clear the StatementCache of all Clients you should be calling pool.manager().statement_caches.clear() instead.

Source

pub fn remove(&self, query: &str, types: &[Type]) -> Option<Statement>

Removes a Statement from this StatementCache.

Important: This only removes a Statement from one Client cache. If you want to remove a Statement from all StatementCaches you should be calling pool.manager().statement_caches.remove() instead.

Source

pub async fn prepare( &self, client: &PgClient, query: &str, ) -> Result<Statement, Error>

Creates a new prepared Statement using this StatementCache, if possible.

See tokio_postgres::Client::prepare().

Source

pub async fn prepare_typed( &self, client: &PgClient, query: &str, types: &[Type], ) -> Result<Statement, Error>

Creates a new prepared Statement with specifying its Types explicitly using this StatementCache, if possible.

See tokio_postgres::Client::prepare_typed().

Trait Implementations§

Source§

impl Debug for StatementCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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 = !

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