pub struct CompileCache { /* private fields */ }Expand description
A cache for CompiledQuery values keyed by plan hash + dialect.
Uses a moka::future::Cache with a weigher that limits the total
SQL string length, preventing unbounded memory growth.
§Examples
use vlorql_core::cache::CompileCache;
use vlorql_core::schema::{DialectProfile, SqlDialect, QueryPlan, Projection, FromClause};
use vlorql_core::compile::CompiledQuery;
use vlorql_core::validate::ValidatedPlan;
use std::sync::Arc;
let cache = CompileCache::new(1024, 60);
let plan = ValidatedPlan(Arc::new(QueryPlan {
select: vec![Projection::Column {
table: None, column: "id".to_owned(), alias: None,
}],
from: FromClause { table: "users".to_owned(), alias: None },
r#where: None, group_by: None, having: None,
order_by: None, limit: None, offset: None,
joins: None, ctes: None, distinct: false, distinct_on: None, set_operation: None,
}));
let profile = DialectProfile::default();
assert!(cache.get(&plan, &profile).await.is_none());
let compiled = CompiledQuery {
sql: "SELECT \"id\" FROM \"users\"".to_owned(),
parameters: vec![],
dialect: SqlDialect::Postgres,
};
cache.insert(&plan, &profile, compiled.clone()).await;
let cached = cache.get(&plan, &profile).await;
assert!(cached.is_some());
assert_eq!(cached.unwrap().sql, "SELECT \"id\" FROM \"users\"");Implementations§
Source§impl CompileCache
impl CompileCache
Sourcepub fn new(max_size: u64, ttl_seconds: u64) -> Self
pub fn new(max_size: u64, ttl_seconds: u64) -> Self
Creates a new compile cache.
max_size— maximum total weight (sum of SQL string lengths in bytes) before the cache evicts least-recently-used items.ttl_seconds— time-to-live in seconds. Entries older than this are automatically invalidated.
Sourcepub async fn get(
&self,
plan: &ValidatedPlan,
profile: &DialectProfile,
) -> Option<Arc<CompiledQuery>>
pub async fn get( &self, plan: &ValidatedPlan, profile: &DialectProfile, ) -> Option<Arc<CompiledQuery>>
Returns the cached compiled query for plan under profile, or
None on a cache miss.
The key is computed from the plan’s normalised JSON hash, the dialect, and the quoting style, so the same plan compiled for different dialects produces different cache entries.
Sourcepub async fn insert(
&self,
plan: &ValidatedPlan,
profile: &DialectProfile,
query: CompiledQuery,
)
pub async fn insert( &self, plan: &ValidatedPlan, profile: &DialectProfile, query: CompiledQuery, )
Inserts a compiled query into the cache.
The key is computed from plan and profile identically to
Self::get, ensuring that a subsequent get with the same
arguments finds the entry.
Sourcepub async fn invalidate_plan(
&self,
plan: &ValidatedPlan,
profile: &DialectProfile,
)
pub async fn invalidate_plan( &self, plan: &ValidatedPlan, profile: &DialectProfile, )
Removes the cache entry for plan under profile.
Trait Implementations§
Source§impl Clone for CompileCache
impl Clone for CompileCache
Source§fn clone(&self) -> CompileCache
fn clone(&self) -> CompileCache
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for CompileCache
impl !UnwindSafe for CompileCache
impl Freeze for CompileCache
impl Send for CompileCache
impl Sync for CompileCache
impl Unpin for CompileCache
impl UnsafeUnpin for CompileCache
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request