Fix the stale-while-revalidate cache policy
Your task
Your serverless function fetches a user's feature flags from an API. An AI's first cache was a module-level Map, which starts empty on every cold start, and its 1-hour TTL made every request after expiry hit the API at once. The new design keeps one shared cache entry and serves a stale value while one refresh runs, so callers do not stampede the API.
An assistant wrote plan_cache_reads(request_times, fresh_ttl, stale_window), which decides what each request does. It is in the editor. It has a bug. Find it and fix it.
Input and output:
request_times: when each request arrives, in seconds, in non-decreasing order.fresh_ttl: how long a stored value counts as fresh.stale_window: how long after that it may still be served while it refreshes.- Return one action per request:
"fetch","hit"or"stale_revalidate".
Rules the tests check:
- A
fresh_ttlorstale_windowbelow 0 counts as 0. - The first request is a
"fetch", and the entry is stored at its time. - For every later request, the age is its time minus the time the entry was stored:
- age below
fresh_ttl:"hit". - otherwise, age below
fresh_ttl + stale_window:"stale_revalidate". The stale value is served and one refresh starts, and from now on the entry counts as stored at this request's time. - otherwise:
"fetch", and the entry is stored at this request's time.
- age below
Examples
plan_cache_reads(request_times: list[int], fresh_ttl: int, stale_window: int) -> list[str]Example 1
Input
plan_cache_reads([0, 10, 30, 95, 200], 60, 30)Output
["fetch", "hit", "hit", "fetch", "fetch"]Example 2
Input
plan_cache_reads([0, 70, 70, 75], 60, 30)Output
["fetch", "stale_revalidate", "hit", "hit"]Example 3
Input
plan_cache_reads([], 60, 30)Output
[]