A mobile app at PulseFit calls several REST endpoints repeatedly. To reduce unnecessary network requests, implement an in-memory cache that stores API responses by endpoint key and expires entries after a fixed time-to-live.
Write a function that processes a sequence of cache operations and returns the result of each get operation.
Implement process_api_cache(operations, ttl) where:
operations is a list of operations.["set", key, value, timestamp]["get", key, timestamp]["invalidate", key, timestamp]key and value are strings.timestamp is a non-negative integer.ttl is a positive integer.For a get at time t, return the cached value only if the key exists and t - set_timestamp < ttl. Otherwise return "MISS". An invalidate removes the key immediately if present. Return a list containing the outputs of all get operations in order.
Example 1
operations = [["set", "/users", "A", 1], ["get", "/users", 2], ["get", "/users", 5]], ttl = 3["A", "MISS"]Example 2
operations = [["set", "/feed", "X", 10], ["invalidate", "/feed", 11], ["get", "/feed", 12]], ttl = 5["MISS"]1 <= len(operations) <= 10^51 <= ttl <= 10^9