[MailSystem] Config -> Cache
SharkyTheKing opened this issue · 1 comments
One of the main issues I knew was going to happen with this cog is a possible memory leak and the large number of config calls that are made.
I've made some attempts to lessen the config calls for the channel checks, though everything else is still calling to config on each command basis.
Need to figure out the best flow for a cache that holds what guild has a user blocked and the global blocks (for guilds and users), along with the other simple config calls.
This CAN'T break the flow of channel checkups, per the following two functions:
def check_tied_for_user(self, user_id: int):
"""
Checks tied channel to user
"""
user_channel = {v: k for k, v in self.cache.items()}
try:
user_channel[user_id]
return user_channel[user_id]
except KeyError:
return False
return False
def check_tied_for_channel(self, channel_id: int):
"""
Checks tied user to channel
"""
channel_user = self.cache
try:
channel_user[channel_id]
return channel_user[channel_id]
except KeyError:
return False
return False
Uses a cache based upon cog loading:
async def _update_cache(self):
"""
Update cache with config information
"""
await self.bot.wait_until_red_ready()
for guild in self.bot.guilds:
category = await self.config.guild(guild).category()
if category:
chan = await self.bot.fetch_channel(category)
for channel in chan.channels:
user = await self.config.channel(channel).user()
if user:
self.cache.update({channel.id: user})
So, either we add onto this cache system and make slight adjustments or we hook into another self function for the other options. No clue, something to think on how to incorporate.
A few ideas I thought of while in the car, possibly having different cache sets for certain things.
For example, guild activation isn't really something that should be called from cache a lot, as it's almost always a single call and then continued from cache for channel -> user input.
Channel cache should be renamed to self.channel_user_cache
or something similar to distinguish it from the full cache with has everything else stored.