pub struct CachedQueryRunner<F, 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>
impl<F, E> CachedQueryRunner<F, E>
Sourcepub fn new(capacity: usize, executor: F) -> Self
pub fn new(capacity: usize, executor: F) -> Self
Create a new runner with the given cache capacity and executor.
Sourcepub fn run_with_ttl(&mut self, sql: &str, ttl: Duration) -> Result<RowSet, E>
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.
Sourcepub fn invalidate(&mut self, sql: &str)
pub fn invalidate(&mut self, sql: &str)
Invalidate a specific cached result.
Sourcepub fn stats(&self) -> QueryCacheStats
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more