"""Thread-safe bounded TTL/LRU for completed materialized retrieval results.""" import copy import json import math import threading import time from collections import OrderedDict class ResultCache: def __init__(self, *, ttl=1.0,max_entries=4,max_bytes=65536,clock=time.monotonic): if isinstance(ttl,bool) or not isinstance(ttl,(int,float)) or not math.isfinite(ttl) or not 0self.max_bytes:return False self.entries[key]=(self.clock()+self.ttl,frozen,size);self.bytes+=size while len(self.entries)>self.max_entries or self.bytes>self.max_bytes:self._drop(next(iter(self.entries))) return True def clear(self): with self.lock:self.entries.clear();self.bytes=0