This commit is contained in:
2024-04-29 15:05:05 +03:00
parent c6f5c44d00
commit 7dc6df0182
5 changed files with 59 additions and 14 deletions

35
tools/redis.py Normal file
View File

@@ -0,0 +1,35 @@
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
)