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>
impl<P: Clone> SqlPlanCache<P>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new plan cache with the given maximum entry count.
Sourcepub fn get(&mut self, sql: &str) -> Option<&P>
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.
Sourcepub fn put_with_ttl(&mut self, sql: &str, plan: P, ttl: Duration)
pub fn put_with_ttl(&mut self, sql: &str, plan: P, ttl: Duration)
Store a plan for sql that expires after ttl.
Sourcepub fn invalidate(&mut self, sql: &str) -> Option<P>
pub fn invalidate(&mut self, sql: &str) -> Option<P>
Remove the cached plan for sql, returning it if present.