Skip to main content

CachedQueryRunner

Struct CachedQueryRunner 

Source
pub struct CachedQueryRunner<F, E>
where F: FnMut(&str) -> Result<RowSet, E>,
{ /* private fields */ }
Expand description

A read-through adapter that wraps a synchronous query executor with a SqlQueryCache.

The executor F is a FnMut(&str) -> Result<RowSet, E> — a closure or function pointer that actually runs the SQL against a database backend.

On a cache hit the executor is never called. On a miss the executor is called, the result stored in the cache, and returned to the caller.

§Example

use oxisql_core::{Row, RowSet, Value};
use oxisql_cache::CachedQueryRunner;

let mut runner = CachedQueryRunner::new(
    32,
    |sql: &str| -> Result<RowSet, String> {
        // Simulated DB hit.
        Ok(RowSet::from_rows(vec![Row::new(
            vec!["n".into()],
            vec![Value::I64(1)],
        )]))
    },
);

let r1 = runner.run("SELECT 1").unwrap();
let r2 = runner.run("SELECT 1").unwrap();
assert_eq!(r1.len(), r2.len());
assert_eq!(runner.hits(), 1);
assert_eq!(runner.misses(), 1);

Implementations§

Source§

impl<F, E> CachedQueryRunner<F, E>
where F: FnMut(&str) -> Result<RowSet, E>,

Source

pub fn new(capacity: usize, executor: F) -> Self

Create a new runner with the given cache capacity and executor.

Source

pub fn run(&mut self, sql: &str) -> Result<RowSet, E>

Run sql against the cache/executor.

Returns a cached RowSet clone on a hit. On a miss, calls the executor, caches the result (without TTL), and returns it.

§Errors

Propagates any error returned by the executor.

Source

pub fn run_with_ttl(&mut self, sql: &str, ttl: Duration) -> Result<RowSet, E>

Run sql with a TTL on the cached result.

Cached results expire after ttl; the next call after expiry will re-execute the query.

§Errors

Propagates any error returned by the executor.

Source

pub fn invalidate(&mut self, sql: &str)

Invalidate a specific cached result.

Source

pub fn clear(&mut self)

Remove all cached results.

Source

pub fn hits(&self) -> u64

Return the number of cache hits since creation.

Source

pub fn misses(&self) -> u64

Return the number of cache misses since creation.

Source

pub fn stats(&self) -> QueryCacheStats

Return a stats snapshot.

Auto Trait Implementations§

§

impl<F, E> Freeze for CachedQueryRunner<F, E>
where F: Freeze,

§

impl<F, E> RefUnwindSafe for CachedQueryRunner<F, E>
where F: RefUnwindSafe,

§

impl<F, E> Send for CachedQueryRunner<F, E>
where F: Send,

§

impl<F, E> Sync for CachedQueryRunner<F, E>
where F: Sync,

§

impl<F, E> Unpin for CachedQueryRunner<F, E>
where F: Unpin,

§

impl<F, E> UnsafeUnpin for CachedQueryRunner<F, E>
where F: UnsafeUnpin,

§

impl<F, E> UnwindSafe for CachedQueryRunner<F, E>
where F: UnwindSafe,

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.