Skip to main content

EmbeddingCache

Struct EmbeddingCache 

Source
pub struct EmbeddingCache { /* private fields */ }
Expand description

Unstable: internal LRU caching mechanism; shard count and eviction policy may change.

Embedding cache with sharded LRU eviction policy.

Thread-safe cache for storing computed embeddings. Uses Blake3 hashing for fast, collision-resistant cache keys. Internally sharded into 16 independent LRU caches to reduce write-lock contention.

§Disabling

Pass capacity=0 to disable caching. All cache operations become no-ops (no locking, no hashing work beyond key construction).

§Example

use lattice_embed::{EmbeddingCache, EmbeddingModel, ModelConfig};
use lattice_embed::service::EmbeddingRole;

let cache = EmbeddingCache::new(1000);

// Cache miss - no embedding stored yet
let key = cache.compute_key(
    "Hello, world!",
    ModelConfig::new(EmbeddingModel::BgeSmallEnV15),
    EmbeddingRole::Generic,
);
assert!(cache.get(&key).is_none());

// Store embedding
let embedding = vec![0.1, 0.2, 0.3];
cache.put(key, embedding.clone());

// Cache hit — returns Arc<[f32]>
let cached = cache.get(&key).unwrap();
assert_eq!(&*cached, &embedding[..]);

Implementations§

Source§

impl EmbeddingCache

Source

pub fn new(capacity: usize) -> Self

Unstable: constructor signature may change when shard count becomes configurable.

The capacity is divided equally across 16 internal shards. Each shard independently manages its own LRU eviction.

§Arguments
  • capacity - Maximum total number of embeddings to cache. Use 0 to disable caching.
Source

pub fn with_default_capacity() -> Self

Unstable: convenience constructor; subject to change with cache redesign.

Source

pub fn compute_key( &self, text: &str, model_config: ModelConfig, role: EmbeddingRole, ) -> [u8; 32]

Unstable: key scheme (Blake3 + EmbeddingKey canonical bytes) may change; don’t store keys across sessions.

Uses Blake3 hashing for fast, collision-resistant keys. The key includes the model name, revision, and active dimension from the ModelConfig, so different MRL truncations produce different cache keys.

The role is also included so that embed_query("hello") and embed_passage("hello") produce different cache entries even when the raw text and model config are identical. Use EmbeddingRole::Generic for the backwards-compatible embed() path.

Source

pub fn get(&self, key: &[u8; 32]) -> Option<Arc<[f32]>>

Unstable: return type (Arc<[f32]>) may change to a newtype; internal cache API.

Returns Some(Arc<[f32]>) if found (cheap refcount bump), None otherwise. Updates per-shard hit/miss counters for metrics.

Source

pub fn put(&self, key: [u8; 32], embedding: Vec<f32>)

Unstable: internal cache storage method; interface may change.

Converts the Vec into Arc<[f32]> for shared-ownership storage. If the shard is at capacity, its least recently used entry is evicted.

Source

pub fn get_many(&self, keys: &[[u8; 32]]) -> Vec<Option<Arc<[f32]>>>

Unstable: batch cache access; return type may change with cache redesign.

Returns a vector of Option<Arc<[f32]>> for each key, in the same order. Each hit is an O(1) refcount bump (no data copy).

Source

pub fn put_many(&self, entries: Vec<([u8; 32], Vec<f32>)>)

Unstable: batch cache storage; interface may change with cache redesign.

Converts each Vec into Arc<[f32]> for shared-ownership storage.

Source

pub fn stats(&self) -> CacheStats

Unstable: returns CacheStats which is itself Unstable; metrics shape may evolve.

Aggregates per-shard counters. The size field is the sum of all shard sizes.

Source

pub fn per_shard_stats(&self) -> Vec<ShardStats>

Unstable: internal monitoring hook; shard count and ShardStats shape may change.

Returns a vector of (size, hits, misses) tuples, one per shard.

Source

pub fn clear(&self)

Unstable: internal cache management; may be removed in favor of capacity-based eviction.

Source

pub fn is_enabled(&self) -> bool

Unstable: internal state query; may be removed when zero-capacity is the only disable path.

Trait Implementations§

Source§

impl Default for EmbeddingCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more