mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
docs: change wordings on caching and desired props page (#4010)
* docs: change wordings on caching and desired props page * change value to property
This commit is contained in:
+51
-12
@@ -35,28 +35,28 @@ const getProxyCacheBot = (bot: Bot) =>
|
||||
// Define what properties of individual cache you wish to cache. Caches no props by default. Or you can use the `undesiredProps` prop to reverse the behavior of `desiredProps`.
|
||||
desiredProps: {
|
||||
// Example props that are cached in channels and other cache. Accepts an array of props of the cache. All props are optional.
|
||||
guilds: ['channels', 'icon', 'id', 'name', 'roles'],
|
||||
users: ['avatar', 'id', 'username'],
|
||||
guild: ['channels', 'icon', 'id', 'name', 'roles'],
|
||||
user: ['avatar', 'id', 'username'],
|
||||
},
|
||||
// Define what to cache in memory. All props are optional except `default`. By default, all props inside `cacheInMemory` are set to `true`.
|
||||
cacheInMemory: {
|
||||
// Whether or not to cache guilds.
|
||||
guilds: true,
|
||||
channels: true,
|
||||
guild: true,
|
||||
channel: true,
|
||||
// Default value for the properties that are not provided inside `cacheInMemory`.
|
||||
default: false,
|
||||
},
|
||||
// Define what to cache outside memory. All props are optional except `default`. By default, all props inside `cacheOutsideMemory` are set to `false`.
|
||||
cacheOutsideMemory: {
|
||||
// Whether or not to cache channels.
|
||||
channels: false,
|
||||
roles: false,
|
||||
channel: false,
|
||||
role: false,
|
||||
// Default value for the properties that are not provided inside `cacheOutsideMemory`.
|
||||
default: true,
|
||||
},
|
||||
// Function to get an item from outside cache. `getItem`, `setItem`, `removeItem` must be provided if you cache outside memory, can be omitted if you don't store outside memory.
|
||||
setItem: (table, item) => {
|
||||
if (table === 'channels') {
|
||||
if (table === 'channel') {
|
||||
// Custom code to store data into your cache outside memory, say redis or a database or whichever you use.
|
||||
}
|
||||
},
|
||||
@@ -90,6 +90,18 @@ await bot.cache.guilds.get(guildId);
|
||||
|
||||
Each cache will be in their own property under `bot.cache` and each of them have the following methods: `delete`, `get`, `set`, usage of these should be self explanatory from intellisense. If you cache in memory and need access to the collection directly, you can use `bot.cache.guilds.memory`, this will return a collection.
|
||||
|
||||
### Types Support
|
||||
|
||||
The types of cached objects change based on the provided `desiredProperties` and `undesiredProperties`, so only the stored properties will appear in your intelliSense, making the package easier to use.
|
||||
|
||||
These types are also exposed under `bot.cache.$inferredTypes`, and if you wish, you can export them as with a custom name for ease of use, like:
|
||||
|
||||
```ts
|
||||
export type CachedGuild = typeof bot.cache.$inferredTypes.guild;
|
||||
```
|
||||
|
||||
Now you can import `CachedGuild` in your code and use it.
|
||||
|
||||
### Important Points To Note
|
||||
|
||||
- Make sure to include the correct `bot.transformers.desiredProperties` somewhere in your code, this must include at least **all** the properties from `bot.cache.options.desiredProps` for it to cache all those properties you want to cache.
|
||||
@@ -113,7 +125,9 @@ shouldCache: {
|
||||
|
||||
#### `options.bulk`:
|
||||
|
||||
Lets you define how to deal with bulk removal of data. Useful to provide when you use cache outside memory. For example, if you store channels individually and separately from a guild, say in a database, when a guild is deleted, all of those channels will be deleted individually in individual queries, which is not ideal, so you can use `options.bulk.removeGuild` to delete the guild and all the channels related to that guild as one query or so, whichever gives better performance.
|
||||
This option allows you to specify how to handle the removal of objects that may trigger bulk modifications or deletions of associated entities.
|
||||
|
||||
For example, if you store guild channels individually in a database separate from the guild itself, deleting a guild could result in each channel being deleted one by one through individual queries. This method can be inefficient, especially as the number of channels increases. To improve permformance, you can use `options.bulk.removeGuild` to remove the guild and all associated channels in a single query.
|
||||
|
||||
This provides the following props: (should be self explanatory with intellisense)
|
||||
|
||||
@@ -121,13 +135,38 @@ This provides the following props: (should be self explanatory with intellisense
|
||||
- `options.bulk.removeRole`
|
||||
- `options.bulk.replaceInternalBulkRemover` - To set props under this prop to tell the cache proxy whether or not to run internal bulk removers.
|
||||
|
||||
#### `options.maxCacheInactiveTime`:
|
||||
#### `options.sweeper`:
|
||||
|
||||
Lets you provide the amount of inactive time (in milliseconds) for a cached object after which it should be removed from cache. Useful if for example you want to cache only active guilds.
|
||||
This option allows you to specify options for sweeper. This works for in-memory cache only. For outside memory cache, you should implement your own sweeper.
|
||||
|
||||
#### `options.cacheSweepInterval`:
|
||||
This provides the following props:
|
||||
|
||||
Lets you define the interval (in milliseconds) in which the cache sweeper should check for inactive objects based on maxCacheInactiveTime to clear them.
|
||||
- `options.sweeper.interval`
|
||||
- `options.sweeper.filter`
|
||||
|
||||
##### `options.sweeper.interval`:
|
||||
|
||||
The interval (in milliseconds) at which the cache sweeper should run the provided filter functions.
|
||||
|
||||
##### `options.sweeper.filter`:
|
||||
|
||||
This option allows you to provide filter functions to decide which object to remove from cache and which to keep. Defaults to removing nothing from the cache, so you should provide your own filters if you enable cache sweeper.
|
||||
|
||||
Note: You can use the `lastInteractedTime` property in the object to implement an NRU (Not Recently Used) cache if you'd like. For example, if you'd like to only remove the members that aren't accessed in the last 15 minutes and isn't the bot member, you can do:
|
||||
|
||||
```js
|
||||
sweeper: {
|
||||
// Run the sweeper every 5 minutes
|
||||
interval: 300000,
|
||||
filter: {
|
||||
member: (member) => {
|
||||
// Remove member from cache if it hasn't been accessed in the last 15 minutes and if the member isn't bot member
|
||||
if (Date.now() - member.lastInteractedTime > 900000 && member.id !== bot.id) return true;
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Manual Caching
|
||||
|
||||
|
||||
Reference in New Issue
Block a user