pub struct SqlQueryCache { /* private fields */ }Expand description
In-memory LRU cache of SQL query result sets.
Result sets are keyed by their normalised SQL text (leading/trailing whitespace collapsed,
consecutive spaces reduced to a single space, ASCII letters upper-cased).
Each entry stores a cloned RowSet; callers receive another clone on
get so the cached copy is never mutated.
§TTL
Insert with a TTL via SqlQueryCache::put_with_ttl. After the TTL
elapses, get returns None and the stale entry is lazily removed.
§Example
use oxisql_core::{Row, RowSet, Value};
use oxisql_cache::SqlQueryCache;
let mut cache = SqlQueryCache::new(256);
let rows = vec![Row::new(vec!["id".into()], vec![Value::I64(1)])];
let rs = RowSet::from_rows(rows);
cache.put("SELECT id FROM t WHERE id = 1", rs.clone());
// Equivalent queries (different whitespace/case) hit the same entry.
assert!(cache.get("select id from t where id = 1").is_some());Implementations§
Source§impl SqlQueryCache
impl SqlQueryCache
Sourcepub fn get(&mut self, sql: &str) -> Option<RowSet>
pub fn get(&mut self, sql: &str) -> Option<RowSet>
Look up a query result set by SQL text.
The query is normalised before lookup. Returns a clone of the cached
RowSet on a hit, None on a miss or TTL expiry.
Sourcepub fn put(&mut self, sql: &str, result: RowSet)
pub fn put(&mut self, sql: &str, result: RowSet)
Insert a query result set, replacing any existing entry for the same normalised SQL.
Sourcepub fn put_with_ttl(&mut self, sql: &str, result: RowSet, ttl: Duration)
pub fn put_with_ttl(&mut self, sql: &str, result: RowSet, ttl: Duration)
Insert a query result set that expires after ttl.
Sourcepub fn invalidate(&mut self, sql: &str) -> Option<RowSet>
pub fn invalidate(&mut self, sql: &str) -> Option<RowSet>
Remove the cached result for sql, returning it if present.
Sourcepub fn contains(&mut self, sql: &str) -> bool
pub fn contains(&mut self, sql: &str) -> bool
Return true if the cache contains a live entry for sql.
Sourcepub fn stats(&self) -> QueryCacheStats
pub fn stats(&self) -> QueryCacheStats
Return a stats snapshot.