from config import Config
from util import Singleton
import redis


class RedisDB(metaclass=Singleton):
    def __init__(self):
        self.r = redis.Redis(Config().REDIS_ADDR)

    def get_state(self, user_id: int) -> int:
        db_ret = self.r.get(f"{user_id}_test_state")
        return int(db_ret.decode()) if db_ret else None

    def set_state(self, user_id: int, state: int):
        self.r.set(f"{user_id}_test_state", str(state))

    def del_state(self, user_id):
        self.r.delete(f"{user_id}_test_state")
