Skip to main content

SemanticCache

Struct SemanticCache 

Source
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>

Source

pub fn new(max_entries: usize) -> Self

In memory, at most max_entries, with the default threshold and no TTL.

Source

pub fn with_config(config: SemanticConfig) -> Self

Source

pub fn config(&self) -> &SemanticConfig

Source

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.

Source

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.

Source

pub fn remove(&self, id: u64) -> Option<V>

Removes entry id and returns its value.

Source

pub fn remove_scope(&self, scope: &str) -> usize

Removes every entry in scope and returns how many there were.

Source

pub fn clear(&self)

Removes every entry and resets the counters.

Source

pub fn len(&self) -> usize

Entries stored, expired ones not yet dropped included.

Source

pub fn is_empty(&self) -> bool

Source

pub fn stats(&self) -> SemanticStats

Source§

impl<V: Clone + Serialize + DeserializeOwned> SemanticCache<V>

Source

pub fn snapshot(&self) -> Result<Vec<u8>, SemanticError>

The live entries as CBOR, least recently used first, for restore.

Source

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.

Auto Trait Implementations§

§

impl<V = CachedAnswer> !Freeze for SemanticCache<V>

§

impl<V> Forget for SemanticCache<V>

§

impl<V> Move for SemanticCache<V>

§

impl<V> RefUnwindSafe for SemanticCache<V>
where Mutex<State<V>>: RefUnwindSafe,

§

impl<V> Send for SemanticCache<V>
where Mutex<State<V>>: Send,

§

impl<V> Sync for SemanticCache<V>
where Mutex<State<V>>: Sync,

§

impl<V> Unpin for SemanticCache<V>
where Mutex<State<V>>: Unpin,

§

impl<V> UnsafeUnpin for SemanticCache<V>
where Mutex<State<V>>: UnsafeUnpin,

§

impl<V> UnwindSafe for SemanticCache<V>
where Mutex<State<V>>: UnwindSafe,

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<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> Same for T

Source§

type Output = T

Should always be Self
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.