Commit f93e16a
committed
Feat: Implement LRU cache for SurrealDB tool results (Phase 3B.3)
This commit implements transparent LRU caching for all SurrealDB graph
analysis tool calls to reduce redundant database queries in multi-step
agentic workflows.
Changes:
1. AgenticConfig:
- Added enable_cache (bool) field - default true
- Added cache_size (usize) field - default 100 entries
- Updated from_tier() to initialize cache config
2. GraphToolExecutor:
- Added LRU cache with parking_lot::Mutex for thread safety
- Implemented CacheStats struct for observability:
* hits, misses, evictions counters
* current_size and max_size tracking
* hit_rate() calculation method
- Added cache configuration methods:
* with_cache() constructor for custom configuration
* cache_stats() getter for metrics
* clear_cache() for manual invalidation
* cache_key() for deterministic key generation
- Modified execute() to use cache transparently:
* Check cache on entry, return cached result if hit
* Record cache misses
* Cache successful results after execution
* Track LRU evictions
* Update cache statistics atomically
3. Public API (lib.rs):
- Exported CacheStats for external observability
4. Unit Tests:
- test_cache_key_generation() - deterministic keys
- test_cache_stats_initialization() - proper defaults
- test_cache_stats_hit_rate_calculation() - metrics
- test_cache_stats_hit_rate_no_requests() - edge cases
Implementation Notes:
- Uses lru crate (already in workspace dependencies)
- Cache is enabled by default (100 entries ~1MB memory)
- Thread-safe with parking_lot::Mutex
- Zero-heuristic design: only caches successful results
- Transparent operation: no changes to callers required
- Cache key format: "tool_name:json_params" for determinism
Performance Impact:
- Reduces SurrealDB calls for repeated queries
- Expected cache hit rate: 30-60% in typical agentic workflows
- Memory overhead: ~10KB per cached result × cache_size
- Lock contention: minimal (fast cache ops, no DB wait under lock)
Phase 3B.3 Complete (~350 lines added)1 parent 5bde44c commit f93e16a
File tree
3 files changed
+326
-11
lines changed- crates/codegraph-mcp/src
3 files changed
+326
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
27 | 31 | | |
28 | 32 | | |
29 | 33 | | |
| |||
41 | 45 | | |
42 | 46 | | |
43 | 47 | | |
| 48 | + | |
| 49 | + | |
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
| |||
81 | 87 | | |
82 | 88 | | |
83 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
84 | 188 | | |
85 | 189 | | |
86 | 190 | | |
| |||
179 | 283 | | |
180 | 284 | | |
181 | 285 | | |
182 | | - | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
183 | 292 | | |
184 | 293 | | |
185 | 294 | | |
186 | 295 | | |
187 | 296 | | |
188 | 297 | | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
189 | 306 | | |
190 | 307 | | |
191 | 308 | | |
| |||
0 commit comments