import os

import redis


def get_redis():
    password = os.getenv("REDIS_PASSWORD", None)
    kwargs = {
        "host": os.getenv("REDIS_HOST", "127.0.0.1")
    }
    if password:
        kwargs['password'] = password
    return redis.Redis(**kwargs)


def get(key):
    with get_redis() as r:
        return r.get(key)


def set(key, value):
    with get_redis() as r:
        return r.set(key, value)


def lock(name='lock'):
    def dec(fun):
        def wrapper(*args, **kwargs):
            with get_redis() as r:
                with r.lock(name):
                    return fun(*args, **kwargs)

        return wrapper
    return dec
