2.1.0 • Published 20 days ago

dd-cache-proxy v2.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
20 days ago

Discordeno Cache Proxy (dd-cache-proxy)

A simple, easy-to-use, highly customizable cache proxy for discordeno which supports in-memory and outside memory caching with custom properties you wish to cache.

Initially forked from cache-proxy, but modified to be a proper, minimal, non-bloated cache proxy that aims to fit larger bots.

Used In:

Example Usage:

import { createProxyCache } from 'dd-cache-proxy';
import { createBot, Bot, Intents } from '@discordeno/bot';

// Create a function for easier use and cleaner code.
const getProxyCacheBot = (bot: Bot) =>
    createProxyCache(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'],
        },
        // 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,
            // 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,
            // 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') {
                // Custom code to store data into your cache outside memory, say redis or a database or whichever you use.
            }
        },
    });

// Pass the created bot object to `getProxyCacheBot` so it can add the cache proxy to it.
const bot = getProxyCacheBot(
    // Create the bot object.
    createBot({
        token,
        intents: Intents.Guilds,
    })
);

Get guild from cache:

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.

Important Things To Note:

  • Make sure to include the correct client.transformers.desiredProperties somewhere in your code, this must include at least all the properties from client.cache.options.desiredProps for it to cache all those properties you want to cache.
  • It's not recommended to dynamically change client.cache.options.cacheInMemory or client.cache.options.cacheOutsideMemory since it may not cache newly added cache if events for that isn't setup. If you need to do so, you need to manually rerun the setupDummyEvents function.
    • You should also avoid directly replacing client.events (like client.events = { ready: ReadyFunction }) since it'll override the dummy events setup by the cache proxy, which may make it unable to cache data. Instead, assign to individual event properties, like client.events.ready = ReadyFunction, client.events.messageCreate = MessageCreateFunction etc.

Useful Options To Note:

options.shouldCache:

This is a property with which you can conditionally cache only certain objects and leave out the others. For example, if you only want to cache guild channels, you can simply do:

shouldCache: {
    channel: async (channel) => {
        if (channel.guildId) return true;
        else return false;
    },
}

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 provides the following props: (should be self explanatory with intellisense)

  • options.bulk.removeGuild
  • 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:

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.

options.cacheSweepInterval:

Lets you define the interval (in milliseconds) in which the cache sweeper should check for inactive objects based on maxCacheInactiveTime to clear them.

Questions / Support:

If you have any questions or require support, feel free to contact me (@awesomestickz) in the discordeno server.

2.1.0

20 days ago

2.0.2

5 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.0.0

1 year ago