Skip to content

Question: What is the cache? #7661

Closed Answered by almostSouji
JakeElder asked this question in Q&A
Discussion options

You must be logged in to vote

Discord.js uses Collections, a homebrew extension of the regular JavaScript Map data structure to cache data it receives via both websocket and REST API response payloads.

To only access the cache you can access the .cache property of the respective manager instance or use the .resolve() method on the manager itself.

const m1 = guild.members.cache.get("someid");
const m2 = guild.members.resolve("someid");

To request data from the API, in case it is not yet available in the cache, you use the .fetch() method. It will by default check the cache and hit the API if nothing is found. As such it returns a Promise you need to handle accordingly.

const m1 = await guild.members.fetch("someid");

If…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@almostSouji
Comment options

Answer selected by kyranet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #7652 on March 14, 2022 07:44.