fix: add async cache to async operations and sync cache (stdlib) for sync operations

This commit is contained in:
austnv
2026-06-14 23:23:05 +03:00
parent 5d221415cc
commit ccf99af7e1
4 changed files with 39 additions and 17 deletions
+8 -7
View File
@@ -1,15 +1,16 @@
from fastapi import APIRouter
from functools import lru_cache
import aiofiles
import json
import asyncio
from async_lru import alru_cache
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)
@alru_cache(maxsize=1, ttl=86400)
async def get_tariffs_sync():
async with aiofiles.open('data/tariffs.json', 'r', encoding='utf-8') as f:
content = await f.read()
return json.loads(content)
@router.get('/tariffs')
async def get_tariffs():
return await asyncio.to_thread(get_tariffs_sync)
return await get_tariffs_sync()