Skip to content

Commit

Permalink
LRU caching for SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jun 24, 2020
1 parent 5d64310 commit 3ef98fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions sqlx-core/src/common/statement_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use lru_cache::LruCache;
/// A cache for prepared statements. When full, the least recently used
/// statement gets removed.
#[derive(Debug)]
pub struct StatementCache {
inner: LruCache<String, u32>,
pub struct StatementCache<T> {
inner: LruCache<String, T>,
}

impl StatementCache {
impl<T> StatementCache<T> {
/// Create a new cache with the given capacity.
pub fn new(capacity: usize) -> Self {
Self {
Expand All @@ -17,19 +17,19 @@ impl StatementCache {

/// Returns a mutable reference to the value corresponding to the given key
/// in the cache, if any.
pub fn get_mut(&mut self, k: &str) -> Option<&mut u32> {
pub fn get_mut(&mut self, k: &str) -> Option<&mut T> {
self.inner.get_mut(k)
}

/// Inserts a new statement to the cache, returning the least recently used
/// statement id if the cache is full, or if inserting with an existing key,
/// the replaced existing statement.
pub fn insert(&mut self, k: &str, v: u32) -> Option<u32> {
pub fn insert(&mut self, k: &str, v: T) -> Option<T> {
let mut lru_item = None;

if self.inner.capacity() == self.len() && !self.inner.contains_key(k) {
lru_item = self.remove_lru();
} else if self.inner.contains_key(k) {
} else if self.contains_key(k) {
lru_item = self.inner.remove(k);
}

Expand All @@ -44,12 +44,17 @@ impl StatementCache {
}

/// Removes the least recently used item from the cache.
pub fn remove_lru(&mut self) -> Option<u32> {
pub fn remove_lru(&mut self) -> Option<T> {
self.inner.remove_lru().map(|(_, v)| v)
}

/// Clear all cached statements from the cache.
pub fn clear(&mut self) {
self.inner.clear();
}

/// True if cache has a value for the given key.
pub fn contains_key(&mut self, k: &str) -> bool {
self.inner.contains_key(k)
}
}
2 changes: 1 addition & 1 deletion sqlx-core/src/mysql/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct MySqlConnection {
pub(crate) stream: MySqlStream,

// cache by query string to the statement id
cache_statement: StatementCache,
cache_statement: StatementCache<u32>,

// working memory for the active row's column information
// this allows us to re-use these allocations unless the user is persisting the
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/postgres/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct PgConnection {
next_statement_id: u32,

// cache statement by query string to the id and columns
cache_statement: StatementCache,
cache_statement: StatementCache<u32>,

// cache user-defined types by id <-> info
cache_type_info: HashMap<u32, PgTypeInfo>,
Expand Down

0 comments on commit 3ef98fb

Please sign in to comment.