mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge pull request #145 from gabbhack/redis_storage
Redis storage update
This commit is contained in:
commit
0c732236fc
1 changed files with 14 additions and 12 deletions
|
|
@ -35,7 +35,6 @@ class RedisStorage(BaseStorage):
|
||||||
await dp.storage.wait_closed()
|
await dp.storage.wait_closed()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, host='localhost', port=6379, db=None, password=None, ssl=None, loop=None, **kwargs):
|
def __init__(self, host='localhost', port=6379, db=None, password=None, ssl=None, loop=None, **kwargs):
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
|
|
@ -62,8 +61,6 @@ class RedisStorage(BaseStorage):
|
||||||
async def redis(self) -> aioredis.RedisConnection:
|
async def redis(self) -> aioredis.RedisConnection:
|
||||||
"""
|
"""
|
||||||
Get Redis connection
|
Get Redis connection
|
||||||
|
|
||||||
This property is awaitable.
|
|
||||||
"""
|
"""
|
||||||
# Use thread-safe asyncio Lock because this method without that is not safe
|
# Use thread-safe asyncio Lock because this method without that is not safe
|
||||||
async with self._connection_lock:
|
async with self._connection_lock:
|
||||||
|
|
@ -222,9 +219,12 @@ class RedisStorage2(BaseStorage):
|
||||||
await dp.storage.wait_closed()
|
await dp.storage.wait_closed()
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, host: str = 'localhost', port=6379, db=None, password=None,
|
||||||
def __init__(self, host='localhost', port=6379, db=None, password=None, ssl=None,
|
ssl=None, pool_size=10, loop=None, prefix='fsm',
|
||||||
pool_size=10, loop=None, prefix='fsm', **kwargs):
|
state_ttl: int = 0,
|
||||||
|
data_ttl: int = 0,
|
||||||
|
bucket_ttl: int = 0,
|
||||||
|
**kwargs):
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
self._db = db
|
self._db = db
|
||||||
|
|
@ -235,14 +235,16 @@ class RedisStorage2(BaseStorage):
|
||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
self._prefix = (prefix,)
|
self._prefix = (prefix,)
|
||||||
|
|
||||||
|
self._state_ttl = state_ttl
|
||||||
|
self._data_ttl = data_ttl
|
||||||
|
self._bucket_ttl = bucket_ttl
|
||||||
|
|
||||||
self._redis: aioredis.RedisConnection = None
|
self._redis: aioredis.RedisConnection = None
|
||||||
self._connection_lock = asyncio.Lock(loop=self._loop)
|
self._connection_lock = asyncio.Lock(loop=self._loop)
|
||||||
|
|
||||||
async def redis(self) -> aioredis.Redis:
|
async def redis(self) -> aioredis.Redis:
|
||||||
"""
|
"""
|
||||||
Get Redis connection
|
Get Redis connection
|
||||||
|
|
||||||
This property is awaitable.
|
|
||||||
"""
|
"""
|
||||||
# Use thread-safe asyncio Lock because this method without that is not safe
|
# Use thread-safe asyncio Lock because this method without that is not safe
|
||||||
async with self._connection_lock:
|
async with self._connection_lock:
|
||||||
|
|
@ -294,14 +296,14 @@ class RedisStorage2(BaseStorage):
|
||||||
if state is None:
|
if state is None:
|
||||||
await redis.delete(key)
|
await redis.delete(key)
|
||||||
else:
|
else:
|
||||||
await redis.set(key, state)
|
await redis.set(key, state, expire=self._state_ttl)
|
||||||
|
|
||||||
async def set_data(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
|
async def set_data(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
|
||||||
data: typing.Dict = None):
|
data: typing.Dict = None):
|
||||||
chat, user = self.check_address(chat=chat, user=user)
|
chat, user = self.check_address(chat=chat, user=user)
|
||||||
key = self.generate_key(chat, user, STATE_DATA_KEY)
|
key = self.generate_key(chat, user, STATE_DATA_KEY)
|
||||||
redis = await self.redis()
|
redis = await self.redis()
|
||||||
await redis.set(key, json.dumps(data))
|
await redis.set(key, json.dumps(data), expire=self._data_ttl)
|
||||||
|
|
||||||
async def update_data(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
|
async def update_data(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
|
||||||
data: typing.Dict = None, **kwargs):
|
data: typing.Dict = None, **kwargs):
|
||||||
|
|
@ -329,7 +331,7 @@ class RedisStorage2(BaseStorage):
|
||||||
chat, user = self.check_address(chat=chat, user=user)
|
chat, user = self.check_address(chat=chat, user=user)
|
||||||
key = self.generate_key(chat, user, STATE_BUCKET_KEY)
|
key = self.generate_key(chat, user, STATE_BUCKET_KEY)
|
||||||
redis = await self.redis()
|
redis = await self.redis()
|
||||||
await redis.set(key, json.dumps(bucket))
|
await redis.set(key, json.dumps(bucket), expire=self._bucket_ttl)
|
||||||
|
|
||||||
async def update_bucket(self, *, chat: typing.Union[str, int, None] = None,
|
async def update_bucket(self, *, chat: typing.Union[str, int, None] = None,
|
||||||
user: typing.Union[str, int, None] = None,
|
user: typing.Union[str, int, None] = None,
|
||||||
|
|
@ -338,7 +340,7 @@ class RedisStorage2(BaseStorage):
|
||||||
bucket = {}
|
bucket = {}
|
||||||
temp_bucket = await self.get_bucket(chat=chat, user=user)
|
temp_bucket = await self.get_bucket(chat=chat, user=user)
|
||||||
temp_bucket.update(bucket, **kwargs)
|
temp_bucket.update(bucket, **kwargs)
|
||||||
await self.set_bucket(chat=chat, user=user, data=temp_bucket)
|
await self.set_bucket(chat=chat, user=user, bucket=temp_bucket)
|
||||||
|
|
||||||
async def reset_all(self, full=True):
|
async def reset_all(self, full=True):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue