pub struct SemanticCacheEngine { /* private fields */ }Expand description
The semantic cache engine (DW-083). Constructed once at
startup and stored on the dataplane (persists across reloads
— the HNSW index and cached entries survive config refreshes).
Config is updated in place via update_config.
Implementations§
Source§impl SemanticCacheEngine
impl SemanticCacheEngine
Sourcepub fn new(config: SemanticCacheConfig) -> Self
pub fn new(config: SemanticCacheConfig) -> Self
Build the engine from a config. Creates a fresh HNSW
index sized for max_entries.
Sourcepub fn config_of(cfg: Option<&AiConfig>) -> Option<SemanticCacheConfig>
pub fn config_of(cfg: Option<&AiConfig>) -> Option<SemanticCacheConfig>
Extract the semantic-cache config from the ai: block.
Returns None when the block or the semantic_cache field
is absent. Does NOT check enabled — a disabled engine
still compiles (so a reload can flip it on); the runtime
checks enabled on every lookup/store.
Sourcepub fn compile(cfg: Option<&AiConfig>) -> Option<Self>
pub fn compile(cfg: Option<&AiConfig>) -> Option<Self>
Compile from the ai: config block. Returns None when
the semantic_cache field is absent. The returned engine
is live only when enabled is true (checked at runtime).
Sourcepub fn update_config(&self, config: SemanticCacheConfig)
pub fn update_config(&self, config: SemanticCacheConfig)
Update the config in place (reload path). The HNSW index
and cached entries PERSIST — only the config (threshold,
TTL, timeout, etc.) changes. A max_entries change does
NOT rebuild the index immediately; the next reset (when
the cache fills) sizes the new index to the new value.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Whether the cache is enabled (the runtime gate for every lookup/store).
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Number of cached entries (introspection / tests).
Sourcepub async fn lookup(
&self,
prompt_text: &str,
model: &str,
) -> Option<CachedResponse>
pub async fn lookup( &self, prompt_text: &str, model: &str, ) -> Option<CachedResponse>
Look up a cached response for prompt_text + model.
Returns the cached response (JSON or SSE frames) when a
match is found within the similarity threshold, within TTL,
and for the same model alias. None otherwise (or when
disabled, or on any embedding/search error — the cache
fails open: a miss never blocks the request).
PERF-02: the exact-match fast tier checks a hash map BEFORE calling the embedding service. An exact prompt-text match skips the embedding call entirely (the common case for repeated identical prompts).
Sourcepub async fn store(&self, prompt_text: &str, response_json: &Value, model: &str)
pub async fn store(&self, prompt_text: &str, response_json: &Value, model: &str)
Store a non-streaming response in the cache (fire-and-
forget from the request path). When the cache is full
(entry_count >= max_entries), the least recently used
entry is evicted (PERF-02 LRU, not a wholesale reset).
Errors are logged and swallowed.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for SemanticCacheEngine
impl !RefUnwindSafe for SemanticCacheEngine
impl Send for SemanticCacheEngine
impl Sync for SemanticCacheEngine
impl Unpin for SemanticCacheEngine
impl !UnwindSafe for SemanticCacheEngine
Blanket Implementations§
§impl<T> AnyExt for T
impl<T> AnyExt for T
§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind reference§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable reference§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointer§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointer§fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
T behind Box pointer§fn downcast_move<T>(this: Self) -> Option<T>
fn downcast_move<T>(this: Self) -> Option<T>
Self to T,
useful only in generic context as a workaround for specializationSource§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
§impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
fn coerce_rc_to(self: Rc<X>) -> Rc<T>
fn coerce_box_to(self: Box<X>) -> Box<T>
fn coerce_ref_to(&self) -> &T
fn coerce_mut_to(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more