import json | |
import time | |
import logging | |
from core.redis_client import redis_client | |
from utils.config import config | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def save_user_state(user_id: str, state: dict): | |
"""Save user state to Redis with fallback to in-memory storage""" | |
client = redis_client.get_client() | |
if not client: | |
# Fallback: use in-memory storage (will not persist across restarts) | |
logger.info("Redis not available, using in-memory storage for user state") | |
return False | |
try: | |
client.hset(f"user:{user_id}", mapping=state) | |
return True | |
except Exception as e: | |
logger.error(f"Error saving user state: {e}") | |
return False | |
def load_user_state(user_id: str): | |
"""Load user state from Redis with fallback""" | |
client = redis_client.get_client() | |
if not client: | |
logger.info("Redis not available, returning empty state") | |
return {} | |
try: | |
return client.hgetall(f"user:{user_id}") | |
except Exception as e: | |
logger.error(f"Error loading user state: {e}") | |
return {} | |
def check_redis_health(): | |
"""Check if Redis is healthy""" | |
return redis_client.is_healthy() | |