* Cache a response with optional custom TTL.
(
key: string,
response: {
body: Buffer;
status: number;
headers: Record<string, string>;
model: string;
},
ttlSeconds?: number,
)
| 188 | * Cache a response with optional custom TTL. |
| 189 | */ |
| 190 | set( |
| 191 | key: string, |
| 192 | response: { |
| 193 | body: Buffer; |
| 194 | status: number; |
| 195 | headers: Record<string, string>; |
| 196 | model: string; |
| 197 | }, |
| 198 | ttlSeconds?: number, |
| 199 | ): void { |
| 200 | // Don't cache if disabled or maxSize is 0 |
| 201 | if (!this.config.enabled || this.config.maxSize <= 0) return; |
| 202 | |
| 203 | // Don't cache if item too large |
| 204 | if (response.body.length > this.config.maxItemSize) { |
| 205 | console.log(`[ResponseCache] Skipping cache - item too large: ${response.body.length} bytes`); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Don't cache error responses |
| 210 | if (response.status >= 400) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | // Evict if at capacity |
| 215 | if (this.cache.size >= this.config.maxSize) { |
| 216 | this.evict(); |
| 217 | } |
| 218 | |
| 219 | const now = Date.now(); |
| 220 | const ttl = ttlSeconds ?? this.config.defaultTTL; |
| 221 | const expiresAt = now + ttl * 1000; |
| 222 | |
| 223 | const entry: CachedLLMResponse = { |
| 224 | ...response, |
| 225 | cachedAt: now, |
| 226 | expiresAt, |
| 227 | }; |
| 228 | |
| 229 | this.cache.set(key, entry); |
| 230 | this.expirationHeap.push({ expiresAt, key }); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Evict expired and oldest entries to make room. |
no test coverage detected