Skip to content

Commit

Permalink
Redis storage speedup globals (#652)
Browse files Browse the repository at this point in the history
* chore: redis storage speedup globals #651

* chore: temp ignore aioredis typing issue

* Added patch-notes

Co-authored-by: Alex Root Junior <jroot.junior@gmail.com>
  • Loading branch information
Olegt0rr and JrooTJunior committed Aug 3, 2021
1 parent 89f7deb commit fff33e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/651.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Redis storage speedup globals
50 changes: 39 additions & 11 deletions aiogram/dispatcher/fsm/storage/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,67 @@ def generate_key(self, bot: Bot, *parts: Any) -> str:
return ":".join(map(str, prefix_parts))

@asynccontextmanager
async def lock(self, bot: Bot, chat_id: int, user_id: int) -> AsyncGenerator[None, None]:
key = self.generate_key(bot, chat_id, user_id, STATE_LOCK_KEY)
async def lock(
self, bot: Bot, chat_id: int, user_id: int, state_lock_key: str = STATE_LOCK_KEY
) -> AsyncGenerator[None, None]:
key = self.generate_key(bot, chat_id, user_id, state_lock_key)
async with self.redis.lock(name=key, **self.lock_kwargs):
yield None

async def set_state(
self, bot: Bot, chat_id: int, user_id: int, state: StateType = None
self,
bot: Bot,
chat_id: int,
user_id: int,
state: StateType = None,
state_key: str = STATE_KEY,
) -> None:
key = self.generate_key(bot, chat_id, user_id, STATE_KEY)
key = self.generate_key(bot, chat_id, user_id, state_key)
if state is None:
await self.redis.delete(key)
else:
await self.redis.set(
key, state.state if isinstance(state, State) else state, ex=self.state_ttl # type: ignore[arg-type]
key,
state.state if isinstance(state, State) else state, # type: ignore[arg-type]
ex=self.state_ttl, # type: ignore[arg-type]
)

async def get_state(self, bot: Bot, chat_id: int, user_id: int) -> Optional[str]:
key = self.generate_key(bot, chat_id, user_id, STATE_KEY)
async def get_state(
self,
bot: Bot,
chat_id: int,
user_id: int,
state_key: str = STATE_KEY,
) -> Optional[str]:
key = self.generate_key(bot, chat_id, user_id, state_key)
value = await self.redis.get(key)
if isinstance(value, bytes):
return value.decode("utf-8")
return cast(Optional[str], value)

async def set_data(self, bot: Bot, chat_id: int, user_id: int, data: Dict[str, Any]) -> None:
key = self.generate_key(bot, chat_id, user_id, STATE_DATA_KEY)
async def set_data(
self,
bot: Bot,
chat_id: int,
user_id: int,
data: Dict[str, Any],
state_data_key: str = STATE_DATA_KEY,
) -> None:
key = self.generate_key(bot, chat_id, user_id, state_data_key)
if not data:
await self.redis.delete(key)
return
json_data = bot.session.json_dumps(data)
await self.redis.set(key, json_data, ex=self.data_ttl) # type: ignore[arg-type]

async def get_data(self, bot: Bot, chat_id: int, user_id: int) -> Dict[str, Any]:
key = self.generate_key(bot, chat_id, user_id, STATE_DATA_KEY)
async def get_data(
self,
bot: Bot,
chat_id: int,
user_id: int,
state_data_key: str = STATE_DATA_KEY,
) -> Dict[str, Any]:
key = self.generate_key(bot, chat_id, user_id, state_data_key)
value = await self.redis.get(key)
if value is None:
return {}
Expand Down

0 comments on commit fff33e4

Please sign in to comment.