"""Redis-backed result cache; degrades to no-op when Redis is unavailable.""" import json import logging import redis.asyncio as aioredis log = logging.getLogger("orchestrator") class Cache: def __init__(self, redis_url: str | None): self._redis = None if redis_url: self._redis = aioredis.from_url(redis_url, socket_timeout=2, socket_connect_timeout=2) else: log.warning("no REDIS_URL configured; orchestrator result cache disabled") async def get_json(self, key: str) -> dict | None: if not self._redis: return None try: raw = await self._redis.get(key) return json.loads(raw) if raw else None except Exception: return None async def set_json(self, key: str, value: dict, ttl: int) -> None: if not self._redis: return try: await self._redis.set(key, json.dumps(value), ex=ttl) except Exception: pass async def is_ready(self) -> bool: if not self._redis: return False try: return await self._redis.ping() except Exception: return False async def close(self) -> None: if self._redis: await self._redis.aclose()