Skip to main content

SqlPlanCache

Struct SqlPlanCache 

Source
pub struct SqlPlanCache<P> { /* private fields */ }
Expand description

In-memory LRU cache of prepared SQL statement plans.

P is the caller-supplied plan representation — typically a compiled query plan, a byte buffer, or a Box<dyn Any>. The cache is generic so it can accommodate any backend’s plan type without imposing a common trait.

Plans are never expired by TTL (they are considered stable for the lifetime of a connection). TTL can be added via SqlPlanCache::put_with_ttl when plans have limited validity (e.g. after schema changes).

Keys are normalised SQL text (same rules as SqlQueryCache).

§Example

use oxisql_cache::SqlPlanCache;

// Use Vec<u8> as a stand-in for a serialised plan.
let mut plan_cache: SqlPlanCache<Vec<u8>> = SqlPlanCache::new(512);
plan_cache.put("SELECT 1", vec![0x01, 0x02]);
assert!(plan_cache.get("select  1").is_some());

Implementations§

Source§

impl<P: Clone> SqlPlanCache<P>

Source

pub fn new(capacity: usize) -> Self

Create a new plan cache with the given maximum entry count.

Source

pub fn get(&mut self, sql: &str) -> Option<&P>

Look up a plan by SQL text.

Returns a reference to the cached plan on a hit, None on a miss.

Source

pub fn put(&mut self, sql: &str, plan: P)

Store a plan for sql.

Source

pub fn put_with_ttl(&mut self, sql: &str, plan: P, ttl: Duration)

Store a plan for sql that expires after ttl.

Source

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

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

Source

pub fn clear(&mut self)

Remove all cached plans.

Source

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

Return true if a live plan is cached for sql.

Source

pub fn len(&self) -> usize

Return the number of plans 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 plans.

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 hit_rate(&self) -> f64

Return the hit rate as a fraction in [0.0, 1.0].

Source

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

Dynamically resize the cache.

Auto Trait Implementations§

§

impl<P> Freeze for SqlPlanCache<P>
where LruCache<String, P>: Freeze,

§

impl<P> RefUnwindSafe for SqlPlanCache<P>

§

impl<P> Send for SqlPlanCache<P>
where LruCache<String, P>: Send,

§

impl<P> Sync for SqlPlanCache<P>
where LruCache<String, P>: Sync,

§

impl<P> Unpin for SqlPlanCache<P>
where LruCache<String, P>: Unpin,

§

impl<P> UnsafeUnpin for SqlPlanCache<P>

§

impl<P> UnwindSafe for SqlPlanCache<P>

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.