36 lines
688 B
Python
36 lines
688 B
Python
import contextlib
|
|
|
|
import redis
|
|
|
|
import settings
|
|
|
|
|
|
class RedisClient:
|
|
|
|
def __init__(self, host, password=None):
|
|
kwargs = {
|
|
"host": host,
|
|
}
|
|
if password:
|
|
kwargs['password'] = password
|
|
self.cli = redis.Redis(**kwargs)
|
|
|
|
def get(self, key):
|
|
with self.cli as cli:
|
|
return cli.get(f"ruletka_{key}")
|
|
|
|
def set(self, key, value):
|
|
with self.cli as cli:
|
|
cli.set(f"ruletka_{key}", value)
|
|
|
|
@contextlib.contextmanager
|
|
def lock(self, key):
|
|
with self.cli.lock(f"ruletka_{key}"):
|
|
yield
|
|
|
|
|
|
redis_client = RedisClient(
|
|
settings.REDIS_HOST,
|
|
settings.REDIS_PASSWORD
|
|
)
|