Expand description
§Semantic Cache Manager
A production-quality semantic-aware cache that returns cached results for queries whose embeddings are semantically similar to previously cached queries — even when the exact query text differs.
Unlike a regular cache (exact key match), a SemanticCacheManager can return a
cached result for a semantically similar query, reducing redundant computation
when two different phrasings ask for the same information.
§Features
- Exact-match fast-path via FNV-1a hashed query text
- Semantic-match slow-path via cosine similarity scan
- TTL expiry on every lookup (lazy) and explicit batch expiry
- Five eviction strategies: LRU, LFU, SemanticCluster, TTLFirst, HybridScore
- Byte-budget enforcement (
max_bytes) - Semantic neighbors query for introspection
- Greedy cluster statistics for monitoring embedding distribution
- Comprehensive statistics (hit-rates, avg similarity on semantic hit, etc.)
§Example
use ipfrs_semantic::semantic_cache_manager::{
ScmCacheConfig, ScmCacheKey, ScmCacheHit, ScmEvictionStrategy, SemanticCacheManager,
};
let config = ScmCacheConfig {
max_entries: 256,
max_bytes: 64 * 1024 * 1024,
semantic_threshold: 0.90,
ttl_us: None,
strategy: ScmEvictionStrategy::LRU,
cluster_radius: 0.15,
};
let mut mgr = SemanticCacheManager::new(config);
let key = ScmCacheKey::new("What is IPFS?".to_string(), vec![0.1, 0.2, 0.3]);
let id = mgr.insert(key, b"answer bytes".to_vec(), None).expect("insert ok");
let hit = mgr
.lookup("What is IPFS?", &[0.1, 0.2, 0.3], 0)
.expect("lookup ok");
assert!(matches!(hit, ScmCacheHit::Exact { entry_id } if entry_id == id));Structs§
- ScmCache
Config - Configuration for
super::SemanticCacheManager. - ScmCache
Entry - A single cached result, keyed by a
ScmCacheKey. - ScmCache
Key - Cache key: combines the raw query text, its embedding, and a fast FNV-1a hash of the query text used for exact-match lookups.
- ScmCache
Stats - Runtime statistics snapshot from
super::SemanticCacheManager::stats. - Semantic
Cache Manager - A semantic-aware cache backed by cosine similarity search.
Enums§
- ScmCache
Error - Errors returned by
super::SemanticCacheManageroperations. - ScmCache
Hit - Result returned by
super::SemanticCacheManager::lookup. - ScmEviction
Strategy - Determines which entry to remove when the cache is full.
Type Aliases§
- ScmEntry
Alias - Alias: the cache entry type used by
SemanticCacheManager. - ScmError
Alias - Alias: the error type used by
SemanticCacheManager. - ScmHit
Alias - Alias: the cache hit/miss result type used by
SemanticCacheManager. - ScmKey
Alias - Alias: the cache key type used by
SemanticCacheManager. - ScmStats
Alias - Alias: the cache statistics type used by
SemanticCacheManager.