optimisation: Populate data caches on startup.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from collections import defaultdict
|
||||
|
||||
import settings
|
||||
from settings import GuildSettings
|
||||
from wards import guild_admin
|
||||
@@ -37,6 +39,34 @@ class untracked_channels(settings.ChannelList, settings.ListData, settings.Setti
|
||||
else:
|
||||
return "Study time will now be counted in all channels."
|
||||
|
||||
@classmethod
|
||||
async def launch_task(cls, client):
|
||||
"""
|
||||
Launch initialisation step for the `untracked_channels` setting.
|
||||
|
||||
Pre-fill cache for the guilds with currently active voice channels.
|
||||
"""
|
||||
active_guildids = [
|
||||
guild.id
|
||||
for guild in client.guilds
|
||||
if any(channel.members for channel in guild.voice_channels)
|
||||
]
|
||||
if active_guildids:
|
||||
rows = cls._table_interface.select_where(
|
||||
guildid=active_guildids
|
||||
)
|
||||
cache = defaultdict(list)
|
||||
for row in rows:
|
||||
cache[row['guildid']].append(row['channelid'])
|
||||
cls._cache.update(cache)
|
||||
client.log(
|
||||
"Cached {} untracked channels for {} active guilds.".format(
|
||||
len(rows),
|
||||
len(cache)
|
||||
),
|
||||
context="UNTRACKED_CHANNELS"
|
||||
)
|
||||
|
||||
|
||||
@GuildSettings.attach_setting
|
||||
class hourly_reward(settings.Integer, settings.GuildSetting):
|
||||
|
||||
@@ -101,6 +101,8 @@ async def _study_tracker():
|
||||
|
||||
@module.launch_task
|
||||
async def launch_study_tracker(client):
|
||||
# First pre-load the untracked channels
|
||||
await admin.untracked_channels.launch_task(client)
|
||||
asyncio.create_task(_study_tracker())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user