Cache/sweeper settings for memory constraints, mostly messages

We are in a very memory constrained environment. Our individual bot processes can't exceed 512 MB. Right now each process is managing 3 shards of our Discord connection. 1. The bot only listens for new messages and new threads to join. 2. It only determines if it needs to reply to a message, and we also log some info about the guild to an external service 3. We don't care about reactions, members, message edits, any other part of the message lifecycle, and we don't need to retain any messages longer than it takes to process them. I'm trying to configure the makeCache and sweepers settings to help optimize memory usage in this scenario. What I have is below, any other suggestions would be welcome
const BotName = new Client({
// Other config omitted
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
partials: [
Partials.Channel, // Required to get DM content for some reason
],
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
ReactionManager: 0,
GuildMemberManager: {
maxSize: 200, // Only store up to 200 members per guild at a time
keepOverLimit: (member) => member.id === member.client.user.id, // Keep ourselves
},
}),
sweepers: {
...Options.DefaultSweeperSettings,
messages: {
interval: 300, // Sweep cache every 5 minutes
lifetime: 300, // Remove messages older than 5 minutes.
},
users: {
interval: 300, // Sweep cache every 5 minutes
lifetime: 7200, // Remove users older than two hours
filter: (user) => user.id !== user.client.user.id, // Keep ourselves
},
},
});
const BotName = new Client({
// Other config omitted
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
],
partials: [
Partials.Channel, // Required to get DM content for some reason
],
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
ReactionManager: 0,
GuildMemberManager: {
maxSize: 200, // Only store up to 200 members per guild at a time
keepOverLimit: (member) => member.id === member.client.user.id, // Keep ourselves
},
}),
sweepers: {
...Options.DefaultSweeperSettings,
messages: {
interval: 300, // Sweep cache every 5 minutes
lifetime: 300, // Remove messages older than 5 minutes.
},
users: {
interval: 300, // Sweep cache every 5 minutes
lifetime: 7200, // Remove users older than two hours
filter: (user) => user.id !== user.client.user.id, // Keep ourselves
},
},
});
4 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs11mo ago
guide Miscellaneous: Cache customization - Limiting caches When creating a new Clientopen in new window, you can limit the size of caches, which are specific to certain managers, using the makeCache option. Generally speaking, almost all your customizations can be done via the helper functions from the Optionsopen in new window module. read moreinterface CacheFactory
Karew
KarewOP11mo ago
If I don't cache members at all, does that increase latency when I get a new message again from the same member right afterward? It's unclear to me what the caches are for exactly. My bot is very simple, it is just reading all messages to check for things to reply to, I don't need to retain the message or info about it beyond that, unless it makes the bot perform better somehow with some caching Would you set most caches to 0 in that case to disable them?
Want results from more Discord servers?
Add your server