@wolfstar/plugin-cache 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +150 -0
- package/dist/esm/index.d.ts +469 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +1436 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import { APIAuditLogEntry, APIAutoModerationRule, APIChannel, APIEmoji, APIEntitlement, APIGuild, APIGuildMember, APIGuildScheduledEvent, APIRole, APISoundboardSound, APIStageInstance, APISticker, APISubscription, APIThreadChannel, APIThreadMember, APIUser, APIVoiceState, GatewayApplicationCommandPermissionsUpdateDispatchData, GatewayDispatchPayload, GatewayGuildBanModifyDispatchData, GatewayGuildCreateDispatchData, GatewayIntegrationCreateDispatchData, GatewayIntegrationUpdateDispatchData, GatewayInviteCreateDispatchData, GatewayMessageCreateDispatchData, GatewayMessageUpdateDispatchData, GatewayPresenceUpdateDispatchData, Snowflake } from "discord-api-types/v10";
|
|
2
|
+
//#region src/lib/keys.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a stable key for a guild-scoped entity.
|
|
5
|
+
*/
|
|
6
|
+
export declare function guildScopedKey(guildId: Snowflake, id: Snowflake): string;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a stable key for a channel message.
|
|
9
|
+
*/
|
|
10
|
+
export declare function messageKey(channelId: Snowflake, messageId: Snowflake): string;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a stable key for a guild member.
|
|
13
|
+
*/
|
|
14
|
+
export declare function memberKey(guildId: Snowflake, userId: Snowflake): string;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a stable key for a guild presence.
|
|
17
|
+
*/
|
|
18
|
+
export declare function presenceKey(guildId: Snowflake, userId: Snowflake): string;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a stable key for a guild voice state.
|
|
21
|
+
*/
|
|
22
|
+
export declare function voiceStateKey(guildId: Snowflake, userId: Snowflake): string;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a stable key for a guild role.
|
|
25
|
+
*/
|
|
26
|
+
export declare function roleKey(guildId: Snowflake, roleId: Snowflake): string;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a stable key for a guild emoji.
|
|
29
|
+
*/
|
|
30
|
+
export declare function emojiKey(guildId: Snowflake, emojiId: Snowflake): string;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a stable key for a guild sticker.
|
|
33
|
+
*/
|
|
34
|
+
export declare function stickerKey(guildId: Snowflake, stickerId: Snowflake): string;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a stable key for a guild scheduled event.
|
|
37
|
+
*/
|
|
38
|
+
export declare function scheduledEventKey(guildId: Snowflake, scheduledEventId: Snowflake): string;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a stable key for a stage instance.
|
|
41
|
+
*/
|
|
42
|
+
export declare function stageInstanceKey(guildId: Snowflake, channelId: Snowflake): string;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a stable key for a guild soundboard sound.
|
|
45
|
+
*/
|
|
46
|
+
export declare function soundboardSoundKey(guildId: Snowflake, soundId: Snowflake): string;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a stable key for a guild auto moderation rule.
|
|
49
|
+
*/
|
|
50
|
+
export declare function autoModerationRuleKey(guildId: Snowflake, ruleId: Snowflake): string;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a stable key for a guild ban.
|
|
53
|
+
*/
|
|
54
|
+
export declare function banKey(guildId: Snowflake, userId: Snowflake): string;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a stable key for a guild integration.
|
|
57
|
+
*/
|
|
58
|
+
export declare function integrationKey(guildId: Snowflake, integrationId: Snowflake): string;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a stable key for an invite, using `@global` for invites that do not belong to a guild.
|
|
61
|
+
*/
|
|
62
|
+
export declare function inviteKey(guildId: Snowflake | null | undefined, code: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Creates a stable key for a thread member.
|
|
65
|
+
*/
|
|
66
|
+
export declare function threadMemberKey(threadId: Snowflake, userId: Snowflake): string;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a stable key for application command permissions.
|
|
69
|
+
*/
|
|
70
|
+
export declare function applicationCommandPermissionsKey(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake): string;
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/lib/types.d.ts
|
|
73
|
+
/**
|
|
74
|
+
* A value that may or may not be wrapped in a promise.
|
|
75
|
+
*/
|
|
76
|
+
type Awaitable<T> = T | Promise<T>;
|
|
77
|
+
/**
|
|
78
|
+
* The raw, JSON-serializable shape stored for every entity kind.
|
|
79
|
+
*
|
|
80
|
+
* @remarks
|
|
81
|
+
* The cache never stores `Structure` instances, only the raw API payloads (merged in place for partial updates).
|
|
82
|
+
* Building structures on top of them is the job of the consumer, e.g. `@wolfstar/plugin-gateway`'s managers.
|
|
83
|
+
*/
|
|
84
|
+
interface CacheEntityTypes {
|
|
85
|
+
applicationCommandPermissions: GatewayApplicationCommandPermissionsUpdateDispatchData;
|
|
86
|
+
auditLogEntries: APIAuditLogEntry & {
|
|
87
|
+
guild_id: Snowflake;
|
|
88
|
+
};
|
|
89
|
+
autoModerationRules: APIAutoModerationRule;
|
|
90
|
+
bans: GatewayGuildBanModifyDispatchData;
|
|
91
|
+
channels: APIChannel & {
|
|
92
|
+
guild_id?: Snowflake;
|
|
93
|
+
};
|
|
94
|
+
emojis: APIEmoji & {
|
|
95
|
+
guild_id: Snowflake;
|
|
96
|
+
};
|
|
97
|
+
entitlements: APIEntitlement;
|
|
98
|
+
/**
|
|
99
|
+
* A guild without its collections (`roles`, `emojis`, `stickers`, `channels`, `members`, ...): they are stored in
|
|
100
|
+
* their own entity caches, which stay up to date as the guild changes.
|
|
101
|
+
*/
|
|
102
|
+
guilds: Omit<APIGuild, "emojis" | "roles" | "stickers"> & Partial<Omit<GatewayGuildCreateDispatchData, keyof APIGuild>>;
|
|
103
|
+
integrations: GatewayIntegrationCreateDispatchData | GatewayIntegrationUpdateDispatchData;
|
|
104
|
+
invites: GatewayInviteCreateDispatchData;
|
|
105
|
+
members: APIGuildMember & {
|
|
106
|
+
guild_id: Snowflake;
|
|
107
|
+
};
|
|
108
|
+
messages: GatewayMessageCreateDispatchData | GatewayMessageUpdateDispatchData;
|
|
109
|
+
presences: GatewayPresenceUpdateDispatchData;
|
|
110
|
+
roles: APIRole & {
|
|
111
|
+
guild_id: Snowflake;
|
|
112
|
+
};
|
|
113
|
+
scheduledEvents: APIGuildScheduledEvent;
|
|
114
|
+
soundboardSounds: APISoundboardSound;
|
|
115
|
+
stageInstances: APIStageInstance;
|
|
116
|
+
stickers: APISticker & {
|
|
117
|
+
guild_id: Snowflake;
|
|
118
|
+
};
|
|
119
|
+
subscriptions: APISubscription;
|
|
120
|
+
threadMembers: APIThreadMember & {
|
|
121
|
+
guild_id?: Snowflake;
|
|
122
|
+
};
|
|
123
|
+
threads: APIThreadChannel;
|
|
124
|
+
users: APIUser;
|
|
125
|
+
voiceStates: APIVoiceState;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The name of any of the entity caches held by a {@link Cache}.
|
|
129
|
+
*/
|
|
130
|
+
type CacheEntityName = keyof CacheEntityTypes;
|
|
131
|
+
/**
|
|
132
|
+
* A Map-like, per-entity key-value store.
|
|
133
|
+
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* Every method returns an {@link Awaitable}, so synchronous (in-memory) and asynchronous (Redis) stores implement the
|
|
136
|
+
* exact same contract. `keys`, `values`, and `entries` return snapshots rather than live iterators, which keeps the
|
|
137
|
+
* semantics identical across backends.
|
|
138
|
+
*/
|
|
139
|
+
interface EntityCache<Raw> {
|
|
140
|
+
get(key: string): Awaitable<Raw | undefined>;
|
|
141
|
+
set(key: string, value: Raw): Awaitable<void>;
|
|
142
|
+
has(key: string): Awaitable<boolean>;
|
|
143
|
+
delete(key: string): Awaitable<boolean>;
|
|
144
|
+
clear(): Awaitable<void>;
|
|
145
|
+
getSize(): Awaitable<number>;
|
|
146
|
+
keys(): Awaitable<string[]>;
|
|
147
|
+
values(): Awaitable<Raw[]>;
|
|
148
|
+
entries(): Awaitable<[key: string, value: Raw][]>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* One {@link EntityCache} per entity kind.
|
|
152
|
+
*/
|
|
153
|
+
type CacheEntities = { readonly [Name in CacheEntityName]: EntityCache<CacheEntityTypes[Name]>; };
|
|
154
|
+
/**
|
|
155
|
+
* A storage-agnostic Discord cache: one {@link EntityCache} per entity kind, and nothing else.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* The cache is intentionally dumb: it has no knowledge of the gateway nor of the relations between entities. Cascading
|
|
159
|
+
* a gateway dispatch into every relevant entity cache (e.g. dropping a guild's channels on `GUILD_DELETE`) is done by
|
|
160
|
+
* {@link applyGatewayDispatch}, which only relies on this interface, so any custom implementation gets it for free.
|
|
161
|
+
*/
|
|
162
|
+
interface Cache extends CacheEntities {}
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/lib/operations.d.ts
|
|
165
|
+
/**
|
|
166
|
+
* The name of every entity cache held by a {@link Cache}.
|
|
167
|
+
*/
|
|
168
|
+
export declare const CacheEntityNames: readonly CacheEntityName[];
|
|
169
|
+
/**
|
|
170
|
+
* A single mutation a gateway dispatch produces on a {@link Cache}.
|
|
171
|
+
*/
|
|
172
|
+
type CacheOperation = {
|
|
173
|
+
type: "upsert";
|
|
174
|
+
store: CacheEntityName;
|
|
175
|
+
key: string;
|
|
176
|
+
raw: unknown;
|
|
177
|
+
/** Whether to shallow-merge `raw` onto the existing value, used for partial updates. */
|
|
178
|
+
merge?: boolean;
|
|
179
|
+
} | {
|
|
180
|
+
type: "update";
|
|
181
|
+
store: CacheEntityName;
|
|
182
|
+
key: string;
|
|
183
|
+
/** Computes the new value from the cached one. Nothing is written when the key is not cached. */
|
|
184
|
+
update: (value: unknown) => unknown;
|
|
185
|
+
} | {
|
|
186
|
+
type: "delete";
|
|
187
|
+
store: CacheEntityName;
|
|
188
|
+
key: string;
|
|
189
|
+
} | {
|
|
190
|
+
type: "deletePrefix";
|
|
191
|
+
store: CacheEntityName;
|
|
192
|
+
prefix: string;
|
|
193
|
+
} | {
|
|
194
|
+
type: "deleteWhere";
|
|
195
|
+
store: CacheEntityName;
|
|
196
|
+
predicate: (value: unknown) => boolean;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* What {@link createCacheOperations} needs to know besides the dispatch.
|
|
200
|
+
*/
|
|
201
|
+
interface CacheOperationContext {
|
|
202
|
+
/**
|
|
203
|
+
* The bot's user ID. Reactions and poll votes only carry the voter's ID, so without it the cached `me` and
|
|
204
|
+
* `me_voted` flags are never set.
|
|
205
|
+
*/
|
|
206
|
+
clientUserId?: string;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Translates a gateway dispatch into the list of {@link CacheOperation}s it implies, including the cascades (e.g.
|
|
210
|
+
* `CHANNEL_DELETE` also drops that channel's messages).
|
|
211
|
+
*
|
|
212
|
+
* @remarks
|
|
213
|
+
* This is a pure function, it does not touch any cache. Use {@link applyGatewayDispatch} to apply them.
|
|
214
|
+
* `INTERACTION_CREATE` is intentionally ignored: interactions are short-lived and never cached.
|
|
215
|
+
*
|
|
216
|
+
* @param payload The gateway dispatch payload.
|
|
217
|
+
* @param context The bot's user ID, for the `me` flags of reactions and poll votes.
|
|
218
|
+
*/
|
|
219
|
+
export declare function createCacheOperations(payload: GatewayDispatchPayload, context?: CacheOperationContext): CacheOperation[];
|
|
220
|
+
/**
|
|
221
|
+
* Applies a list of {@link CacheOperation}s to a {@link Cache}, sequentially and in order.
|
|
222
|
+
*
|
|
223
|
+
* @param cache The cache to mutate.
|
|
224
|
+
* @param operations The operations to apply, usually created by {@link createCacheOperations}.
|
|
225
|
+
*/
|
|
226
|
+
export declare function applyCacheOperations(cache: Cache, operations: readonly CacheOperation[]): Promise<void>;
|
|
227
|
+
/**
|
|
228
|
+
* Writes a gateway dispatch into every relevant entity cache of a {@link Cache}.
|
|
229
|
+
*
|
|
230
|
+
* @param cache The cache to mutate.
|
|
231
|
+
* @param payload The gateway dispatch payload.
|
|
232
|
+
* @param context The bot's user ID, for the `me` flags of reactions and poll votes.
|
|
233
|
+
*/
|
|
234
|
+
export declare function applyGatewayDispatch(cache: Cache, payload: GatewayDispatchPayload, context?: CacheOperationContext): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Shallow-merges `value` onto `existing` when both are plain objects, returning `value` otherwise.
|
|
237
|
+
*/
|
|
238
|
+
export declare function mergeValues<Value>(existing: Value | undefined, value: Value): Value;
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/lib/gateway.d.ts
|
|
241
|
+
/** A gateway that emits the dispatch event used by @discordjs/ws and @discordjs/core. */
|
|
242
|
+
interface GatewayDispatchSource {
|
|
243
|
+
on(event: "dispatch", listener: (payload: GatewayDispatchPayload, shardId: number) => void): unknown;
|
|
244
|
+
off?(event: "dispatch", listener: (payload: GatewayDispatchPayload, shardId: number) => void): unknown;
|
|
245
|
+
}
|
|
246
|
+
interface CacheGatewayOptions extends CacheOperationContext {
|
|
247
|
+
/** Receives cache write failures from the asynchronous gateway listener. */
|
|
248
|
+
onError?: (error: unknown, payload: GatewayDispatchPayload, shardId: number) => void;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Writes gateway dispatches into a cache. Call the returned function to stop listening.
|
|
252
|
+
* Use this when a gateway has no GatewayClient managing its dispatch queue.
|
|
253
|
+
*/
|
|
254
|
+
export declare function attachCacheToGateway(gateway: GatewayDispatchSource, cache: Cache, options?: CacheGatewayOptions): () => void;
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/lib/memory.d.ts
|
|
257
|
+
/**
|
|
258
|
+
* An {@link EntityCache} backed by a `Map`, optionally bounded as a least-recently-used cache.
|
|
259
|
+
*/
|
|
260
|
+
export declare class MemoryEntityCache<Raw> implements EntityCache<Raw> {
|
|
261
|
+
#private;
|
|
262
|
+
/**
|
|
263
|
+
* The maximum amount of entries, `Infinity` for an unbounded cache.
|
|
264
|
+
*/
|
|
265
|
+
readonly maxSize: number;
|
|
266
|
+
constructor(maxSize?: number);
|
|
267
|
+
get(key: string): Raw | undefined;
|
|
268
|
+
set(key: string, value: Raw): void;
|
|
269
|
+
has(key: string): boolean;
|
|
270
|
+
delete(key: string): boolean;
|
|
271
|
+
clear(): void;
|
|
272
|
+
getSize(): number;
|
|
273
|
+
keys(): string[];
|
|
274
|
+
values(): Raw[];
|
|
275
|
+
entries(): [key: string, value: Raw][];
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* A {@link Cache} whose entity caches are all {@link MemoryEntityCache}s.
|
|
279
|
+
*/
|
|
280
|
+
type InMemoryCache = { readonly [Name in CacheEntityName]: MemoryEntityCache<CacheEntityTypes[Name]>; };
|
|
281
|
+
interface InMemoryCacheOptions {
|
|
282
|
+
/**
|
|
283
|
+
* The maximum amount of entries kept per entity cache before evicting the least recently used ones, either for
|
|
284
|
+
* every entity cache or per entity cache. Entity caches left out are unbounded.
|
|
285
|
+
*
|
|
286
|
+
* @default Infinity
|
|
287
|
+
*/
|
|
288
|
+
maxSize?: number | Partial<Record<CacheEntityName, number>>;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Creates a {@link Cache} that keeps everything in the process' memory.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* import { createInMemoryCache } from '@wolfstar/plugin-cache';
|
|
296
|
+
*
|
|
297
|
+
* // Keep at most 1000 messages around, everything else is unbounded.
|
|
298
|
+
* const cache = createInMemoryCache({ maxSize: { messages: 1_000 } });
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* @param options The options for the cache.
|
|
302
|
+
*/
|
|
303
|
+
export declare function createInMemoryCache(options?: InMemoryCacheOptions): InMemoryCache & Cache;
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/lib/redis.d.ts
|
|
306
|
+
/**
|
|
307
|
+
* The subset of the [`ioredis`](https://github.com/redis/ioredis) client API the Redis cache relies on. An `ioredis`
|
|
308
|
+
* `Redis` or `Cluster` instance satisfies it, as can any other client exposing the same commands.
|
|
309
|
+
*/
|
|
310
|
+
interface RedisClientLike {
|
|
311
|
+
get(key: string): Promise<string | null>;
|
|
312
|
+
mget(...keys: string[]): Promise<(string | null)[]>;
|
|
313
|
+
set(key: string, value: string): Promise<unknown>;
|
|
314
|
+
set(key: string, value: string, mode: "PX", milliseconds: number): Promise<unknown>;
|
|
315
|
+
del(...keys: string[]): Promise<number>;
|
|
316
|
+
exists(...keys: string[]): Promise<number>;
|
|
317
|
+
zadd(key: string, ...scoreMembers: (string | number)[]): Promise<unknown>;
|
|
318
|
+
zrem(key: string, ...members: string[]): Promise<number>;
|
|
319
|
+
zrange(key: string, start: string, stop: string): Promise<string[]>;
|
|
320
|
+
zcard(key: string): Promise<number>;
|
|
321
|
+
zremrangebyscore(key: string, min: number | string, max: number | string): Promise<number>;
|
|
322
|
+
multi(): RedisTransactionLike;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* The subset of an [`ioredis`](https://github.com/redis/ioredis) `MULTI` transaction the Redis cache relies on: every
|
|
326
|
+
* queued command returns the transaction, and `exec` runs them atomically.
|
|
327
|
+
*/
|
|
328
|
+
interface RedisTransactionLike {
|
|
329
|
+
set(key: string, value: string): RedisTransactionLike;
|
|
330
|
+
set(key: string, value: string, mode: "PX", milliseconds: number): RedisTransactionLike;
|
|
331
|
+
del(...keys: string[]): RedisTransactionLike;
|
|
332
|
+
zadd(key: string, ...scoreMembers: (string | number)[]): RedisTransactionLike;
|
|
333
|
+
zrem(key: string, ...members: string[]): RedisTransactionLike;
|
|
334
|
+
zremrangebyscore(key: string, min: number | string, max: number | string): RedisTransactionLike;
|
|
335
|
+
exec(): Promise<[error: Error | null, result: unknown][] | null>;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Thrown when a value stored in Redis cannot be read back: invalid JSON, or compressed bytes that fail to decompress.
|
|
339
|
+
*
|
|
340
|
+
* @remarks
|
|
341
|
+
* A missing value is not an error, `get` resolves to `undefined` for it. Redis connection errors are not wrapped
|
|
342
|
+
* either, they propagate as the client throws them.
|
|
343
|
+
*/
|
|
344
|
+
export declare class CacheValueError extends Error {
|
|
345
|
+
/**
|
|
346
|
+
* The Redis key holding the unreadable value.
|
|
347
|
+
*/
|
|
348
|
+
readonly key: string;
|
|
349
|
+
constructor(key: string, cause: unknown);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* The algorithm used to compress values before writing them to Redis.
|
|
353
|
+
*/
|
|
354
|
+
type RedisCacheCompression = "gzip" | "brotli" | "none";
|
|
355
|
+
interface RedisEntityCacheOptions {
|
|
356
|
+
/**
|
|
357
|
+
* The prefix of every Redis key owned by this entity cache.
|
|
358
|
+
*/
|
|
359
|
+
prefix: string;
|
|
360
|
+
/**
|
|
361
|
+
* The time-to-live of every entry, in seconds. Entries never expire when omitted.
|
|
362
|
+
*/
|
|
363
|
+
ttl?: number;
|
|
364
|
+
/**
|
|
365
|
+
* The compression algorithm to use.
|
|
366
|
+
*
|
|
367
|
+
* @default "none"
|
|
368
|
+
*/
|
|
369
|
+
compression?: RedisCacheCompression;
|
|
370
|
+
/**
|
|
371
|
+
* The minimum size, in bytes, a serialized value must reach to be compressed. Small payloads rarely benefit from it.
|
|
372
|
+
*
|
|
373
|
+
* @default 1024
|
|
374
|
+
*/
|
|
375
|
+
compressionThreshold?: number;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* An {@link EntityCache} backed by Redis.
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* Every entry is stored as its own string key (`<prefix>:<key>`), and a sorted set (`<prefix>:@index`) tracks the
|
|
382
|
+
* stored keys with their expiration time as score, which is what `keys`, `entries`, `getSize`, and `clear` read from.
|
|
383
|
+
*/
|
|
384
|
+
export declare class RedisEntityCache<Raw> implements EntityCache<Raw> {
|
|
385
|
+
#private;
|
|
386
|
+
readonly prefix: string;
|
|
387
|
+
readonly ttl: number | undefined;
|
|
388
|
+
readonly compression: RedisCacheCompression;
|
|
389
|
+
readonly compressionThreshold: number;
|
|
390
|
+
constructor(redis: RedisClientLike, options: RedisEntityCacheOptions);
|
|
391
|
+
get(key: string): Promise<Raw | undefined>;
|
|
392
|
+
set(key: string, value: Raw): Promise<void>;
|
|
393
|
+
has(key: string): Promise<boolean>;
|
|
394
|
+
delete(key: string): Promise<boolean>;
|
|
395
|
+
clear(): Promise<void>;
|
|
396
|
+
getSize(): Promise<number>;
|
|
397
|
+
keys(): Promise<string[]>;
|
|
398
|
+
values(): Promise<Raw[]>;
|
|
399
|
+
entries(): Promise<[key: string, value: Raw][]>;
|
|
400
|
+
/**
|
|
401
|
+
* Gets the Redis key a value is stored at.
|
|
402
|
+
* @param key The entity cache key.
|
|
403
|
+
*/
|
|
404
|
+
valueKey(key: string): string;
|
|
405
|
+
/**
|
|
406
|
+
* The Redis key of the sorted set indexing the stored keys.
|
|
407
|
+
*/
|
|
408
|
+
get indexKey(): string;
|
|
409
|
+
private prune;
|
|
410
|
+
private serialize;
|
|
411
|
+
private deserialize;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* A {@link Cache} whose entity caches are all {@link RedisEntityCache}s.
|
|
415
|
+
*/
|
|
416
|
+
type RedisCache = { readonly [Name in CacheEntityName]: RedisEntityCache<CacheEntityTypes[Name]>; };
|
|
417
|
+
interface RedisCacheOptions {
|
|
418
|
+
/**
|
|
419
|
+
* The Redis client to use, e.g. an [`ioredis`](https://github.com/redis/ioredis) instance.
|
|
420
|
+
*/
|
|
421
|
+
redis: RedisClientLike;
|
|
422
|
+
/**
|
|
423
|
+
* The prefix of every Redis key owned by the cache, which allows several caches to share a database.
|
|
424
|
+
*
|
|
425
|
+
* @default "wolfstar:cache"
|
|
426
|
+
*/
|
|
427
|
+
prefix?: string;
|
|
428
|
+
/**
|
|
429
|
+
* The compression algorithm to use for values of at least {@link RedisCacheOptions.compressionThreshold} bytes.
|
|
430
|
+
*
|
|
431
|
+
* @default "none"
|
|
432
|
+
*/
|
|
433
|
+
compression?: RedisCacheCompression;
|
|
434
|
+
/**
|
|
435
|
+
* The minimum size, in bytes, a serialized value must reach to be compressed.
|
|
436
|
+
*
|
|
437
|
+
* @default 1024
|
|
438
|
+
*/
|
|
439
|
+
compressionThreshold?: number;
|
|
440
|
+
/**
|
|
441
|
+
* The time-to-live per entity cache, in seconds. Entity caches left out never expire.
|
|
442
|
+
*/
|
|
443
|
+
ttl?: Partial<Record<CacheEntityName, number>>;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* The default prefix of every Redis key owned by a cache created with {@link createRedisCache}.
|
|
447
|
+
*/
|
|
448
|
+
export declare const DefaultRedisCachePrefix = "wolfstar:cache";
|
|
449
|
+
/**
|
|
450
|
+
* Creates a {@link Cache} stored in Redis, optionally compressing its values.
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* import { createRedisCache } from '@wolfstar/plugin-cache';
|
|
455
|
+
* import { Redis } from 'ioredis';
|
|
456
|
+
*
|
|
457
|
+
* const cache = createRedisCache({
|
|
458
|
+
* redis: new Redis(process.env.REDIS_URL!),
|
|
459
|
+
* compression: 'gzip',
|
|
460
|
+
* ttl: { guilds: 60 * 60, users: 30 * 60 },
|
|
461
|
+
* });
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @param options The options for the cache.
|
|
465
|
+
*/
|
|
466
|
+
export declare function createRedisCache(options: RedisCacheOptions): RedisCache & Cache;
|
|
467
|
+
//#endregion
|
|
468
|
+
export type { Awaitable, Cache, CacheEntities, CacheEntityName, CacheEntityTypes, CacheGatewayOptions, CacheOperation, CacheOperationContext, EntityCache, GatewayDispatchSource, InMemoryCache, InMemoryCacheOptions, RedisCache, RedisCacheCompression, RedisCacheOptions, RedisClientLike, RedisEntityCacheOptions, RedisTransactionLike };
|
|
469
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/lib/keys.ts","../../src/lib/types.ts","../../src/lib/operations.ts","../../src/lib/gateway.ts","../../src/lib/memory.ts","../../src/lib/redis.ts"],"mappings":";;;;;wBAKgB,eAAe,SAAS,WAAW,IAAI;;;;wBAOvC,WAAW,WAAW,WAAW,WAAW;;;;wBAO5C,UAAU,SAAS,WAAW,QAAQ;;;;wBAOtC,YAAY,SAAS,WAAW,QAAQ;;;;wBAOxC,cAAc,SAAS,WAAW,QAAQ;;;;wBAO1C,QAAQ,SAAS,WAAW,QAAQ;;;;wBAOpC,SAAS,SAAS,WAAW,SAAS;;;;wBAOtC,WAAW,SAAS,WAAW,WAAW;;;;wBAO1C,kBAAkB,SAAS,WAAW,kBAAkB;;;;wBAOxD,iBAAiB,SAAS,WAAW,WAAW;;;;wBAOhD,mBAAmB,SAAS,WAAW,SAAS;;;;wBAOhD,sBAAsB,SAAS,WAAW,QAAQ;;;;wBAOlD,OAAO,SAAS,WAAW,QAAQ;;;;wBAOnC,eAAe,SAAS,WAAW,eAAe;;;;wBAOlD,UAAU,SAAS,8BAA8B;;;;wBAOjD,gBAAgB,UAAU,WAAW,QAAQ;;;;wBAO7C,iCACd,eAAe,WACf,SAAS,WACT,WAAW;;;;;;KCvFD,UAAU,KAAK,IAAI,QAAQ;;;;;;;;UAStB;EACf,+BAA+B;EAC/B,iBAAiB;IAAqB,UAAU;;EAChD,qBAAqB;EACrB,MAAM;EACN,UAAU;IAAe,WAAW;;EACpC,QAAQ;IAAa,UAAU;;EAC/B,cAAc;;;;;EAKd,QAAQ,KAAK,6CACX,QAAQ,KAAK,sCAAsC;EACrD,cAAc,uCAAuC;EACrD,SAAS;EAC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