Перенести реализацию мессенджера из ЛР 6

Файлы скопированы без изменений из solution/BPI2301-Vozzhin
репозитория mtusi2026lab6. Доработки ЛР 7 идут отдельными коммитами.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-12-03 19:48:41 +05:00
co-authored by Claude Opus 5.5
parent 20afc2c790
commit 31d441184b
92 changed files with 6153 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from uuid import UUID
from redis.asyncio import Redis
from redis.exceptions import RedisError
from ..auth.tokens import session_key
from ..domain.errors import Unavailable
def create_redis(url: str) -> Redis:
return Redis.from_url(url, decode_responses=True, socket_connect_timeout=3, socket_timeout=3)
class RedisSessionStore:
def __init__(self, client: Redis, ttl_seconds: int) -> None:
self._client = client
self._ttl_seconds = ttl_seconds
async def create(self, user_id: UUID, token: str) -> datetime:
try:
await self._client.set(session_key(token), str(user_id), ex=self._ttl_seconds)
except RedisError as exc:
raise Unavailable('хранилище сессий недоступно') from exc
return datetime.now(timezone.utc) + timedelta(seconds=self._ttl_seconds)
async def get_user_id(self, token: str) -> UUID | None:
try:
value = await self._client.get(session_key(token))
except RedisError as exc:
raise Unavailable('хранилище сессий недоступно') from exc
if value is None:
return None
try:
return UUID(value)
except ValueError:
return None
async def revoke(self, token: str) -> None:
try:
await self._client.delete(session_key(token))
except RedisError as exc:
raise Unavailable('хранилище сессий недоступно') from exc
async def ping(self) -> None:
try:
await self._client.ping()
except RedisError as exc:
raise Unavailable('хранилище сессий недоступно') from exc