import redis

from allinvest import settings


class RedisClient:

    def __init__(self, host, db=1, password=None):
        kwargs = {
            "host": host,
            "db": db
        }
        if password:
            kwargs['password'] = password
        self.cli = redis.Redis(**kwargs)

    def get(self, key):
        with self.cli as cli:
            return cli.get(key)

    def set(self, key, value):
        with self.cli as cli:
            cli.set(key, value)


redis_client = RedisClient(
    settings.REDIS_HOST,
    settings.REDIS_DB,
    settings.REDIS_PASSWORD
)
