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
| Scenario | Without Cache | With Cache | Savings |
|---|---|---|---|
| Symbol lookup | 50-100ms (D1) | <1µs (memory) | 99.9% |
| Metadata query | 20-50ms (D1) | <1µs (memory) | 99.9% |
| Re-analysis (90% hit) | 100ms total | 10ms total | 90% |
Structs§
- Cache
Config - Configuration for query result cache
- Cache
Stats - Cache statistics for monitoring
- Query
Cache - No-op cache for when caching feature is disabled