Files
mtusi2026lab2/app/cache.py
T
dawuduk 8f1d7e27c7
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / application (pull_request) Canceled after 0s
reworked
2026-11-07 11:56:38 +03:00

152 lines
4.0 KiB
Python

import asyncio
import json
from collections.abc import Awaitable, Callable
from typing import TypeVar
from app import config, rediscli
from app.errors import AppError
T = TypeVar("T")
CFG_SINGLEFLIGHT = "cfg:singleflight"
CFG_INVALIDATE = "cfg:invalidate"
CFG_DELAY_MS = "cfg:delay_ms"
SQL_HISTORY = "metrics:sql:messages_select"
class SingleFlight:
def __init__(self) -> None:
self._lock = asyncio.Lock()
self._inflight: dict[str, asyncio.Task] = {}
async def do(self, key: str, factory: Callable[[], Awaitable[T]]) -> T:
async with self._lock:
task = self._inflight.get(key)
if task is None:
task = asyncio.create_task(factory())
self._inflight[key] = task
owner = True
else:
owner = False
try:
return await asyncio.shield(task)
finally:
if owner:
async with self._lock:
self._inflight.pop(key, None)
_sf = SingleFlight()
async def _flag(name: str, default: bool = True) -> bool:
if not config.lab_mode():
return default
try:
raw = await rediscli.require().get(name)
except Exception:
return default
if raw is None:
return default
return raw not in {"0", "false", "off"}
async def singleflight_enabled() -> bool:
return await _flag(CFG_SINGLEFLIGHT, True)
async def invalidate_enabled() -> bool:
return await _flag(CFG_INVALIDATE, True)
async def incr_history_sql() -> None:
try:
await rediscli.require().incr(SQL_HISTORY)
except Exception:
pass
async def history_sql_count() -> int:
try:
return int(await rediscli.require().get(SQL_HISTORY) or 0)
except Exception:
return 0
async def reset_history_sql_count() -> None:
try:
await rediscli.require().set(SQL_HISTORY, 0)
except Exception:
pass
async def load_delay_seconds() -> float:
if not config.lab_mode():
return 0.0
try:
raw = await rediscli.require().get(CFG_DELAY_MS)
except Exception:
return 0.0
try:
return max(0.0, float(raw or 0) / 1000.0)
except (TypeError, ValueError):
return 0.0
async def set_flags(
*,
singleflight: bool | None = None,
invalidate: bool | None = None,
delay_ms: int | None = None,
) -> dict:
r = rediscli.require()
if singleflight is not None:
await r.set(CFG_SINGLEFLIGHT, "1" if singleflight else "0")
if invalidate is not None:
await r.set(CFG_INVALIDATE, "1" if invalidate else "0")
if delay_ms is not None:
await r.set(CFG_DELAY_MS, str(int(delay_ms)))
return {
"singleflight": await singleflight_enabled(),
"invalidate": await invalidate_enabled(),
"history_sql": await history_sql_count(),
"delay_ms": int((await load_delay_seconds()) * 1000),
}
async def get_history_page(chat_id: str, cursor: str, limit: int, loader: Callable[[], Awaitable[dict]]) -> dict:
r = rediscli.require()
try:
version = await r.get("histver:" + chat_id) or "0"
key = f"hist:{chat_id}:{version}:{cursor}:{limit}"
cached = await r.get(key)
if cached:
return json.loads(cached)
async def load_and_store() -> dict:
delay = await load_delay_seconds()
if delay:
await asyncio.sleep(delay)
page = await loader()
try:
await r.set(key, json.dumps(page, ensure_ascii=False), ex=config.CACHE_TTL_SECONDS)
except Exception:
pass
return page
if await singleflight_enabled():
return await _sf.do(key, load_and_store)
return await load_and_store()
except AppError:
raise
except Exception:
return await loader()
async def invalidate_history(chat_id: str) -> None:
if not await invalidate_enabled():
return
try:
await rediscli.require().incr("histver:" + chat_id)
except Exception:
pass