redis
This commit is contained in:
35
tools/redis.py
Normal file
35
tools/redis.py
Normal 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
|
||||
)
|
||||
Reference in New Issue
Block a user