"""SPOILER: reference implementation for the published synthetic exercise.

This covers the exercise contract, not a production cache implementation.
Capacity limits, concurrent access and wall-clock rollback are out of scope.
"""


class TTLCache:
    def __init__(self, clock):
        self.clock = clock
        self.entries = {}

    def set(self, key, value, ttl=60):
        self.entries[key] = (value, self.clock() + ttl)

    def get(self, key, default=None):
        entry = self.entries.get(key)
        if entry is None:
            return default
        value, expires_at = entry
        if self.clock() >= expires_at:
            del self.entries[key]
            return default
        return value
