Skip to main content

Module cache

Module cache 

Source
Expand description

Query result caching for Thread pipeline

This module provides LRU caching for frequently accessed query results, reducing database round-trips and improving response times.

§Features

  • Async-first: Built on moka’s async cache for tokio compatibility
  • Type-safe: Generic caching with compile-time type checking
  • TTL support: Configurable time-to-live for cache entries
  • Statistics: Track cache hit/miss rates for monitoring
  • Size limits: Automatic eviction when cache exceeds capacity

§Usage

use thread_flow::cache::{QueryCache, CacheConfig};
use thread_services::conversion::Fingerprint;

// Create cache with 1000 entry limit, 5 minute TTL
let cache = QueryCache::new(CacheConfig {
    max_capacity: 1000,
    ttl_seconds: 300,
});

// Cache symbol query results
let fingerprint = compute_content_fingerprint("fn main() {}");
cache.insert(fingerprint, symbols).await;

// Retrieve from cache
if let Some(symbols) = cache.get(&fingerprint).await {
    // Cache hit - saved D1 query!
}

§Performance Impact

ScenarioWithout CacheWith CacheSavings
Symbol lookup50-100ms (D1)<1µs (memory)99.9%
Metadata query20-50ms (D1)<1µs (memory)99.9%
Re-analysis (90% hit)100ms total10ms total90%

Structs§

CacheConfig
Configuration for query result cache
CacheStats
Cache statistics for monitoring
QueryCache
No-op cache for when caching feature is disabled