(key, value)
| 149 | } |
| 150 | |
| 151 | export function cacheSet(key, value) { |
| 152 | if (!isCacheEnabled()) return; |
| 153 | // Don't cache empty or partial results |
| 154 | if (!value || (!value.text && !(value.chunks && value.chunks.length))) return; |
| 155 | const bytes = valueBytes(value); |
| 156 | const limit = maxBytes(); |
| 157 | deleteEntry(key); |
| 158 | if (!Number.isFinite(bytes) || bytes > limit) { |
| 159 | _stats.skips++; |
| 160 | return; |
| 161 | } |
| 162 | _store.set(key, { value, expiresAt: Date.now() + TTL_MS, bytes }); |
| 163 | _bytes += bytes; |
| 164 | _stats.stores++; |
| 165 | while (_store.size > MAX_ENTRIES || _bytes > limit) { |
| 166 | const oldest = _store.keys().next().value; |
| 167 | if (oldest === undefined) break; |
| 168 | deleteEntry(oldest); |
| 169 | _stats.evictions++; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | export function cacheStats() { |
| 174 | const total = _stats.hits + _stats.misses; |
no test coverage detected