encore.dev 1.54.2 → 1.55.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.
Files changed (41) hide show
  1. package/config/secrets.ts +7 -1
  2. package/dist/config/secrets.js +4 -0
  3. package/dist/config/secrets.js.map +1 -1
  4. package/dist/internal/runtime/napi/napi.cjs +3 -1
  5. package/dist/internal/runtime/napi/napi.d.cts +114 -1
  6. package/dist/storage/cache/basic.d.ts +268 -0
  7. package/dist/storage/cache/basic.js +383 -0
  8. package/dist/storage/cache/basic.js.map +1 -0
  9. package/dist/storage/cache/cluster.d.ts +48 -0
  10. package/dist/storage/cache/cluster.js +40 -0
  11. package/dist/storage/cache/cluster.js.map +1 -0
  12. package/dist/storage/cache/errors.d.ts +19 -0
  13. package/dist/storage/cache/errors.js +59 -0
  14. package/dist/storage/cache/errors.js.map +1 -0
  15. package/dist/storage/cache/expiry.d.ts +55 -0
  16. package/dist/storage/cache/expiry.js +74 -0
  17. package/dist/storage/cache/expiry.js.map +1 -0
  18. package/dist/storage/cache/keyspace.d.ts +77 -0
  19. package/dist/storage/cache/keyspace.js +100 -0
  20. package/dist/storage/cache/keyspace.js.map +1 -0
  21. package/dist/storage/cache/list.d.ts +249 -0
  22. package/dist/storage/cache/list.js +376 -0
  23. package/dist/storage/cache/list.js.map +1 -0
  24. package/dist/storage/cache/mod.d.ts +10 -0
  25. package/dist/storage/cache/mod.js +13 -0
  26. package/dist/storage/cache/mod.js.map +1 -0
  27. package/dist/storage/cache/set.d.ts +258 -0
  28. package/dist/storage/cache/set.js +411 -0
  29. package/dist/storage/cache/set.js.map +1 -0
  30. package/dist/tsconfig.tsbuildinfo +1 -1
  31. package/internal/runtime/napi/napi.cjs +3 -1
  32. package/internal/runtime/napi/napi.d.cts +114 -1
  33. package/package.json +6 -1
  34. package/storage/cache/basic.ts +511 -0
  35. package/storage/cache/cluster.ts +67 -0
  36. package/storage/cache/errors.ts +66 -0
  37. package/storage/cache/expiry.ts +98 -0
  38. package/storage/cache/keyspace.ts +142 -0
  39. package/storage/cache/list.ts +496 -0
  40. package/storage/cache/mod.ts +36 -0
  41. package/storage/cache/set.ts +491 -0
@@ -0,0 +1,383 @@
1
+ import { getCurrentRequest } from "../../internal/reqtrack/mod.js";
2
+ import { CacheMiss, CacheKeyExists } from "./errors.js";
3
+ import { Keyspace } from "./keyspace.js";
4
+ /**
5
+ * Base class for basic (scalar value) keyspaces.
6
+ * Provides get/set/replace/etc operations.
7
+ * @internal
8
+ */
9
+ class BasicKeyspace extends Keyspace {
10
+ constructor(cluster, config) {
11
+ super(cluster, config);
12
+ }
13
+ /**
14
+ * Gets the value stored at key.
15
+ * If the key does not exist, it returns `undefined`.
16
+ *
17
+ * @returns The value, or `undefined` if the key does not exist.
18
+ * @see https://redis.io/commands/get/
19
+ */
20
+ async get(key) {
21
+ const source = getCurrentRequest();
22
+ const mappedKey = this.mapKey(key);
23
+ const result = await this.cluster.impl.get(mappedKey, source);
24
+ if (result === null) {
25
+ return undefined;
26
+ }
27
+ return this.deserialize(result);
28
+ }
29
+ /**
30
+ * Gets the values stored at multiple keys.
31
+ *
32
+ * @returns An array of values in the same order as the provided keys.
33
+ * Each element is the value or `undefined` if the key was not found.
34
+ * @see https://redis.io/commands/mget/
35
+ */
36
+ async multiGet(...keys) {
37
+ const source = getCurrentRequest();
38
+ const mappedKeys = keys.map((k) => this.mapKey(k));
39
+ const results = await this.cluster.impl.mget(mappedKeys, source);
40
+ return results.map((r) => r === null || r === undefined ? undefined : this.deserialize(r));
41
+ }
42
+ /**
43
+ * Updates the value stored at key to val.
44
+ *
45
+ * @see https://redis.io/commands/set/
46
+ */
47
+ async set(key, value, options) {
48
+ const source = getCurrentRequest();
49
+ const mappedKey = this.mapKey(key);
50
+ const serialized = this.serialize(value);
51
+ const ttlMs = this.resolveTtl(options);
52
+ await this.cluster.impl.set(mappedKey, serialized, ttlMs, source);
53
+ }
54
+ /**
55
+ * Sets the value stored at key to val, but only if the key does not exist beforehand.
56
+ *
57
+ * @throws {CacheKeyExists} If the key already exists.
58
+ * @see https://redis.io/commands/setnx/
59
+ */
60
+ async setIfNotExists(key, value, options) {
61
+ const source = getCurrentRequest();
62
+ const mappedKey = this.mapKey(key);
63
+ const serialized = this.serialize(value);
64
+ const ttlMs = this.resolveTtl(options);
65
+ const set = await this.cluster.impl.setIfNotExists(mappedKey, serialized, ttlMs, source);
66
+ if (!set) {
67
+ throw new CacheKeyExists(mappedKey);
68
+ }
69
+ }
70
+ /**
71
+ * Replaces the existing value stored at key to val.
72
+ *
73
+ * @throws {CacheMiss} If the key does not already exist.
74
+ * @see https://redis.io/commands/set/
75
+ */
76
+ async replace(key, value, options) {
77
+ const source = getCurrentRequest();
78
+ const mappedKey = this.mapKey(key);
79
+ const serialized = this.serialize(value);
80
+ const ttlMs = this.resolveTtl(options);
81
+ const replaced = await this.cluster.impl.replace(mappedKey, serialized, ttlMs, source);
82
+ if (!replaced) {
83
+ throw new CacheMiss(mappedKey);
84
+ }
85
+ }
86
+ /**
87
+ * Updates the value of key to val and returns the previously stored value.
88
+ * If the key does not already exist, it sets it and returns `undefined`.
89
+ *
90
+ * @returns The previous value, or `undefined` if the key did not exist.
91
+ * @see https://redis.io/commands/getset/
92
+ */
93
+ async getAndSet(key, value, options) {
94
+ const source = getCurrentRequest();
95
+ const mappedKey = this.mapKey(key);
96
+ const serialized = this.serialize(value);
97
+ const ttlMs = this.resolveTtl(options);
98
+ const oldValue = await this.cluster.impl.getAndSet(mappedKey, serialized, ttlMs, source);
99
+ if (oldValue === null) {
100
+ return undefined;
101
+ }
102
+ return this.deserialize(oldValue);
103
+ }
104
+ /**
105
+ * Deletes the key and returns the previously stored value.
106
+ * If the key does not already exist, it returns `undefined`.
107
+ *
108
+ * @returns The previous value, or `undefined` if the key did not exist.
109
+ * @see https://redis.io/commands/getdel/
110
+ */
111
+ async getAndDelete(key) {
112
+ const source = getCurrentRequest();
113
+ const mappedKey = this.mapKey(key);
114
+ const value = await this.cluster.impl.getAndDelete(mappedKey, source);
115
+ if (value === null) {
116
+ return undefined;
117
+ }
118
+ return this.deserialize(value);
119
+ }
120
+ }
121
+ /**
122
+ * StringKeyspace stores string values.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * const tokens = new StringKeyspace<string>(cluster, {
127
+ * keyPattern: "token/:id",
128
+ * defaultExpiry: ExpireIn(3600000), // 1 hour
129
+ * });
130
+ *
131
+ * await tokens.set("abc123", "user-token-value");
132
+ * const token = await tokens.get("abc123");
133
+ * ```
134
+ */
135
+ export class StringKeyspace extends BasicKeyspace {
136
+ constructor(cluster, config) {
137
+ super(cluster, config);
138
+ }
139
+ serialize(value) {
140
+ return Buffer.from(value, "utf-8");
141
+ }
142
+ deserialize(data) {
143
+ return data.toString("utf-8");
144
+ }
145
+ /**
146
+ * Appends a string to the value stored at key.
147
+ *
148
+ * If the key does not exist it is first created and set as the empty string,
149
+ * causing append to behave like set.
150
+ *
151
+ * @returns The new string length.
152
+ * @see https://redis.io/commands/append/
153
+ */
154
+ async append(key, value, options) {
155
+ const source = getCurrentRequest();
156
+ const mappedKey = this.mapKey(key);
157
+ const ttlMs = this.resolveTtl(options);
158
+ const result = await this.cluster.impl.append(mappedKey, Buffer.from(value, "utf-8"), ttlMs, source);
159
+ return Number(result);
160
+ }
161
+ /**
162
+ * Returns a substring of the string value stored at key.
163
+ *
164
+ * The `start` and `end` values are zero-based indices, but unlike typical slicing
165
+ * the `end` value is inclusive.
166
+ *
167
+ * Negative values can be used in order to provide an offset starting
168
+ * from the end of the string, so -1 means the last character.
169
+ *
170
+ * If the string does not exist it returns the empty string.
171
+ *
172
+ * @param key - The cache key.
173
+ * @param start - Start index (inclusive, 0-based).
174
+ * @param end - End index (inclusive, 0-based). Use -1 for end of string.
175
+ * @returns The substring.
176
+ * @see https://redis.io/commands/getrange/
177
+ */
178
+ async getRange(key, start, end) {
179
+ const source = getCurrentRequest();
180
+ const mappedKey = this.mapKey(key);
181
+ const result = await this.cluster.impl.getRange(mappedKey, start, end, source);
182
+ return result.toString("utf-8");
183
+ }
184
+ /**
185
+ * Overwrites part of the string stored at key, starting at
186
+ * the zero-based `offset` and for the entire length of `value`, extending
187
+ * the string if necessary.
188
+ *
189
+ * If the offset is larger than the current string length stored at key,
190
+ * the string is first padded with zero-bytes to make offset fit.
191
+ *
192
+ * Non-existing keys are considered as empty strings.
193
+ *
194
+ * @param key - The cache key.
195
+ * @param offset - Zero-based byte offset to start writing at.
196
+ * @param value - The string to write.
197
+ * @returns The length of the string after the operation.
198
+ * @see https://redis.io/commands/setrange/
199
+ */
200
+ async setRange(key, offset, value, options) {
201
+ const source = getCurrentRequest();
202
+ const mappedKey = this.mapKey(key);
203
+ const ttlMs = this.resolveTtl(options);
204
+ const result = await this.cluster.impl.setRange(mappedKey, offset, Buffer.from(value, "utf-8"), ttlMs, source);
205
+ return Number(result);
206
+ }
207
+ /**
208
+ * Returns the length of the string value stored at key.
209
+ *
210
+ * Non-existing keys are considered as empty strings.
211
+ *
212
+ * @returns The string length.
213
+ * @see https://redis.io/commands/strlen/
214
+ */
215
+ async len(key) {
216
+ const source = getCurrentRequest();
217
+ const mappedKey = this.mapKey(key);
218
+ const result = await this.cluster.impl.strlen(mappedKey, source);
219
+ return Number(result);
220
+ }
221
+ }
222
+ /**
223
+ * IntKeyspace stores 64-bit integer values.
224
+ * Values are floored to integers using `Math.floor`.
225
+ * For fractional values, use {@link FloatKeyspace} instead.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * const counters = new IntKeyspace<string>(cluster, {
230
+ * keyPattern: "counter/:name",
231
+ * });
232
+ *
233
+ * await counters.set("page-views", 0);
234
+ * const newCount = await counters.increment("page-views", 1);
235
+ * ```
236
+ */
237
+ export class IntKeyspace extends BasicKeyspace {
238
+ constructor(cluster, config) {
239
+ super(cluster, config);
240
+ }
241
+ serialize(value) {
242
+ return Buffer.from(String(Math.floor(value)), "utf-8");
243
+ }
244
+ deserialize(data) {
245
+ return parseInt(data.toString("utf-8"), 10);
246
+ }
247
+ /**
248
+ * Increments the number stored at key by `delta`.
249
+ *
250
+ * If the key does not exist it is first created with a value of 0
251
+ * before incrementing.
252
+ *
253
+ * Negative values can be used to decrease the value,
254
+ * but typically you want to use {@link decrement} for that.
255
+ *
256
+ * @param key - The cache key.
257
+ * @param delta - The amount to increment by (default 1).
258
+ * @returns The new value after incrementing.
259
+ * @see https://redis.io/commands/incrby/
260
+ */
261
+ async increment(key, delta = 1, options) {
262
+ const source = getCurrentRequest();
263
+ const mappedKey = this.mapKey(key);
264
+ const ttlMs = this.resolveTtl(options);
265
+ return await this.cluster.impl.incrBy(mappedKey, Math.floor(delta), ttlMs, source);
266
+ }
267
+ /**
268
+ * Decrements the number stored at key by `delta`.
269
+ *
270
+ * If the key does not exist it is first created with a value of 0
271
+ * before decrementing.
272
+ *
273
+ * Negative values can be used to increase the value,
274
+ * but typically you want to use {@link increment} for that.
275
+ *
276
+ * @param key - The cache key.
277
+ * @param delta - The amount to decrement by (default 1).
278
+ * @returns The new value after decrementing.
279
+ * @see https://redis.io/commands/decrby/
280
+ */
281
+ async decrement(key, delta = 1, options) {
282
+ const source = getCurrentRequest();
283
+ const mappedKey = this.mapKey(key);
284
+ const ttlMs = this.resolveTtl(options);
285
+ return await this.cluster.impl.decrBy(mappedKey, Math.floor(delta), ttlMs, source);
286
+ }
287
+ }
288
+ /**
289
+ * FloatKeyspace stores 64-bit floating point values.
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * const scores = new FloatKeyspace<string>(cluster, {
294
+ * keyPattern: "score/:playerId",
295
+ * });
296
+ *
297
+ * await scores.set("player1", 100.5);
298
+ * const newScore = await scores.increment("player1", 10.25);
299
+ * ```
300
+ */
301
+ export class FloatKeyspace extends BasicKeyspace {
302
+ constructor(cluster, config) {
303
+ super(cluster, config);
304
+ }
305
+ serialize(value) {
306
+ return Buffer.from(String(value), "utf-8");
307
+ }
308
+ deserialize(data) {
309
+ return parseFloat(data.toString("utf-8"));
310
+ }
311
+ /**
312
+ * Increments the number stored at key by `delta`.
313
+ *
314
+ * If the key does not exist it is first created with a value of 0
315
+ * before incrementing.
316
+ *
317
+ * Negative values can be used to decrease the value,
318
+ * but typically you want to use {@link decrement} for that.
319
+ *
320
+ * @param key - The cache key.
321
+ * @param delta - The amount to increment by (default 1).
322
+ * @returns The new value after incrementing.
323
+ * @see https://redis.io/commands/incrbyfloat/
324
+ */
325
+ async increment(key, delta = 1, options) {
326
+ const source = getCurrentRequest();
327
+ const mappedKey = this.mapKey(key);
328
+ const ttlMs = this.resolveTtl(options);
329
+ return await this.cluster.impl.incrByFloat(mappedKey, delta, ttlMs, source);
330
+ }
331
+ /**
332
+ * Decrements the number stored at key by `delta`.
333
+ *
334
+ * If the key does not exist it is first created with a value of 0
335
+ * before decrementing.
336
+ *
337
+ * Negative values can be used to increase the value,
338
+ * but typically you want to use {@link increment} for that.
339
+ *
340
+ * @param key - The cache key.
341
+ * @param delta - The amount to decrement by (default 1).
342
+ * @returns The new value after decrementing.
343
+ * @see https://redis.io/commands/incrbyfloat/
344
+ */
345
+ async decrement(key, delta = 1, options) {
346
+ const source = getCurrentRequest();
347
+ const mappedKey = this.mapKey(key);
348
+ const ttlMs = this.resolveTtl(options);
349
+ return await this.cluster.impl.incrByFloat(mappedKey, -delta, ttlMs, source);
350
+ }
351
+ }
352
+ /**
353
+ * StructKeyspace stores arbitrary objects serialized as JSON.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * interface User {
358
+ * id: string;
359
+ * name: string;
360
+ * email: string;
361
+ * }
362
+ *
363
+ * const users = new StructKeyspace<string, User>(cluster, {
364
+ * keyPattern: "user/:id",
365
+ * defaultExpiry: ExpireIn(3600000),
366
+ * });
367
+ *
368
+ * await users.set("user1", { id: "user1", name: "Alice", email: "alice@example.com" });
369
+ * const user = await users.get("user1");
370
+ * ```
371
+ */
372
+ export class StructKeyspace extends BasicKeyspace {
373
+ constructor(cluster, config) {
374
+ super(cluster, config);
375
+ }
376
+ serialize(value) {
377
+ return Buffer.from(JSON.stringify(value), "utf-8");
378
+ }
379
+ deserialize(data) {
380
+ return JSON.parse(data.toString("utf-8"));
381
+ }
382
+ }
383
+ //# sourceMappingURL=basic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basic.js","sourceRoot":"","sources":["../../../storage/cache/basic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAgC,MAAM,YAAY,CAAC;AAEpE;;;;GAIG;AACH,MAAe,aAAoB,SAAQ,QAAW;IACpD,YAAY,OAAqB,EAAE,MAAyB;QAC1D,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACzB,CAAC;IAYD;;;;;;OAMG;IACH,KAAK,CAAC,GAAG,CAAC,GAAM;QACd,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAC;QACnC,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAE9D,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAS;QACzB,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAC;QACnC,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QACnD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CA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;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,cAAqB,SAAQ,aAAmB;IAC3D,YAAY,OAAqB,EAAE,MAAyB;QAC1D,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACzB,CAAC;IAES,SAAS,CAAC,KAAQ;QAC1B,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;IAES,WAAW,CAAC,IAAY;QAChC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAM,CAAC;IACjD,CAAC;CACF"}
@@ -0,0 +1,48 @@
1
+ import * as runtime from "../../internal/runtime/mod.js";
2
+ import { StringLiteral } from "../../internal/utils/constraints.js";
3
+ /**
4
+ * Redis eviction policy that determines how keys are evicted when memory is full.
5
+ */
6
+ export type EvictionPolicy = "noeviction" | "allkeys-lru" | "allkeys-lfu" | "allkeys-random" | "volatile-lru" | "volatile-lfu" | "volatile-ttl" | "volatile-random";
7
+ /**
8
+ * Configuration options for a cache cluster.
9
+ */
10
+ export interface CacheClusterConfig {
11
+ /**
12
+ * The eviction policy to use when the cache is full.
13
+ * Defaults to "allkeys-lru".
14
+ */
15
+ evictionPolicy?: EvictionPolicy;
16
+ }
17
+ /**
18
+ * CacheCluster represents a Redis cache cluster.
19
+ *
20
+ * Create a new cluster using `new CacheCluster(name)`.
21
+ * Reference an existing cluster using `CacheCluster.named(name)`.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { CacheCluster } from "encore.dev/storage/cache";
26
+ *
27
+ * const myCache = new CacheCluster("my-cache", {
28
+ * evictionPolicy: "allkeys-lru",
29
+ * });
30
+ * ```
31
+ */
32
+ export declare class CacheCluster {
33
+ /** @internal */
34
+ readonly impl: runtime.CacheCluster;
35
+ /** @internal */
36
+ readonly clusterName: string;
37
+ /**
38
+ * Creates a new cache cluster with the given name and configuration.
39
+ * @param name - The unique name for this cache cluster
40
+ * @param cfg - Optional configuration for the cluster
41
+ */
42
+ constructor(name: string, cfg?: CacheClusterConfig);
43
+ /**
44
+ * Reference an existing cache cluster by name.
45
+ * To create a new cache cluster, use `new CacheCluster(...)` instead.
46
+ */
47
+ static named<Name extends string>(name: StringLiteral<Name>): CacheCluster;
48
+ }
@@ -0,0 +1,40 @@
1
+ import * as runtime from "../../internal/runtime/mod.js";
2
+ /**
3
+ * CacheCluster represents a Redis cache cluster.
4
+ *
5
+ * Create a new cluster using `new CacheCluster(name)`.
6
+ * Reference an existing cluster using `CacheCluster.named(name)`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { CacheCluster } from "encore.dev/storage/cache";
11
+ *
12
+ * const myCache = new CacheCluster("my-cache", {
13
+ * evictionPolicy: "allkeys-lru",
14
+ * });
15
+ * ```
16
+ */
17
+ export class CacheCluster {
18
+ /** @internal */
19
+ impl;
20
+ /** @internal */
21
+ clusterName;
22
+ /**
23
+ * Creates a new cache cluster with the given name and configuration.
24
+ * @param name - The unique name for this cache cluster
25
+ * @param cfg - Optional configuration for the cluster
26
+ */
27
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
+ constructor(name, cfg) {
29
+ this.clusterName = name;
30
+ this.impl = runtime.RT.cacheCluster(name);
31
+ }
32
+ /**
33
+ * Reference an existing cache cluster by name.
34
+ * To create a new cache cluster, use `new CacheCluster(...)` instead.
35
+ */
36
+ static named(name) {
37
+ return new CacheCluster(name);
38
+ }
39
+ }
40
+ //# sourceMappingURL=cluster.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cluster.js","sourceRoot":"","sources":["../../../storage/cache/cluster.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,4BAA4B,CAAC;AA2BtD;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,YAAY;IACvB,gBAAgB;IACP,IAAI,CAAuB;IACpC,gBAAgB;IACP,WAAW,CAAS;IAE7B;;;;OAIG;IACH,6DAA6D;IAC7D,YAAY,IAAY,EAAE,GAAwB;QAChD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,KAAK,CAAsB,IAAyB;QACzD,OAAO,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * CacheError is the base class for all cache-related errors.
3
+ */
4
+ export declare class CacheError extends Error {
5
+ constructor(msg: string);
6
+ }
7
+ /**
8
+ * CacheMiss is thrown when a cache key is not found.
9
+ */
10
+ export declare class CacheMiss extends CacheError {
11
+ constructor(key: string);
12
+ }
13
+ /**
14
+ * CacheKeyExists is thrown when attempting to set a key that already exists
15
+ * using setIfNotExists.
16
+ */
17
+ export declare class CacheKeyExists extends CacheError {
18
+ constructor(key: string);
19
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * CacheError is the base class for all cache-related errors.
3
+ */
4
+ export class CacheError extends Error {
5
+ constructor(msg) {
6
+ // extending errors causes issues after you construct them, unless you apply the following fixes
7
+ super(msg);
8
+ // set error name as constructor name, make it not enumerable to keep native Error behavior
9
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors
10
+ Object.defineProperty(this, "name", {
11
+ value: "CacheError",
12
+ enumerable: false,
13
+ configurable: true
14
+ });
15
+ // Fix the prototype chain, capture stack trace.
16
+ Object.setPrototypeOf(this, CacheError.prototype);
17
+ Error.captureStackTrace(this, this.constructor);
18
+ }
19
+ }
20
+ /**
21
+ * CacheMiss is thrown when a cache key is not found.
22
+ */
23
+ export class CacheMiss extends CacheError {
24
+ constructor(key) {
25
+ // extending errors causes issues after you construct them, unless you apply the following fixes
26
+ super(`cache key "${key}" not found`);
27
+ // set error name as constructor name, make it not enumerable to keep native Error behavior
28
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors
29
+ Object.defineProperty(this, "name", {
30
+ value: "CacheMiss",
31
+ enumerable: false,
32
+ configurable: true
33
+ });
34
+ // Fix the prototype chain, capture stack trace.
35
+ Object.setPrototypeOf(this, CacheMiss.prototype);
36
+ Error.captureStackTrace(this, this.constructor);
37
+ }
38
+ }
39
+ /**
40
+ * CacheKeyExists is thrown when attempting to set a key that already exists
41
+ * using setIfNotExists.
42
+ */
43
+ export class CacheKeyExists extends CacheError {
44
+ constructor(key) {
45
+ // extending errors causes issues after you construct them, unless you apply the following fixes
46
+ super(`cache key "${key}" already exists`);
47
+ // set error name as constructor name, make it not enumerable to keep native Error behavior
48
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors
49
+ Object.defineProperty(this, "name", {
50
+ value: "CacheKeyExists",
51
+ enumerable: false,
52
+ configurable: true
53
+ });
54
+ // Fix the prototype chain, capture stack trace.
55
+ Object.setPrototypeOf(this, CacheKeyExists.prototype);
56
+ Error.captureStackTrace(this, this.constructor);
57
+ }
58
+ }
59
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../storage/cache/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,YAAY,GAAW;QACrB,gGAAgG;QAChG,KAAK,CAAC,GAAG,CAAC,CAAC;QAEX,2FAA2F;QAC3F,oHAAoH;QACpH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE;YAClC,KAAK,EAAE,YAAY;YACnB,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QAEH,gDAAgD;QAChD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;QAClD,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,SAAU,SAAQ,UAAU;IACvC,YAAY,GAAW;QACrB,gGAAgG;QAChG,KAAK,CAAC,cAAc,GAAG,aAAa,CAAC,CAAC;QAEtC,2FAA2F;QAC3F,oHAAoH;QACpH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE;YAClC,KAAK,EAAE,WAAW;YAClB,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QAEH,gDAAgD;QAChD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;QACjD,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,UAAU;IAC5C,YAAY,GAAW;QACrB,gGAAgG;QAChG,KAAK,CAAC,cAAc,GAAG,kBAAkB,CAAC,CAAC;QAE3C,2FAA2F;QAC3F,oHAAoH;QACpH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE;YAClC,KAAK,EAAE,gBAAgB;YACvB,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QAEH,gDAAgD;QAChD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,cAAc,CAAC,SAAS,CAAC,CAAC;QACtD,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;CACF"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Expiry represents a cache key expiration configuration.
3
+ * Use the helper functions to create expiry configurations.
4
+ */
5
+ export type Expiry = {
6
+ type: "duration";
7
+ durationMs: number;
8
+ } | {
9
+ type: "time";
10
+ hours: number;
11
+ minutes: number;
12
+ seconds: number;
13
+ } | "never" | "keep-ttl";
14
+ /**
15
+ * expireIn sets the cache entry to expire after the specified duration.
16
+ * @param ms - Duration in milliseconds
17
+ */
18
+ export declare function expireIn(ms: number): Expiry;
19
+ /**
20
+ * expireInSeconds sets the cache entry to expire after the specified seconds.
21
+ * @param seconds - Duration in seconds
22
+ */
23
+ export declare function expireInSeconds(seconds: number): Expiry;
24
+ /**
25
+ * expireInMinutes sets the cache entry to expire after the specified minutes.
26
+ * @param minutes - Duration in minutes
27
+ */
28
+ export declare function expireInMinutes(minutes: number): Expiry;
29
+ /**
30
+ * expireInHours sets the cache entry to expire after the specified hours.
31
+ * @param hours - Duration in hours
32
+ */
33
+ export declare function expireInHours(hours: number): Expiry;
34
+ /**
35
+ * expireDailyAt sets the cache entry to expire at a specific time each day (UTC).
36
+ * @param hours - Hour (0-23)
37
+ * @param minutes - Minutes (0-59)
38
+ * @param seconds - Seconds (0-59)
39
+ */
40
+ export declare function expireDailyAt(hours: number, minutes: number, seconds: number): Expiry;
41
+ /**
42
+ * neverExpire sets the cache entry to never expire.
43
+ * Note: Redis may still evict the key based on the eviction policy.
44
+ */
45
+ export declare const neverExpire: Expiry;
46
+ /**
47
+ * keepTTL preserves the existing TTL when updating a cache entry.
48
+ * If the key doesn't exist, no TTL is set.
49
+ */
50
+ export declare const keepTTL: Expiry;
51
+ /**
52
+ * Resolves an Expiry to a duration in milliseconds, "never", or "keep-ttl".
53
+ * @internal
54
+ */
55
+ export declare function resolveExpiry(expiry: Expiry): number | "never" | "keep-ttl";