"""{{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

Simple TTL cache backing repeated `search`/`get` lookups.
"""

from __future__ import annotations

import time
from dataclasses import dataclass
from typing import Generic, TypeVar

T = TypeVar("T")


@dataclass
class _CacheEntry(Generic[T]):
    value: T
    expires_at: float


class QueryCache(Generic[T]):
    def __init__(self, max_size: int = 500, ttl_s: float = 60.0) -> None:
        self._max_size = max_size
        self._ttl_s = ttl_s
        self._store: dict[str, _CacheEntry[T]] = {}

    def get(self, key: str) -> T | None:
        entry = self._store.get(key)
        if entry is None:
            return None
        if time.monotonic() > entry.expires_at:
            del self._store[key]
            return None
        return entry.value

    def set(self, key: str, value: T) -> None:
        if len(self._store) >= self._max_size and key not in self._store:
            oldest_key = next(iter(self._store))
            del self._store[oldest_key]
        self._store[key] = _CacheEntry(value=value, expires_at=time.monotonic() + self._ttl_s)

    def clear(self) -> None:
        self._store.clear()
