Skip to content

Commit ee50393

Browse files
committed
refactor: rewrite cache utilities
1 parent 1ac231d commit ee50393

File tree

9 files changed

+284
-201
lines changed

9 files changed

+284
-201
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
SlashCommandProps,
3+
CommandData,
4+
unstable_invalidate as invalidate,
5+
} from 'commandkit';
6+
7+
export const data: CommandData = {
8+
name: 'invalidate-random',
9+
description: 'This is a random command invalidation.',
10+
};
11+
12+
export async function run({ interaction }: SlashCommandProps) {
13+
await interaction.deferReply();
14+
15+
await invalidate('random');
16+
17+
return interaction.editReply({
18+
content: `Random value has been invalidated.`,
19+
});
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
SlashCommandProps,
3+
CommandData,
4+
unstable_cache as cache,
5+
} from 'commandkit';
6+
7+
export const data: CommandData = {
8+
name: 'random',
9+
description: 'This is a random command.',
10+
};
11+
12+
const random = cache(
13+
async () => {
14+
return Math.random();
15+
},
16+
{ tag: 'random', ttl: 60_000 },
17+
);
18+
19+
export async function run({ interaction }: SlashCommandProps) {
20+
await interaction.deferReply();
21+
22+
const xp = await random();
23+
24+
return interaction.editReply({
25+
content: `Random value is: ${xp}`,
26+
});
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
SlashCommandProps,
3+
CommandData,
4+
unstable_revalidate as revalidate,
5+
} from 'commandkit';
6+
7+
export const data: CommandData = {
8+
name: 'revalidate-random',
9+
description: 'This is a random command invalidation.',
10+
};
11+
12+
export async function run({ interaction }: SlashCommandProps) {
13+
await interaction.deferReply();
14+
15+
const revalidated = await revalidate<number>('random');
16+
17+
return interaction.editReply({
18+
content: `Random value has been revalidated. The new value will be ${revalidated}`,
19+
});
20+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ export const data: CommandData = {
1313
async function getUserXP(guildId: string, userId: string) {
1414
'use cache';
1515

16-
cacheTag(`xp:${guildId}:${userId}`);
16+
const key = `xp:${guildId}:${userId}`;
1717

18-
const xp: number = (await database.get(`${guildId}:${userId}`)) ?? 0;
18+
cacheTag(key);
19+
20+
const xp: number = (await database.get(key)) ?? 0;
21+
22+
console.log(`Cached XP: ${xp} for ${key}`);
1923

2024
return xp;
2125
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Message } from 'discord.js';
2-
import { unstable_revalidate as revalidate } from 'commandkit';
2+
import { unstable_invalidate as invalidate } from 'commandkit';
33
import { database } from '../../database/store';
44

55
export default async function (message: Message) {
@@ -9,8 +9,9 @@ export default async function (message: Message) {
99
(await database.get(`${message.guildId}:${message.author.id}`)) ?? 0;
1010
const xp = Math.floor(Math.random() * 10) + 1;
1111

12-
await database.set(`${message.guildId}:${message.author.id}`, oldXp + xp);
12+
const key = `xp:${message.guildId}:${message.author.id}`;
13+
await database.set(key, oldXp + xp);
1314

14-
// revalidate the cache
15-
await revalidate(`xp:${message.guildId}:${message.author.id}`);
15+
// invalidate the cache
16+
await invalidate(key);
1617
}

packages/commandkit/src/cache/CacheProvider.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
export interface CacheEntry {
2-
key: string;
3-
value: any;
1+
export interface CacheEntry<T = unknown> {
2+
value: T;
43
ttl?: number;
54
}
65

76
export abstract class CacheProvider {
8-
abstract get(key: string): Promise<CacheEntry | undefined>;
9-
abstract set(key: string, value: any, ttl?: number): Promise<void>;
7+
abstract get<T>(key: string): Promise<CacheEntry<T> | undefined>;
8+
abstract set<T>(key: string, value: T, ttl?: number): Promise<void>;
109
abstract exists(key: string): Promise<boolean>;
1110
abstract delete(key: string): Promise<void>;
1211
abstract clear(): Promise<void>;

packages/commandkit/src/cache/MemoryCache.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CacheEntry, CacheProvider } from './CacheProvider';
33
export class MemoryCache extends CacheProvider {
44
#cache = new Map<string, CacheEntry>();
55

6-
public async get<T>(key: string): Promise<T | undefined> {
6+
public async get<T>(key: string): Promise<CacheEntry<T> | undefined> {
77
const entry = this.#cache.get(key);
88

99
if (!entry) {
@@ -15,15 +15,16 @@ export class MemoryCache extends CacheProvider {
1515
return undefined;
1616
}
1717

18-
return entry.value;
18+
return entry as CacheEntry<T>;
1919
}
2020

2121
public async set<T>(key: string, value: T, ttl?: number): Promise<void> {
22-
this.#cache.set(key, {
23-
key,
22+
const entry: CacheEntry<T> = {
2423
value,
2524
ttl: ttl != null ? Date.now() + ttl : undefined,
26-
});
25+
};
26+
27+
this.#cache.set(key, entry);
2728
}
2829

2930
public async exists(key: string): Promise<boolean> {

0 commit comments

Comments
 (0)