mirror of
https://github.com/isar/rusqlite.git
synced 2025-12-25 12:52:23 +08:00
Cache param count and make statement cache more effective
This commit is contained in:
committed by
Thom Chiovoloni
parent
6485b324d7
commit
a776f460e8
25
src/cache.rs
25
src/cache.rs
@@ -5,6 +5,7 @@ use crate::{Connection, Result, Statement};
|
||||
use lru_cache::LruCache;
|
||||
use std::cell::RefCell;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::Arc;
|
||||
|
||||
impl Connection {
|
||||
/// Prepare a SQL statement for execution, returning a previously prepared
|
||||
@@ -54,7 +55,7 @@ impl Connection {
|
||||
|
||||
/// Prepared statements LRU cache.
|
||||
#[derive(Debug)]
|
||||
pub struct StatementCache(RefCell<LruCache<String, RawStatement>>);
|
||||
pub struct StatementCache(RefCell<LruCache<Arc<str>, RawStatement>>);
|
||||
|
||||
/// Cacheable statement.
|
||||
///
|
||||
@@ -125,12 +126,16 @@ impl StatementCache {
|
||||
conn: &'conn Connection,
|
||||
sql: &str,
|
||||
) -> Result<CachedStatement<'conn>> {
|
||||
let trimmed = sql.trim();
|
||||
let mut cache = self.0.borrow_mut();
|
||||
let stmt = match cache.remove(sql.trim()) {
|
||||
let stmt = match cache.remove(trimmed) {
|
||||
Some(raw_stmt) => Ok(Statement::new(conn, raw_stmt)),
|
||||
None => conn.prepare(sql),
|
||||
None => conn.prepare(trimmed),
|
||||
};
|
||||
stmt.map(|stmt| CachedStatement::new(stmt, self))
|
||||
stmt.map(|mut stmt| {
|
||||
stmt.stmt.set_statement_cache_key(trimmed);
|
||||
CachedStatement::new(stmt, self)
|
||||
})
|
||||
}
|
||||
|
||||
// Return a statement to the cache.
|
||||
@@ -140,10 +145,14 @@ impl StatementCache {
|
||||
}
|
||||
let mut cache = self.0.borrow_mut();
|
||||
stmt.clear_bindings();
|
||||
let sql = String::from_utf8_lossy(stmt.sql().unwrap().to_bytes())
|
||||
.trim()
|
||||
.to_string();
|
||||
cache.insert(sql, stmt);
|
||||
if let Some(sql) = stmt.statement_cache_key() {
|
||||
cache.insert(sql, stmt);
|
||||
} else {
|
||||
debug_assert!(
|
||||
false,
|
||||
"bug in statement cache code, statement returned to cache that without key"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
|
||||
Reference in New Issue
Block a user