Skip to content

Commit 6bf4288

Browse files
committed
feat: unflag cache
1 parent 8549b45 commit 6bf4288

File tree

10 files changed

+24
-61
lines changed

10 files changed

+24
-61
lines changed

apps/test-bot/src/commands/misc/invalidate-random.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
SlashCommandProps,
3-
CommandData,
4-
unstable_invalidate as invalidate,
5-
} from 'commandkit';
1+
import { SlashCommandProps, CommandData, invalidate } from 'commandkit';
62

73
export const data: CommandData = {
84
name: 'invalidate-random',

apps/test-bot/src/commands/misc/random.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
SlashCommandProps,
3-
CommandData,
4-
unstable_cache as cache,
5-
} from 'commandkit';
1+
import { SlashCommandProps, CommandData, cache } from 'commandkit';
62

73
export const data: CommandData = {
84
name: 'random',

apps/test-bot/src/commands/misc/revalidate-random.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
SlashCommandProps,
3-
CommandData,
4-
unstable_revalidate as revalidate,
5-
} from 'commandkit';
1+
import { SlashCommandProps, CommandData, revalidate } from 'commandkit';
62

73
export const data: CommandData = {
84
name: 'revalidate-random',

apps/test-bot/src/commands/misc/xp.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
SlashCommandProps,
3-
CommandData,
4-
unstable_cacheTag as cacheTag,
5-
} from 'commandkit';
1+
import { SlashCommandProps, CommandData, cacheTag } from 'commandkit';
62
import { database } from '../../database/store';
73

84
export const data: CommandData = {

apps/test-bot/src/events/messageCreate/give-xp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Message } from 'discord.js';
2-
import { unstable_invalidate as invalidate } from 'commandkit';
2+
import { invalidate } from 'commandkit';
33
import { database } from '../../database/store';
44

55
export default async function (message: Message) {

apps/website/docs/guide/11-caching.mdx

+9-18
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ description: A guide on how to implement caching in your bot using CommandKit.
55

66
# Caching
77

8-
:::warning
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.
11-
:::
12-
138
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.
149

1510
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.
@@ -58,11 +53,7 @@ Let's build a simple XP system using CommandKit's caching feature. We'll create:
5853
### XP Command
5954

6055
```js
61-
import {
62-
SlashCommandProps,
63-
CommandData,
64-
unstable_cacheTag as cacheTag,
65-
} from 'commandkit';
56+
import { SlashCommandProps, CommandData, cacheTag } from 'commandkit';
6657
import { database } from '../database';
6758

6859
export const data = {
@@ -93,7 +84,7 @@ export async function run({ interaction }) {
9384
### XP Event Handler
9485
9586
```js
96-
import { unstable_invalidate as invalidate } from 'commandkit';
87+
import { invalidate } from 'commandkit';
9788
import { database } from '../database';
9889

9990
// Give XP when user sends a message
@@ -120,7 +111,7 @@ In this example:
120111
### Pattern 1: Cache with Auto-Expiry
121112
122113
```js
123-
import { unstable_cache as cache } from 'commandkit';
114+
import { cache } from 'commandkit';
124115

125116
// Cache API results for 5 minutes
126117
const fetchPokemon = cache(
@@ -160,7 +151,7 @@ async function updateUserProfile(userId, data) {
160151
### Pattern 3: Forced Refresh with Revalidation
161152
162153
```js
163-
import { unstable_revalidate as revalidate } from 'commandkit';
154+
import { revalidate as revalidate } from 'commandkit';
164155

165156
// Command to force refresh server stats
166157
export async function run({ interaction }) {
@@ -230,7 +221,7 @@ The `'use cache'` directive will only work if you are using the commandkit cli t
230221
### Using the cache manually
231222
232223
```js
233-
import { unstable_cache as cache } from 'commandkit';
224+
import { cache } from 'commandkit';
234225

235226
const fetchData = cache(
236227
async () => {
@@ -252,7 +243,7 @@ By default, the cached data will be stored for 15 minutes unless `revalidate()`
252243
When using the `"use cache"` directive, you can use `cacheTag()` to set cache parameters:
253244
254245
```js
255-
import { unstable_cacheTag as cacheTag } from 'commandkit';
246+
import { cacheTag } from 'commandkit';
256247

257248
async function fetchData() {
258249
'use cache';
@@ -272,7 +263,7 @@ async function fetchData() {
272263
You can also set just the TTL using `cacheLife`:
273264
274265
```js
275-
import { unstable_cacheLife as cacheLife } from 'commandkit';
266+
import { cacheLife } from 'commandkit';
276267

277268
async function fetchData() {
278269
'use cache';
@@ -298,7 +289,7 @@ async function fetchData() {
298289
To immediately remove cached data:
299290
300291
```js
301-
import { unstable_invalidate as invalidate } from 'commandkit';
292+
import { invalidate } from 'commandkit';
302293

303294
// Remove the cache entry immediately
304295
await invalidate('user-data');
@@ -309,7 +300,7 @@ await invalidate('user-data');
309300
To force refresh the cached data:
310301
311302
```js
312-
import { unstable_revalidate as revalidate } from 'commandkit';
303+
import { revalidate } from 'commandkit';
313304

314305
// Revalidate and get fresh data
315306
const freshData = await revalidate('user-data');

packages/commandkit/bin/esbuild-plugins/use-cache.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const generate = _generate.default || _generate;
99
const IMPORT_PATH = 'commandkit';
1010
const DIRECTIVE = 'use cache';
1111
const CACHE_IDENTIFIER =
12-
'unstable_super_duper_secret_internal_for_use_cache_directive_of_commandkit_cli_do_not_use_it_directly_or_you_will_be_fired_from_your_job_kthxbai';
12+
'super_duper_secret_internal_for_use_cache_directive_of_commandkit_cli_do_not_use_it_directly_or_you_will_be_fired_from_your_job_kthxbai';
1313

1414
const generateRandomString = (length = 6) => {
1515
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

packages/commandkit/src/cache/cache.ts

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AsyncLocalStorage } from 'node:async_hooks';
22
import ms from 'ms';
33
import { GenericFunction, getCommandKit } from '../context/async-context';
4-
import { warnUnstable } from '../utils/warn-unstable';
54
import { randomUUID } from 'node:crypto';
65

76
export type AsyncFunction<R extends any[] = any[], T = any> = (
@@ -49,7 +48,6 @@ function cache<R extends any[], F extends AsyncFunction<R>>(
4948
fn: F,
5049
params?: Partial<CacheTag>,
5150
): F {
52-
warnUnstable('cache');
5351
params = Object.assign({}, getDefaultCacheTag(), params);
5452

5553
return useCache(fn, __identificationKey, params as CacheTag);
@@ -71,10 +69,6 @@ function useCache<R extends any[], F extends AsyncFunction<R>>(
7169

7270
const isLocal = id === __identificationKey;
7371

74-
if (!isLocal) {
75-
warnUnstable('"use cache"');
76-
}
77-
7872
const memoized = (async (...args) => {
7973
const commandkit = getCommandKit(true);
8074
const cache = commandkit.getCacheProvider();
@@ -173,8 +167,6 @@ function cacheTag(tag: CacheTag | string): void {
173167
throw new TypeError('cacheTag must be called with a tag.');
174168
}
175169

176-
warnUnstable('cacheTag');
177-
178170
const context = cacheContext.getStore();
179171

180172
if (context === undefined) {
@@ -208,8 +200,6 @@ function cacheLife(life: string | number) {
208200
throw new TypeError('cacheLife must be called with a time-to-live value.');
209201
}
210202

211-
warnUnstable('cacheLife');
212-
213203
const context = cacheContext.getStore();
214204

215205
if (context === undefined) {
@@ -231,7 +221,6 @@ function cacheLife(life: string | number) {
231221
* @param tag The cache tag to invalidate.
232222
*/
233223
async function invalidate(tag: string) {
234-
warnUnstable('invalidate');
235224
const commandkit = getCommandKit(true);
236225
const cache = commandkit.getCacheProvider();
237226

@@ -255,7 +244,6 @@ async function revalidate<R = any>(
255244
tag: string,
256245
...args: any[]
257246
): Promise<R | undefined> {
258-
warnUnstable('revalidate');
259247
const commandkit = getCommandKit(true);
260248
const cache = commandkit.getCacheProvider();
261249

@@ -277,10 +265,10 @@ async function revalidate<R = any>(
277265
}
278266

279267
export {
280-
cache as unstable_cache,
281-
useCache as unstable_super_duper_secret_internal_for_use_cache_directive_of_commandkit_cli_do_not_use_it_directly_or_you_will_be_fired_from_your_job_kthxbai,
282-
cacheTag as unstable_cacheTag,
283-
cacheLife as unstable_cacheLife,
284-
invalidate as unstable_invalidate,
285-
revalidate as unstable_revalidate,
268+
cache,
269+
useCache as super_duper_secret_internal_for_use_cache_directive_of_commandkit_cli_do_not_use_it_directly_or_you_will_be_fired_from_your_job_kthxbai,
270+
cacheTag,
271+
cacheLife,
272+
invalidate,
273+
revalidate,
286274
};

packages/redis/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
"tsconfig": "workspace:*",
3434
"typescript": "^5.7.3"
3535
}
36-
}
36+
}

packages/tsconfig/base.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"moduleResolution": "Node",
1010
"module": "CommonJS",
1111
"declaration": true,
12-
"isolatedModules": true,
12+
"isolatedModules": true
1313
}
14-
}
14+
}

0 commit comments

Comments
 (0)