Skip to main content

Module cache

Module cache 

Source
Expand description

Embedding Cache for RAG Pipeline

This module provides caching for text embeddings to avoid re-computing vectors for unchanged content. This is especially valuable for:

  • Large document re-indexing
  • Frequently accessed documents
  • Multi-collection setups with shared documents

§Cache Key Strategy

Cache keys are computed as SHA-256 hashes of text + model_name to ensure:

  • Unique keys for different content
  • Model-specific embeddings (different models produce different vectors)
  • Consistent keys across restarts

§Implementation

Uses the lru crate for O(1) get/put operations with proper LRU eviction. The cache is thread-safe via parking_lot::Mutex.

§Example

use ares::rag::cache::{EmbeddingCache, LruEmbeddingCache, CacheConfig};

// Create a cache with 512MB max size
let cache = LruEmbeddingCache::new(CacheConfig {
    max_size_bytes: 512 * 1024 * 1024,
    ..Default::default()
});

// Check cache before computing embedding
let key = cache.compute_key("hello world", "bge-small-en-v1.5");
if let Some(embedding) = cache.get(&key).await {
    // Use cached embedding
} else {
    // Compute and cache
    let embedding = embed("hello world").await?;
    cache.set(&key, embedding.clone(), None).await?;
}

Structs§

CacheConfig
Configuration for the embedding cache
CacheStats
Statistics for cache performance monitoring
LruEmbeddingCache
In-memory LRU cache for embeddings
NoOpCache
A no-op cache that doesn’t store anything

Traits§

EmbeddingCache
Trait for embedding cache implementations