Michael Manganiello 80291f4be1
misc: Move cache initialization to startup script
Guarantee that cache is initialized during startup, and only once,
instead of every time a `MetadataHandler` object is instantiated.

Also, improve logic to determine `fixtures` paths.
2025-08-13 12:26:15 -03:00

25 lines
1.0 KiB
Python

import json
from itertools import batched
from pathlib import Path
from anyio import open_file
from logger.logger import log
from redis.asyncio import Redis as AsyncRedis
async def conditionally_set_cache(cache: AsyncRedis, key: str, file_path: Path) -> None:
"""Set the content of a JSON file to the cache, if it does not already exist."""
try:
if await cache.exists(key):
return
async with await open_file(file_path, "r") as file:
index_data = json.loads(await file.read())
async with cache.pipeline() as pipe:
for data_batch in batched(index_data.items(), 2000, strict=False):
data_map = {k: json.dumps(v) for k, v in dict(data_batch).items()}
await pipe.hset(key, mapping=data_map)
await pipe.execute()
except Exception as e:
# Log the error but don't fail - this allows migrations to run even if Redis is not available
log.warning(f"Failed to initialize cache for {key}: {e}")