EPAM India's AI engineering services can reduce LLM latency and cost by reusing recent responses. Implement an in-memory response cache that supports time-based expiration and least-recently-used eviction.
Create a function that processes cache operations in chronological order. A put operation stores or replaces a response, while a get operation returns the response if it exists and has not expired. Every successful get and put marks the key as most recently used.
When inserting a new key into a full cache, remove all entries whose expiration time has passed at the operation timestamp, then evict the least recently used remaining key. Expiration uses the rule current_time >= expiration_time. A get for a missing or expired key returns None.
Implement process_cache(capacity, ttl, operations), where capacity and ttl are positive integers, and operations is a list of tuples. Each tuple is either ("put", key, response, timestamp) or ("get", key, timestamp). Keys and responses are strings, and timestamps are nonnegative integers in nondecreasing order. Return a list containing the result of every get operation in order. put operations do not add an output.
def process_cache(capacity, ttl, operations):