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
impl StatementCache
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns current size of this StatementCache.
Sourcepub fn clear(&self)
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.
Sourcepub fn remove(&self, query: &str, types: &[Type]) -> Option<Statement>
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.
Sourcepub async fn prepare(
&self,
client: &PgClient,
query: &str,
) -> Result<Statement, Error>
pub async fn prepare( &self, client: &PgClient, query: &str, ) -> Result<Statement, Error>
Creates a new prepared Statement using this StatementCache, if
possible.