|
| 1 | +--- |
| 2 | +title: Caching |
| 3 | +description: A guide on how to implement caching in your bot using CommandKit. |
| 4 | +--- |
| 5 | + |
| 6 | +# Caching |
| 7 | + |
| 8 | +:::warning |
| 9 | +This feature is currently available in development version of CommandKit only. |
| 10 | +::: |
| 11 | + |
| 12 | +Caching is a technique used to store data in a temporary storage to reduce the time it takes to fetch the data from the original source. This can be useful in Discord bots to reduce the number of database queries or external API calls. |
| 13 | + |
| 14 | +CommandKit provides an easy way to implement caching in your bot without having to worry about the underlying implementation. This guide will show you how to use the caching feature in CommandKit. |
| 15 | + |
| 16 | +## Setting up the cache |
| 17 | + |
| 18 | +By default, commandkit enables in-memory caching. This means that the cache will be stored in the bot's memory and will be lost when the bot restarts. |
| 19 | +You can provide a custom cache store by specifying the `cacheProvider` option when instantiating CommandKit. |
| 20 | + |
| 21 | +```js |
| 22 | +const { CommandKit } = require('commandkit'); |
| 23 | + |
| 24 | +new CommandKit({ |
| 25 | + client, |
| 26 | + commandsPath, |
| 27 | + eventsPath, |
| 28 | + cacheProvider: new MyCustomCacheProvider(), |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +The `MyCustomCacheProvider` class should extend `CacheProvider` from CommandKit and implement the required methods. You may use this to store the cache in redis, a database or a file system. |
| 33 | + |
| 34 | +## Using the cache |
| 35 | + |
| 36 | +### Using commandkit CLI |
| 37 | + |
| 38 | +If you are using the commandkit cli to run your bot, you can simply add `"use cache"` directive on a function that you want to cache the result of. |
| 39 | + |
| 40 | +```js |
| 41 | +async function fetchData() { |
| 42 | + 'use cache'; |
| 43 | + |
| 44 | + // Fetch data from an external source |
| 45 | + const data = await fetch('https://my-example-api.com/data'); |
| 46 | + |
| 47 | + return data.json(); |
| 48 | +} |
| 49 | + |
| 50 | +export async function run({ interaction }) { |
| 51 | + await interaction.deferReply(); |
| 52 | + |
| 53 | + // Fetch data |
| 54 | + const data = await fetchData(); |
| 55 | + |
| 56 | + // Send the data to the user |
| 57 | + await interaction.editReply(data); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Using the cache manually |
| 62 | + |
| 63 | +To use the cache manually, you can import the `unstable_cache()` function from CommandKit and use it to cache the result of a function. |
| 64 | + |
| 65 | +```js |
| 66 | +import { unstable_cache as cache } from 'commandkit'; |
| 67 | + |
| 68 | +const fetchData = cache(async () => { |
| 69 | + // Fetch data from an external source |
| 70 | + const data = await fetch('https://my-example-api.com/data'); |
| 71 | + |
| 72 | + return data.json(); |
| 73 | +}); |
| 74 | + |
| 75 | +export async function run({ interaction }) { |
| 76 | + await interaction.deferReply(); |
| 77 | + |
| 78 | + // Fetch data |
| 79 | + const data = await fetchData(); |
| 80 | + |
| 81 | + // Send the data to the user |
| 82 | + await interaction.editReply(data); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +By default, the cached data will be stored forever until `unstable_revalidate()` or `unstable_invalidate()` is called on the cache object. You can also specify a custom TTL (time to live) for the cache by passing a second argument to the `cache` function. |
| 87 | + |
| 88 | +```js |
| 89 | +const fetchData = cache( |
| 90 | + async () => { |
| 91 | + // Fetch data from an external source |
| 92 | + const data = await fetch('https://my-example-api.com/data'); |
| 93 | + |
| 94 | + return data.json(); |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'fetchData', // name of the cache |
| 98 | + ttl: 60_000, // cache for 1 minute |
| 99 | + }, |
| 100 | +); |
| 101 | +``` |
| 102 | + |
| 103 | +You may want to specify the cache parameters when using `"use cache"` directive. When using this approach, you can use `unstable_cacheTag()` to tag the cache with custom parameters. |
| 104 | + |
| 105 | +```js |
| 106 | +import { unstable_cacheTag as cacheTag } from 'commandkit'; |
| 107 | + |
| 108 | +async function fetchData() { |
| 109 | + 'use cache'; |
| 110 | + |
| 111 | + // Fetch data from an external source |
| 112 | + const data = await fetch('https://my-example-api.com/data'); |
| 113 | + |
| 114 | + return data.json(); |
| 115 | +} |
| 116 | + |
| 117 | +cacheTag( |
| 118 | + { |
| 119 | + name: 'fetchData', // name of the cache |
| 120 | + ttl: 60_000, // cache for 1 minute |
| 121 | + }, |
| 122 | + fetchData, |
| 123 | +); |
| 124 | +``` |
0 commit comments