put method
Caches a response.
Implementation
void put(String prompt, String model, String response,
{List<String>? context}) {
final key = _generateKey(prompt, model, context);
// Check if this is an update to an existing entry
final isUpdate = _cache.containsKey(key);
// Remove existing key from access order to prevent duplicates
if (isUpdate) {
_accessOrder.remove(key);
}
// Evict oldest if at capacity (only for new entries)
if (!isUpdate) {
while (_cache.length >= maxEntries && _accessOrder.isNotEmpty) {
final oldest = _accessOrder.removeAt(0);
_cache.remove(oldest);
}
}
_cache[key] = _CacheEntry(
response: response,
expiresAt: DateTime.now().add(ttl),
);
_accessOrder.add(key);
_saveToDisk();
}