SPB Git forge

spb/uqo-chat

Public
14commits 1branches 0releases
1.4 MBsize
maindefault branch
17 days agolast push
Python 64.6% TypeScript 33.7% CSS 0.8%
831 B · 32 lines python
Raw Blame History
1"""Tiny TTL cache (in-memory). Redis can replace it via REDIS_URL later."""23from __future__ import annotations45import time6from typing import Any789class TTLCache:10    def __init__(self) -> None:11        self._data: dict[str, tuple[float, Any]] = {}1213    def get(self, key: str) -> Any | None:14        item = self._data.get(key)15        if not item:16            return None17        exp, value = item18        if exp < time.time():19            self._data.pop(key, None)20            return None21        return value2223    def set(self, key: str, value: Any, ttl_s: int) -> None:24        if len(self._data) > 5000:25            now = time.time()26            for k in [k for k, (e, _) in self._data.items() if e < now]:27                self._data.pop(k, None)28        self._data[key] = (time.time() + ttl_s, value)293031cache = TTLCache()32