File size: 1,241 Bytes
5420da2
3c74ffa
1fe6f63
45df059
5420da2
 
1fe6f63
 
 
 
5420da2
84ae379
45df059
 
84ae379
1fe6f63
3c74ffa
45df059
5420da2
45df059
5420da2
 
1fe6f63
5420da2
 
 
84ae379
45df059
 
1fe6f63
3c74ffa
45df059
5420da2
45df059
5420da2
1fe6f63
5420da2
3c74ffa
 
 
45df059
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()