use std::collections::HashMap; use std::hash::Hash; use std::sync::Arc; // TODO: figure out a cache eviction strategy // we currently naively cache all prepared statements which could live-leak memory // on both the client and server if the user is synthesizing queries that are different each time // We put an upper bound on this by setting a default max connection lifetime in pool::Options but // that's only a band-aid /// Per-connection prepared statement cache. pub struct StatementCache { statements: HashMap, columns: HashMap, usize>>>, } impl StatementCache where Id: Eq + Hash, { pub fn new() -> Self { StatementCache { statements: HashMap::with_capacity(10), columns: HashMap::with_capacity(10), } } #[allow(unused)] pub fn has_columns(&self, id: Id) -> bool { self.columns.contains_key(&id) } pub fn get(&self, query: &str) -> Option<&Id> { self.statements.get(query) } // It is a logical error to call this without first calling [put_columns] pub fn get_columns(&self, id: Id) -> Arc, usize>> { Arc::clone(&self.columns[&id]) } pub fn put(&mut self, query: String, id: Id) { self.statements.insert(query, id); } pub fn put_columns(&mut self, id: Id, columns: HashMap, usize>) { self.columns.insert(id, Arc::new(columns)); } }