fix: add async cache to async operations and sync cache (stdlib) for sync operations
This commit is contained in:
+8
-7
@@ -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()
|
||||
Reference in New Issue
Block a user