fix cached history pagination and setup docs
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Матвей
2026-10-09 21:06:31 +03:00
parent 7cda433085
commit b538b2db39
4 changed files with 33 additions and 5 deletions
+11 -1
View File
@@ -1,5 +1,6 @@
import json
from asyncio import Lock, sleep
from datetime import UTC, datetime
from time import monotonic
from secrets import token_urlsafe
from uuid import UUID
@@ -73,13 +74,22 @@ class HistoryCache:
def _lock_key(cls, chat_id: UUID, version: int, limit: int) -> str:
return cls._key(chat_id, version, limit) + ":lock"
@staticmethod
def _decode_rows(value: str) -> list[dict]:
rows = json.loads(value)
for row in rows:
created_at = row.get("created_at")
if isinstance(created_at, str):
row["created_at"] = datetime.fromisoformat(created_at).astimezone(UTC)
return rows
async def get(self, chat_id: UUID, version: int, limit: int) -> list[dict] | None:
client = await self._client_or_none()
if client is None:
return None
try:
value = await client.get(self._key(chat_id, version, limit))
return json.loads(value) if value else None
return self._decode_rows(value) if value else None
except RedisError:
await self._mark_unavailable(client)
return None