You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/website/docs/guide/11-caching.mdx
+42-11Lines changed: 42 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,8 @@ description: A guide on how to implement caching in your bot using CommandKit.
6
6
# Caching
7
7
8
8
:::warning
9
-
This feature is currently available in development version of CommandKit only.
9
+
This feature is currently available in development version of CommandKit only. Since it is an unstable feature, it may change in the future.
10
+
You need to prefix the function with `unstable_` to use this feature until it is stable.
10
11
:::
11
12
12
13
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.
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
+
By default, the cached data will be stored forever until `revalidate()` or `expire()` 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
88
89
```js
89
90
constfetchData=cache(
@@ -100,25 +101,55 @@ const fetchData = cache(
100
101
);
101
102
```
102
103
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
+
You may want to specify the cache parameters when using `"use cache"` directive. When using this approach, you can use `cacheTag()` to tag the cache with custom parameters.
`cacheTag()` will only tag the function when it first runs. Subsequent calls to the function will not tag the cache again.
126
+
If not tagged manually, commandkit assigns random tag name with 15 minutes TTL.
127
+
128
+
`cacheTag()` does not work with the `cache` function. It must be used with the `"use cache"` directive only.
129
+
:::
130
+
131
+
> You can alternatively use `cacheLife()` to set the TTL of the cache. Example: `cacheLife(10_000)` would set the TTL to 10 seconds.
132
+
133
+
## Invalidating/Revalidating the cache
134
+
135
+
Revalidating the cache is the process of updating the cached data with fresh data from the original source on demand. You can use the `unstable_revalidate()` function to revalidate the cache. CommandKit will not immediately revalidate the cache, but it will do so the next time the cached data is requested. Because of this, we can also term it as "lazy revalidation".
Expiring the cache is the process of removing the cached data or resetting the TTL of the cache. Use the `unstable_expire()` function to expire the cache.
0 commit comments