This commit is contained in:
2023-05-22 12:23:31 +02:00
parent cd6f28cdbc
commit 688e673364
2 changed files with 11 additions and 5 deletions

View File

@@ -1,11 +1,13 @@
from fastapi import HTTPException, Request, status
from fastapi import HTTPException, status
from redis.asyncio import ConnectionPool
from app.models import CachedFile as CachedFileDB
from app.services.cache_updater import cache_file_by_book_id
async def get_cached_file_or_cache(
request: Request, object_id: int, object_type: str
object_id: int, object_type: str, connection_pool: ConnectionPool
) -> CachedFileDB:
cached_file = await CachedFileDB.objects.get_or_none(
object_id=object_id, object_type=object_type
@@ -13,7 +15,7 @@ async def get_cached_file_or_cache(
if not cached_file:
cached_file = await cache_file_by_book_id(
{"redis": request.app.state.redis_client}, object_id, object_type
object_id, object_type, redis_pool=connection_pool
)
if not cached_file:

View File

@@ -40,7 +40,9 @@ async def get_cached_file(request: Request, object_id: int, object_type: str):
@router.get("/download/{object_id}/{object_type}")
async def download_cached_file(request: Request, object_id: int, object_type: str):
cached_file = await get_cached_file_or_cache(request, object_id, object_type)
cached_file = await get_cached_file_or_cache(
object_id, object_type, request.app.state.redis_pool
)
cache_data: dict = cached_file.data # type: ignore
data = await download_file_from_cache(
@@ -49,7 +51,9 @@ async def download_cached_file(request: Request, object_id: int, object_type: st
if data is None:
await CachedFileDB.objects.filter(id=cached_file.id).delete()
cached_file = await get_cached_file_or_cache(request, object_id, object_type)
cached_file = await get_cached_file_or_cache(
object_id, object_type, request.app.state.redis_pool
)
cache_data: dict = cached_file.data # type: ignore
data = await download_file_from_cache(