-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from pimeys/lru
LRU Statement Caching
- Loading branch information
Showing
29 changed files
with
381 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod statement_cache; | ||
|
||
pub(crate) use statement_cache::StatementCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use lru_cache::LruCache; | ||
|
||
/// A cache for prepared statements. When full, the least recently used | ||
/// statement gets removed. | ||
#[derive(Debug)] | ||
pub struct StatementCache<T> { | ||
inner: LruCache<String, T>, | ||
} | ||
|
||
impl<T> StatementCache<T> { | ||
/// Create a new cache with the given capacity. | ||
pub fn new(capacity: usize) -> Self { | ||
Self { | ||
inner: LruCache::new(capacity), | ||
} | ||
} | ||
|
||
/// 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 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: 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.contains_key(k) { | ||
lru_item = self.inner.remove(k); | ||
} | ||
|
||
self.inner.insert(k.into(), v); | ||
|
||
lru_item | ||
} | ||
|
||
/// The number of statements in the cache. | ||
pub fn len(&self) -> usize { | ||
self.inner.len() | ||
} | ||
|
||
/// Removes the least recently used item from the cache. | ||
pub fn remove_lru(&mut self) -> Option<T> { | ||
self.inner.remove_lru().map(|(_, v)| v) | ||
} | ||
|
||
/// Clear all cached statements from the cache. | ||
#[cfg(any(feature = "sqlite"))] | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::io::Encode; | ||
use crate::mysql::protocol::Capabilities; | ||
|
||
// https://dev.mysql.com/doc/internals/en/com-stmt-close.html | ||
|
||
#[derive(Debug)] | ||
pub struct StmtClose { | ||
pub statement: u32, | ||
} | ||
|
||
impl Encode<'_, Capabilities> for StmtClose { | ||
fn encode_with(&self, buf: &mut Vec<u8>, _: Capabilities) { | ||
buf.push(0x19); // COM_STMT_CLOSE | ||
buf.extend(&self.statement.to_le_bytes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.