sync
CI / backend-and-migrations (push) Canceled after 0s
CI / astro-build (push) Canceled after 0s
CI / compose-e2e (push) Canceled after 0s

This commit is contained in:
ik
2026-09-08 09:31:41 +07:00
parent 8b2d7e2e1c
commit ddb6909c4d
21 changed files with 255 additions and 58 deletions
+13 -2
View File
@@ -7,9 +7,15 @@ from typing import Any
class PublicResponseCache:
def __init__(self) -> None:
def __init__(self, max_entries: int = 128) -> None:
self._items: dict[tuple[Any, ...], tuple[float, Any]] = {}
self._lock = Lock()
self._max_entries = max_entries
self._generation = 0
def generation(self) -> int:
with self._lock:
return self._generation
def get(self, key: tuple[Any, ...], ttl_seconds: int) -> Any | None:
with self._lock:
@@ -19,13 +25,18 @@ class PublicResponseCache:
return None
return deepcopy(item[1])
def set(self, key: tuple[Any, ...], value: Any) -> Any:
def set(self, key: tuple[Any, ...], value: Any, *, generation: int | None = None) -> Any:
with self._lock:
if generation is not None and generation != self._generation:
return value
if key not in self._items and len(self._items) >= self._max_entries:
self._items.pop(next(iter(self._items)))
self._items[key] = (monotonic(), deepcopy(value))
return value
def invalidate(self) -> None:
with self._lock:
self._generation += 1
self._items.clear()