feat: cache chat cards in Valkey

Valkey 9.1 in compose caches the immutable chat card (title, members,
created_at) used by GET /chats/{id} and by the membership check of the
message endpoints. Cache-aside with a write after commit, keys per
database, TTL plus allkeys-lru, persistence off. redis-py runs without
retries; on failure the cache is skipped for a few seconds and requests
go to PostgreSQL. Readiness does not depend on Valkey.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 21:17:55 +03:00
co-authored by Claude Opus 5.5
parent c7681b0e18
commit cae4b09147
46 changed files with 1769 additions and 210 deletions
+23
View File
@@ -93,6 +93,28 @@ class Auth(BaseModel):
kdf_concurrency: int = Field(default=4, gt=0)
class Cache(BaseModel):
"""Подключение к Valkey (кэш карточек чатов).
Адрес и пароль приходят из `.env`: в console — мостами к `VALKEY_*`,
в docker — из `environment` compose. Таймауты и срок записей — из
`config.toml`.
"""
host: str = "127.0.0.1"
port: int = Field(default=6379, ge=1, le=65535)
password: SecretStr = SecretStr("")
# секунды на соединение и ответ; дольше — запрос идёт в PostgreSQL
timeout_seconds: float = Field(default=0.2, gt=0)
# секунды, на которые кэш пропускается после отказа Valkey
retry_after_seconds: float = Field(default=5, gt=0)
# секунды жизни карточки чата в кэше
chat_ttl_seconds: int = Field(default=3600, gt=0)
# соединений в пуле клиента
max_connections: int = Field(default=10, ge=1)
class Settings(BaseSettings):
model_config = SettingsConfigDict(
extra="ignore",
@@ -105,6 +127,7 @@ class Settings(BaseSettings):
app: App = App()
database: Database = Database()
auth: Auth = Auth()
cache: Cache = Cache()
@classmethod
def settings_customise_sources(