Skip to main content

SqlQueryCache

Struct SqlQueryCache 

Source
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

Source

pub fn new(capacity: usize) -> Self

Create a new cache with the given maximum entry count.

Source

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.

Source

pub fn put(&mut self, sql: &str, result: RowSet)

Insert a query result set, replacing any existing entry for the same normalised SQL.

Source

pub fn put_with_ttl(&mut self, sql: &str, result: RowSet, ttl: Duration)

Insert a query result set that expires after ttl.

Source

pub fn invalidate(&mut self, sql: &str) -> Option<RowSet>

Remove the cached result for sql, returning it if present.

Source

pub fn clear(&mut self)

Remove all cached results.

Source

pub fn contains(&mut self, sql: &str) -> bool

Return true if the cache contains a live entry for sql.

Source

pub fn len(&self) -> usize

Return the number of entries currently in the cache.

Source

pub fn is_empty(&self) -> bool

Return true if the cache is empty.

Source

pub fn cap(&self) -> usize

Return the maximum number of entries.

Source

pub fn stats(&self) -> QueryCacheStats

Return a stats snapshot.

Source

pub fn resize(&mut self, new_cap: usize)

Dynamically resize the cache capacity.

If new_cap is smaller than the current length, entries are evicted (LRU policy) until len() <= new_cap.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.