pub struct PreparedStatementCache { /* private fields */ }Expand description
A cache for prepared statements.
This cache stores prepared statement metadata and tracks usage patterns to optimize database interactions. The actual statement handles are managed by the database driver.
§Features
- LRU eviction when capacity is reached
- Usage statistics for monitoring
- Thread-safe with read-write locking
- Automatic cleanup of stale entries
§Example
use prax_query::db_optimize::PreparedStatementCache;
let cache = PreparedStatementCache::new(100);
// Register a prepared statement
let entry = cache.get_or_create("find_user_by_email", || {
"SELECT * FROM users WHERE email = $1".to_string()
});
// Check cache stats
let stats = cache.stats();
println!("Hit rate: {:.1}%", stats.hit_rate());Implementations§
Source§impl PreparedStatementCache
impl PreparedStatementCache
Sourcepub fn get_or_create<F>(&self, name: &str, generator: F) -> CachedStatement
pub fn get_or_create<F>(&self, name: &str, generator: F) -> CachedStatement
Get or create a prepared statement entry.
If the statement is cached, returns the cached entry and increments hit count. Otherwise, calls the generator function, caches the result, and returns it.
Sourcepub fn stats(&self) -> PreparedStatementStats
pub fn stats(&self) -> PreparedStatementStats
Get cache statistics.
Sourcepub fn record_use(&self, name: &str)
pub fn record_use(&self, name: &str)
Update statement usage (call after executing).
Sourcepub fn set_handle(&self, name: &str, handle: u64)
pub fn set_handle(&self, name: &str, handle: u64)
Set a database-specific handle for a statement.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PreparedStatementCache
impl !RefUnwindSafe for PreparedStatementCache
impl Send for PreparedStatementCache
impl Sync for PreparedStatementCache
impl Unpin for PreparedStatementCache
impl UnwindSafe for PreparedStatementCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more