Skip to content

Commit

Permalink
Add quick_populate_cache method to ThreadManager
Browse files Browse the repository at this point in the history
quick_populate_cache uses the database as the source of truth, and pulls all logs matching a channel ID in the server.
  • Loading branch information
khakers committed Aug 26, 2023
1 parent cd66a7f commit 66b73bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ async def on_ready(self):
)
logger.line()

await self.threads.populate_cache()
await self.threads.quick_populate_cache()

# closures
closures = self.config["closures"]
Expand Down
3 changes: 3 additions & 0 deletions core/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ async def get_log(self, channel_id: Union[str, int]) -> dict:
logger.debug("Retrieving channel %s logs.", channel_id)
return await self.logs.find_one({"channel_id": str(channel_id)})

async def get_logs(self, channel_id: List[Union[str, int]]) -> dict:
return await self.logs.find({"channel_id": {"$in": [str(i) for i in channel_id]}}).to_list(None)

async def get_log_link(self, channel_id: Union[str, int]) -> str:
doc = await self.get_log(channel_id)
logger.debug("Retrieving log link for channel %s.", channel_id)
Expand Down
26 changes: 25 additions & 1 deletion core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import functools
import io
import re
from time import perf_counter

import time
import traceback
import typing
Expand Down Expand Up @@ -1244,6 +1246,7 @@ async def add_users(self, users: typing.List[typing.Union[discord.Member, discor

topic += f"\nOther Recipients: {ids}"

# Add recipients to database
await self.bot.api.add_recipients(self._channel.id, users)

await self.channel.edit(topic=topic)
Expand Down Expand Up @@ -1273,11 +1276,14 @@ class ThreadManager:

def __init__(self, bot):
self.bot = bot
self.cache = {}
self.cache: typing.Dict[int, Thread] = {}

async def populate_cache(self) -> None:
# time method runtime
start = perf_counter()
for channel in self.bot.modmail_guild.text_channels:
await self.find(channel=channel)
logger.info("Cache populated in %fs.", time.perf_counter() - start)

def __len__(self):
return len(self.cache)
Expand All @@ -1288,6 +1294,24 @@ def __iter__(self):
def __getitem__(self, item: str) -> Thread:
return self.cache[item]

async def quick_populate_cache(self) -> None:
start = perf_counter()

# create a list containing the id of every text channel in the modmail guild
channel_ids = [channel.id for channel in self.bot.modmail_guild.text_channels]
logs = await self.bot.api.get_logs(channel_ids)

for log in logs:
recipients = log["other_recipients"]

tasks = [self.bot.get_or_fetch_user(user_data["id"]) for user_data in recipients]
recipient_users: list[discord.Member] = await asyncio.gather(*tasks)

self.cache[log["recipient"]["id"]] = Thread(
self, recipient=log["creator"]["id"], channel=log["channel_id"], other_recipients=recipient_users
)
logger.debug("Cache populated in %fs.", perf_counter() - start)

async def find(
self,
*,
Expand Down

0 comments on commit 66b73bf

Please sign in to comment.