pub struct SemanticCache<V = CachedAnswer> { /* private fields */ }Expand description
Embedding-similarity cache: the nearest stored embedding in the same scope, by cosine similarity, stands in when it reaches the threshold.
Bounded and least-recently-used across scopes like MemoryStore, with
an optional TTL. Values default to CachedAnswer but may be any Clone type. Callers
decide what may be cached; for infer/2 requests check may_read and
may_store first.
Implementations§
Source§impl<V: Clone> SemanticCache<V>
impl<V: Clone> SemanticCache<V>
Sourcepub fn new(max_entries: usize) -> Self
pub fn new(max_entries: usize) -> Self
In memory, at most max_entries, with the default threshold and no TTL.
pub fn with_config(config: SemanticConfig) -> Self
pub fn config(&self) -> &SemanticConfig
Sourcepub fn lookup(
&self,
scope: &str,
embedding: &[f32],
) -> Result<Lookup<V>, SemanticError>
pub fn lookup( &self, scope: &str, embedding: &[f32], ) -> Result<Lookup<V>, SemanticError>
The most similar live entry in scope: a Lookup::Hit when its similarity reaches the
threshold (and the entry counts as used), a Lookup::Miss with the best score otherwise.
Sourcepub fn insert(
&self,
scope: &str,
embedding: &[f32],
value: V,
) -> Result<u64, SemanticError>
pub fn insert( &self, scope: &str, embedding: &[f32], value: V, ) -> Result<u64, SemanticError>
Stores value under embedding in scope and returns the entry’s id. An entry with the
same direction in the scope is replaced (keeping its id); otherwise, when full, expired
entries go first, then the least recently used.
Sourcepub fn remove_scope(&self, scope: &str) -> usize
pub fn remove_scope(&self, scope: &str) -> usize
Removes every entry in scope and returns how many there were.
pub fn is_empty(&self) -> bool
pub fn stats(&self) -> SemanticStats
Source§impl<V: Clone + Serialize + DeserializeOwned> SemanticCache<V>
impl<V: Clone + Serialize + DeserializeOwned> SemanticCache<V>
Sourcepub fn snapshot(&self) -> Result<Vec<u8>, SemanticError>
pub fn snapshot(&self) -> Result<Vec<u8>, SemanticError>
The live entries as CBOR, least recently used first, for restore.
Sourcepub fn restore(
config: SemanticConfig,
bytes: &[u8],
) -> Result<Self, SemanticError>
pub fn restore( config: SemanticConfig, bytes: &[u8], ) -> Result<Self, SemanticError>
A cache with config holding the entries of a snapshot. Entries that
fail validation under config (dimension, finite) or have expired are skipped; the most
recently used are kept when there are more than max_entries.