Insert a result into the cache. Evicts old entries if over budget.
(&mut self, tenant_id: u64, partition_id: i64, query_hash: u64, result: Vec<u8>)
| 48 | |
| 49 | /// Insert a result into the cache. Evicts old entries if over budget. |
| 50 | pub fn insert(&mut self, tenant_id: u64, partition_id: i64, query_hash: u64, result: Vec<u8>) { |
| 51 | let key = (tenant_id, partition_id, query_hash); |
| 52 | |
| 53 | // Don't cache if the single entry exceeds budget. |
| 54 | if result.len() > self.max_bytes { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | // Remove old entry for this key if exists. |
| 59 | if let Some(old) = self.entries.remove(&key) { |
| 60 | self.current_bytes -= old.len(); |
| 61 | self.order.retain(|k| k != &key); |
| 62 | } |
| 63 | |
| 64 | // Evict until we have room. |
| 65 | while self.current_bytes + result.len() > self.max_bytes { |
| 66 | if let Some(evict_key) = self.order.pop_front() { |
| 67 | if let Some(evicted) = self.entries.remove(&evict_key) { |
| 68 | self.current_bytes -= evicted.len(); |
| 69 | } |
| 70 | } else { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | self.current_bytes += result.len(); |
| 76 | self.order.push_back(key); |
| 77 | self.entries.insert(key, result); |
| 78 | } |
| 79 | |
| 80 | /// Invalidate all cached entries for a partition (e.g., if it's modified). |
| 81 | /// |