From 5d221415cc048da7e9314b62c252e5f5c164a39b Mon Sep 17 00:00:00 2001 From: austnv Date: Sun, 14 Jun 2026 23:04:11 +0300 Subject: [PATCH] add cache to /api/v1/tariffs and i/o-bound operations (sync read from file) which locked async event loop has moved to asyncio thread --- routers/tariffs.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/routers/tariffs.py b/routers/tariffs.py index 87e4a93..549e607 100644 --- a/routers/tariffs.py +++ b/routers/tariffs.py @@ -1,9 +1,15 @@ -from fastapi import APIRouter, Request -import aiofiles +from fastapi import APIRouter +from functools import lru_cache +import json +import asyncio router = APIRouter(prefix='/api/v1', tags=['tariffs']) +@lru_cache(maxsize=1) +def get_tariffs_sync(): + with open('data/tariffs.json', 'r', encoding='utf-8') as f: + return json.load(f) + @router.get('/tariffs') -async def get_tariffs(request: Request): - async with aiofiles.open('data/tariffs.json', 'r', encoding='utf-8') as file: - return await file.read() \ No newline at end of file +async def get_tariffs(): + return await asyncio.to_thread(get_tariffs_sync) \ No newline at end of file