def is_global_banned(self, user: discord.User) -> bool:
key = f'cache:globalbans:{user.id}'
if await self.redis.exists(key):
# use the cached value instead
return await self.redis.get(key, encoding='utf-8') == 'banned'
async with self.pgpool.acquire() as conn:
# grab the record from postgres, if any
banned = (await conn.fetchrow('SELECT * FROM globalbans WHERE user_id = $1', user.id)) is not None
# cache the banned value for 2 hours
await self.redis.set(key, 'banned' if banned else 'not banned', expire=7200)
# return whether banned or not
return banned
评论列表
文章目录