import redis
import msgpack
from datetime import datetime
from typing import Any

# Initialize Redis connection
redis_client = redis.Redis(host="127.0.0.1", port=6379, db=0)

def encode_datetime(obj: Any) -> Any:
    """Serialize datetime objects to dictionary format."""
    if isinstance(obj, datetime):
        return {"__datetime__": True, "as_str": obj.strftime("%Y%m%dT%H:%M:%S.%f")}
    return obj

def decode_datetime(obj: Any) -> Any:
    """Deserialize dictionary format to datetime objects."""
    if "__datetime__" in obj:
        return datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
    return obj
