CachingSession provides a convenient wrapper over Session that automatically caches prepared statements and reuses them when possible.
Overview
TheCachingSession type:
- Automatically prepares statements on first use
- Caches prepared statements for reuse
- Provides the same API as
Sessionfor queries and execution - Manages cache size with LRU eviction
When the cache reaches its maximum capacity, the oldest prepared statement is removed to make room for new ones.
Creating a CachingSession
Using CachingSessionBuilder
Using from()
Using with_hasher()
CachingSessionBuilder
new
Creates a new
CachingSessionBuilder from a Session.Parameters:session: Session- The session to wrap
CachingSessionBuilderExample:new_shared
Creates a new
CachingSessionBuilder from an Arc<Session>.Parameters:session: Arc<Session>- The shared session to wrap
CachingSessionBuilderExample:max_capacity
Configures the maximum capacity of the prepared statements cache.The default capacity is 128. If you expect to run many different prepared statements, consider increasing this value.Parameters:
max_capacity: usize- Maximum number of cached prepared statements
SelfExample:use_cached_result_metadata
Configures whether to use cached metadata for result deserialization.When enabled, the driver requests the server not to attach result metadata in responses. The driver caches metadata received during preparation and uses it for deserialization.This option is
false by default.Parameters:use_cached_metadata: bool- Whether to use cached metadata
SelfExample:hasher
Provides a custom hasher for the prepared statement cache.Parameters:
hasher: S2- Custom hasher implementingBuildHasher
CachingSessionBuilder<S2>Example:build
Finishes configuration and creates a
CachingSession.Returns: CachingSession<S>Example:Query Execution Methods
execute_unpaged
Does the same as
Session::execute_unpaged but uses the prepared statement cache.On first execution with a given query string, the statement is prepared and cached. Subsequent executions with the same query reuse the cached prepared statement.Parameters:query: impl Into<Statement>- The query to executevalues: impl SerializeRow- Bound values
Result<QueryResult, ExecutionError>Example:execute_iter
Does the same as
Session::execute_iter but uses the prepared statement cache.Parameters:query: impl Into<Statement>- The query to executevalues: impl SerializeRow- Bound values
Result<QueryPager, PagerExecutionError>Example:execute_single_page
Does the same as
Session::execute_single_page but uses the prepared statement cache.Parameters:query: impl Into<Statement>- The query to executevalues: impl SerializeRow- Bound valuespaging_state: PagingState- Paging state
Result<(QueryResult, PagingStateResponse), ExecutionError>Example:Batch Operations
batch
Does the same as
Session::batch but uses the prepared statement cache.Automatically prepares unprepared statements in the batch before executing.Parameters:batch: &Batch- The batch to executevalues: impl BatchValues- Values for each statement
Result<QueryResult, ExecutionError>Example:prepare_batch
Prepares all statements within a batch and returns a new batch where every statement is prepared.Uses the prepared statement cache for each statement.Parameters:
batch: &Batch- The batch with unprepared statements
Result<Batch, ExecutionError>Example:Cache Management
add_prepared_statement
Adds a prepared statement to the cache manually.If the statement is already in the cache, returns the cached version. Otherwise, prepares it and adds it to the cache.Parameters:
query: impl Into<&Statement>- The query to prepare
Result<PreparedStatement, PrepareError>Example:get_max_capacity
Retrieves the maximum capacity of the prepared statements cache.Returns:
usizeExample:get_session
Retrieves the underlying
Session instance.Returns: &SessionExample:Usage Patterns
Simple Caching
Large Statement Pool
With Custom Configuration
Performance Considerations
The
CachingSession is most beneficial when:- You execute the same queries repeatedly with different values
- You don’t want to manually manage prepared statement lifecycle
- Your application has a limited set of unique queries
Related Types
Session- The underlying session that handles queriesSessionBuilder- Builder for creating sessionsPreparedStatement- Prepared statement returned by the cacheBatch- Container for batch operations
