pub struct SqlTemplateCache { /* private fields */ }Expand description
A high-performance SQL template cache optimized for repeated queries.
Unlike QueryCache which stores full SQL strings, SqlTemplateCache stores
template structures with pre-computed placeholder positions for very fast
instantiation.
§Performance
- Cache lookup: O(1) hash lookup, ~5-10ns
- Template instantiation: O(n) where n is parameter count
- Thread-safe with minimal contention (parking_lot RwLock)
§Examples
use prax_query::cache::SqlTemplateCache;
let cache = SqlTemplateCache::new(1000);
// Register a template
let template = cache.register("users_by_id", "SELECT * FROM users WHERE id = $1");
// Instant retrieval (~5ns)
let sql = cache.get("users_by_id");Implementations§
Source§impl SqlTemplateCache
impl SqlTemplateCache
Sourcepub fn register(
&self,
key: impl Into<Cow<'static, str>>,
sql: impl AsRef<str>,
) -> Arc<SqlTemplate>
pub fn register( &self, key: impl Into<Cow<'static, str>>, sql: impl AsRef<str>, ) -> Arc<SqlTemplate>
Register a SQL template with a string key.
Returns the template for immediate use.
Sourcepub fn register_by_hash(
&self,
hash: u64,
sql: impl AsRef<str>,
) -> Arc<SqlTemplate>
pub fn register_by_hash( &self, hash: u64, sql: impl AsRef<str>, ) -> Arc<SqlTemplate>
Register a template by hash (for pre-computed hashes).
Sourcepub fn get(&self, key: &str) -> Option<Arc<SqlTemplate>>
pub fn get(&self, key: &str) -> Option<Arc<SqlTemplate>>
Get a template by string key (returns Arc for zero-copy).
§Performance
This is the fastest way to get cached SQL:
- Hash lookup: ~5ns
- Returns Arc
(no allocation)
Sourcepub fn get_by_hash(&self, hash: u64) -> Option<Arc<SqlTemplate>>
pub fn get_by_hash(&self, hash: u64) -> Option<Arc<SqlTemplate>>
Get a template by pre-computed hash (fastest path).
§Performance
~3-5ns for cache hit with pre-computed hash.
Sourcepub fn get_sql(&self, key: &str) -> Option<Arc<str>>
pub fn get_sql(&self, key: &str) -> Option<Arc<str>>
Get the SQL string directly (convenience method).
Sourcepub fn get_or_register<F>(
&self,
key: impl Into<Cow<'static, str>>,
f: F,
) -> Arc<SqlTemplate>
pub fn get_or_register<F>( &self, key: impl Into<Cow<'static, str>>, f: F, ) -> Arc<SqlTemplate>
Get or compute a template.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics.
Trait Implementations§
Source§impl Debug for SqlTemplateCache
impl Debug for SqlTemplateCache
Auto Trait Implementations§
impl !Freeze for SqlTemplateCache
impl !RefUnwindSafe for SqlTemplateCache
impl Send for SqlTemplateCache
impl Sync for SqlTemplateCache
impl Unpin for SqlTemplateCache
impl UnwindSafe for SqlTemplateCache
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