inline cache

This commit is contained in:
mihalin 2022-02-11 04:38:07 +03:00
parent 7c3069ccb8
commit 767cfe64ee

View File

@ -7,19 +7,18 @@ from olgram.models.models import Bot
import typing as ty import typing as ty
@cached(ttl=1) @cached(ttl=60)
async def get_phrases(bot: Bot) -> ty.List: async def get_phrases(bot: Bot) -> ty.List:
objects = await bot.answers objects = await bot.answers
return [obj.text for obj in objects] return [obj.text for obj in objects]
@cached(ttl=1)
async def check_chat_member(chat_id: int, user_id: int, bot: AioBot) -> bool: async def check_chat_member(chat_id: int, user_id: int, bot: AioBot) -> bool:
member = await bot.get_chat_member(chat_id, user_id) member = await bot.get_chat_member(chat_id, user_id)
return member.is_chat_member() return member.is_chat_member()
@cached(ttl=1) @cached(ttl=60)
async def check_permissions(inline_query: InlineQuery, bot: Bot): async def check_permissions(inline_query: InlineQuery, bot: Bot):
user_id = inline_query.from_user.id user_id = inline_query.from_user.id
super_chat_id = await bot.super_chat_id() super_chat_id = await bot.super_chat_id()
@ -38,7 +37,7 @@ async def inline_handler(inline_query: InlineQuery, bot: Bot):
# Check permissions at first # Check permissions at first
allow = await check_permissions(inline_query, bot) allow = await check_permissions(inline_query, bot)
if not allow: if not allow:
return await inline_query.answer([], cache_time=1) # forbidden return await inline_query.answer([]) # forbidden
all_phrases = await get_phrases(bot) all_phrases = await get_phrases(bot)
phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()] phrases = [phrase for phrase in all_phrases if inline_query.query.lower() in phrase.lower()]
@ -54,4 +53,4 @@ async def inline_handler(inline_query: InlineQuery, bot: Bot):
) )
items.append(item) items.append(item)
await inline_query.answer(results=items, cache_time=1) await inline_query.answer(results=items)