Get cached execution metadata for a query
(&self, query_key: &str)
| 49 | |
| 50 | /// Get cached execution metadata for a query |
| 51 | pub fn get(&self, query_key: &str) -> Option<ExecutionMetadata> { |
| 52 | // Use fingerprint with literals for queries without parameters |
| 53 | // to avoid cache collisions between queries like "SELECT 42" and "SELECT 9999" |
| 54 | let fingerprint = if query_key.contains('#') { |
| 55 | // Has parameter types, use regular fingerprint |
| 56 | QueryFingerprint::generate(query_key) |
| 57 | } else { |
| 58 | // No parameters, preserve literals to avoid collisions |
| 59 | QueryFingerprint::generate_with_literals(query_key) |
| 60 | }; |
| 61 | let mut cache = self.cache.write().unwrap(); |
| 62 | |
| 63 | if let Some(entry) = cache.get_mut(&fingerprint) { |
| 64 | if entry.cached_at.elapsed() < self.ttl { |
| 65 | entry.hit_count += 1; |
| 66 | return Some(entry.metadata.clone()); |
| 67 | } else { |
| 68 | // Entry expired, remove it |
| 69 | cache.remove(&fingerprint); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | None |
| 74 | } |
| 75 | |
| 76 | /// Cache execution metadata for a query |
| 77 | pub fn insert(&self, query_key: String, metadata: ExecutionMetadata) { |