pub struct SqlTemplateCache { /* private fields */ }Expand description
A thread-safe cache for SQL templates.
This cache stores parameterized SQL templates that can be reused across requests, avoiding repeated string construction for common query patterns.
§Example
use prax_query::sql::{SqlTemplateCache, DatabaseType};
let cache = SqlTemplateCache::new();
// First call generates and caches
let sql = cache.get_or_insert("user_by_email", DatabaseType::PostgreSQL, || {
"SELECT * FROM users WHERE email = $1".to_string()
});
// Second call returns cached value
let sql2 = cache.get_or_insert("user_by_email", DatabaseType::PostgreSQL, || {
panic!("Should not be called - value is cached")
});
assert_eq!(sql, sql2);Implementations§
Source§impl SqlTemplateCache
impl SqlTemplateCache
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new cache with pre-allocated capacity.
Sourcepub fn get_or_insert<F>(
&self,
key: &str,
db_type: DatabaseType,
generator: F,
) -> Arc<String>
pub fn get_or_insert<F>( &self, key: &str, db_type: DatabaseType, generator: F, ) -> Arc<String>
Get or insert a SQL template.
If the template exists in the cache, returns the cached value. Otherwise, calls the generator function, caches the result, and returns it.
Sourcepub fn contains(&self, key: &str, db_type: DatabaseType) -> bool
pub fn contains(&self, key: &str, db_type: DatabaseType) -> bool
Check if a template is cached.
Trait Implementations§
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