"""Cache module.""" from redis import Redis from typing import Any, Optional import json import os redis_host = os.getenv("REDIS_HOST", "redis") redis_port = int(os.getenv("REDIS_PORT", "6379")) redis_client = Redis(host=redis_host, port=redis_port, decode_responses=True) def init_cache() -> None: """Initialize cache connection.""" pass def get(key: str) -> Optional[Any]: """Get value from cache.""" value = redis_client.get(key) if value: return json.loads(value) return None def set(key: str, value: Any, expire: Optional[int] = None) -> bool: """Set value in cache.""" serialized = json.dumps(value) if expire: return redis_client.setex(key, expire, serialized) return redis_client.set(key, serialized) def delete(key: str) -> bool: """Delete value from cache.""" return redis_client.delete(key) def clear() -> bool: """Clear all cache.""" return redis_client.flushdb()