Never call time.time() inside the class. Take a clock argument, a function that returns the current time in seconds, and default it to time.monotonic. There are two reasons.
time.time() is the wall clock, and the system can set it backwards or forwards. Every TTL in the cache would jump with it. time.monotonic() only moves forward. Its docs: "The clock is not affected by system clock updates."
A test can pass a fake clock and move time by hand. There's no sleeping and no flaky timing, and the boundary is easy to hit exactly:
class FakeClock:
def __init__(self):
self.now = 0
def __call__(self):
return self.now
clock = FakeClock()
print(clock())
clock.now = 10
print(clock())
0
10
clock is called like time.monotonic: a __call__ method makes an object callable, so clock() runs clock.__call__(). Setting clock.now moves time.
In the editor, write FakeClock again, now with a start time and an advance(seconds) method that moves time forward and never back.