@beignet/provider-cache-redis 0.0.32
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/CHANGELOG.md +162 -0
- package/LICENSE +21 -0
- package/README.md +256 -0
- package/dist/index.d.ts +176 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +414 -0
- package/dist/index.js.map +1 -0
- package/package.json +104 -0
- package/src/index.ts +536 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @beignet/provider-cache-redis
|
|
3
|
+
*
|
|
4
|
+
* Redis provider that extends ports with cache capabilities using ioredis.
|
|
5
|
+
*
|
|
6
|
+
* Configuration:
|
|
7
|
+
* - REDIS_URL: Redis connection URL (required)
|
|
8
|
+
* - REDIS_DB: Redis database number (optional, default: 0)
|
|
9
|
+
* - REDIS_CONNECT_TIMEOUT_MS: Initial connection timeout in milliseconds (optional, default: 5000)
|
|
10
|
+
* - REDIS_MAX_RETRIES_PER_REQUEST: Per-command retry limit (optional, default: 2)
|
|
11
|
+
* - REDIS_CONNECT_MAX_ATTEMPTS: Connection attempts before startup fails (optional, default: 3)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { createNextServer } from "@beignet/next";
|
|
16
|
+
* import { redisCacheProvider } from "@beignet/provider-cache-redis";
|
|
17
|
+
*
|
|
18
|
+
* const server = await createNextServer({
|
|
19
|
+
* ports: basePorts,
|
|
20
|
+
* providers: [redisCacheProvider],
|
|
21
|
+
* // ...
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // In your use cases:
|
|
25
|
+
* async function myUseCase(ctx: AppCtx) {
|
|
26
|
+
* const cached = await ctx.ports.cache.get("key");
|
|
27
|
+
* if (!cached) {
|
|
28
|
+
* const value = await fetchData();
|
|
29
|
+
* await ctx.ports.cache.set("key", value, { ttlSeconds: 3600 });
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { CachePort } from "@beignet/core/ports";
|
|
36
|
+
import {
|
|
37
|
+
createProvider,
|
|
38
|
+
createProviderInstrumentation,
|
|
39
|
+
} from "@beignet/core/providers";
|
|
40
|
+
import { Redis } from "ioredis";
|
|
41
|
+
import { z } from "zod";
|
|
42
|
+
|
|
43
|
+
const DEFAULT_CONNECT_TIMEOUT_MS = 5000;
|
|
44
|
+
const DEFAULT_MAX_RETRIES_PER_REQUEST = 2;
|
|
45
|
+
const DEFAULT_CONNECT_MAX_ATTEMPTS = 3;
|
|
46
|
+
const RECONNECT_BACKOFF_CAP_MS = 2000;
|
|
47
|
+
|
|
48
|
+
const NumberString = z
|
|
49
|
+
.string()
|
|
50
|
+
.regex(/^\d+$/, "Expected a non-negative integer string")
|
|
51
|
+
.transform((value) => Number.parseInt(value, 10));
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Configuration schema for the Redis provider.
|
|
55
|
+
* Validates environment variables with REDIS_ prefix.
|
|
56
|
+
*/
|
|
57
|
+
const RedisConfigSchema = z.object({
|
|
58
|
+
/**
|
|
59
|
+
* Redis connection URL.
|
|
60
|
+
* Example: "redis://localhost:6379"
|
|
61
|
+
*/
|
|
62
|
+
URL: z.string().url(),
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Redis database number (optional).
|
|
66
|
+
* Defaults to 0 if not specified.
|
|
67
|
+
*/
|
|
68
|
+
DB: NumberString.optional(),
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Timeout for the initial connection attempt, in milliseconds.
|
|
72
|
+
* Defaults to 5000.
|
|
73
|
+
*/
|
|
74
|
+
CONNECT_TIMEOUT_MS: NumberString.default(DEFAULT_CONNECT_TIMEOUT_MS),
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* How many times a single command is retried before its promise rejects.
|
|
78
|
+
* Defaults to 2.
|
|
79
|
+
*/
|
|
80
|
+
MAX_RETRIES_PER_REQUEST: NumberString.default(
|
|
81
|
+
DEFAULT_MAX_RETRIES_PER_REQUEST,
|
|
82
|
+
),
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Connection attempts allowed before startup `connect()` rejects.
|
|
86
|
+
* Defaults to 3.
|
|
87
|
+
*/
|
|
88
|
+
CONNECT_MAX_ATTEMPTS: NumberString.default(DEFAULT_CONNECT_MAX_ATTEMPTS),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Inferred configuration type for Redis provider.
|
|
93
|
+
*/
|
|
94
|
+
export type RedisConfig = z.infer<typeof RedisConfigSchema>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Options for {@link createRedisCacheProvider}.
|
|
98
|
+
*
|
|
99
|
+
* Numeric options become schema defaults, so matching `REDIS_*` environment
|
|
100
|
+
* variables still win when both are set.
|
|
101
|
+
*/
|
|
102
|
+
export interface RedisCacheProviderOptions {
|
|
103
|
+
/**
|
|
104
|
+
* Timeout for the initial connection attempt, in milliseconds.
|
|
105
|
+
*
|
|
106
|
+
* @default 5000
|
|
107
|
+
*/
|
|
108
|
+
connectTimeoutMs?: number;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* How many times a single command is retried before its promise rejects.
|
|
112
|
+
*
|
|
113
|
+
* @default 2
|
|
114
|
+
*/
|
|
115
|
+
maxRetriesPerRequest?: number;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Custom ioredis retry strategy. Replaces the default strategy, which stops
|
|
119
|
+
* retrying after `REDIS_CONNECT_MAX_ATTEMPTS` during the initial connect and
|
|
120
|
+
* reconnects with capped exponential backoff afterwards.
|
|
121
|
+
*/
|
|
122
|
+
retryStrategy?: (times: number) => number | null;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Redis database number.
|
|
126
|
+
*
|
|
127
|
+
* @default 0
|
|
128
|
+
*/
|
|
129
|
+
db?: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface RedisHealth {
|
|
133
|
+
ok: boolean;
|
|
134
|
+
message?: string;
|
|
135
|
+
metadata: {
|
|
136
|
+
adapter: "redis";
|
|
137
|
+
response?: string;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Escape hatch for apps that need the raw ioredis client.
|
|
143
|
+
*/
|
|
144
|
+
export interface RedisEscapeHatch {
|
|
145
|
+
/**
|
|
146
|
+
* Raw ioredis client.
|
|
147
|
+
* Use this for Redis operations the stable cache port does not model.
|
|
148
|
+
*/
|
|
149
|
+
client: Redis;
|
|
150
|
+
/**
|
|
151
|
+
* Check whether Redis responds to a cheap PING command.
|
|
152
|
+
*/
|
|
153
|
+
checkHealth(): Promise<RedisHealth>;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Ports contributed by the Redis provider.
|
|
158
|
+
*/
|
|
159
|
+
export interface RedisCacheProviderPorts {
|
|
160
|
+
/**
|
|
161
|
+
* Beignet cache port backed by Redis.
|
|
162
|
+
*/
|
|
163
|
+
cache: CachePort;
|
|
164
|
+
/**
|
|
165
|
+
* Raw ioredis client escape hatch.
|
|
166
|
+
*/
|
|
167
|
+
redis: RedisEscapeHatch;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function errorMessage(error: unknown): string {
|
|
171
|
+
return error instanceof Error ? error.message : String(error);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function redactConnectionUrl(url: string): string {
|
|
175
|
+
try {
|
|
176
|
+
const parsed = new URL(url);
|
|
177
|
+
if (parsed.username) parsed.username = "redacted";
|
|
178
|
+
if (parsed.password) parsed.password = "redacted";
|
|
179
|
+
if (parsed.search) parsed.search = "?redacted";
|
|
180
|
+
parsed.hash = "";
|
|
181
|
+
return parsed.toString();
|
|
182
|
+
} catch {
|
|
183
|
+
return "[invalid URL]";
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function reconnectBackoff(attempt: number): number {
|
|
188
|
+
return Math.min(2 ** attempt * 100, RECONNECT_BACKOFF_CAP_MS);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function checkRedisHealth(client: Redis): Promise<RedisHealth> {
|
|
192
|
+
try {
|
|
193
|
+
const response = await client.ping();
|
|
194
|
+
return {
|
|
195
|
+
ok: true,
|
|
196
|
+
metadata: { adapter: "redis", response },
|
|
197
|
+
};
|
|
198
|
+
} catch (error) {
|
|
199
|
+
return {
|
|
200
|
+
ok: false,
|
|
201
|
+
message: errorMessage(error),
|
|
202
|
+
metadata: { adapter: "redis" },
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Create an env-backed Redis cache provider.
|
|
209
|
+
*
|
|
210
|
+
* Reads `REDIS_*` env vars, contributes `ports.cache`, and exposes
|
|
211
|
+
* `ports.redis` for raw ioredis client access.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* const provider = createRedisCacheProvider({ connectTimeoutMs: 2000 });
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function createRedisCacheProvider(
|
|
219
|
+
options: RedisCacheProviderOptions = {},
|
|
220
|
+
) {
|
|
221
|
+
const ConfigSchema = RedisConfigSchema.extend({
|
|
222
|
+
DB:
|
|
223
|
+
options.db !== undefined
|
|
224
|
+
? NumberString.default(options.db)
|
|
225
|
+
: RedisConfigSchema.shape.DB,
|
|
226
|
+
CONNECT_TIMEOUT_MS:
|
|
227
|
+
options.connectTimeoutMs !== undefined
|
|
228
|
+
? NumberString.default(options.connectTimeoutMs)
|
|
229
|
+
: RedisConfigSchema.shape.CONNECT_TIMEOUT_MS,
|
|
230
|
+
MAX_RETRIES_PER_REQUEST:
|
|
231
|
+
options.maxRetriesPerRequest !== undefined
|
|
232
|
+
? NumberString.default(options.maxRetriesPerRequest)
|
|
233
|
+
: RedisConfigSchema.shape.MAX_RETRIES_PER_REQUEST,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
return createProvider({
|
|
237
|
+
name: "cache-redis",
|
|
238
|
+
|
|
239
|
+
config: {
|
|
240
|
+
schema: ConfigSchema,
|
|
241
|
+
envPrefix: "REDIS_",
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
async setup({ ports, config }) {
|
|
245
|
+
if (!config) {
|
|
246
|
+
throw new Error(
|
|
247
|
+
"[redisCacheProvider] Missing Redis config. " +
|
|
248
|
+
"Please set REDIS_URL environment variable.",
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const connectMaxAttempts =
|
|
253
|
+
config.CONNECT_MAX_ATTEMPTS ?? DEFAULT_CONNECT_MAX_ATTEMPTS;
|
|
254
|
+
|
|
255
|
+
// ioredis uses a single retryStrategy for both the initial connect and
|
|
256
|
+
// later reconnects, so track whether the first connect ever succeeded.
|
|
257
|
+
let connectedOnce = false;
|
|
258
|
+
|
|
259
|
+
const defaultRetryStrategy = (attempt: number): number | null => {
|
|
260
|
+
if (!connectedOnce) {
|
|
261
|
+
// Stop retrying during the initial connect so `connect()` below
|
|
262
|
+
// rejects promptly instead of hanging against an unreachable host.
|
|
263
|
+
if (attempt >= connectMaxAttempts) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
return reconnectBackoff(attempt);
|
|
267
|
+
}
|
|
268
|
+
// After a successful connection, keep reconnecting with capped
|
|
269
|
+
// exponential backoff.
|
|
270
|
+
return reconnectBackoff(attempt);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const client = new Redis(config.URL, {
|
|
274
|
+
db: config.DB ?? options.db ?? 0,
|
|
275
|
+
// Defer connecting until the explicit connect() below so startup can
|
|
276
|
+
// surface a clear error instead of connecting in the background.
|
|
277
|
+
lazyConnect: true,
|
|
278
|
+
connectTimeout:
|
|
279
|
+
config.CONNECT_TIMEOUT_MS ??
|
|
280
|
+
options.connectTimeoutMs ??
|
|
281
|
+
DEFAULT_CONNECT_TIMEOUT_MS,
|
|
282
|
+
maxRetriesPerRequest:
|
|
283
|
+
config.MAX_RETRIES_PER_REQUEST ??
|
|
284
|
+
options.maxRetriesPerRequest ??
|
|
285
|
+
DEFAULT_MAX_RETRIES_PER_REQUEST,
|
|
286
|
+
retryStrategy: options.retryStrategy ?? defaultRetryStrategy,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// Test connection
|
|
290
|
+
try {
|
|
291
|
+
await client.connect();
|
|
292
|
+
connectedOnce = true;
|
|
293
|
+
} catch (error) {
|
|
294
|
+
client.disconnect();
|
|
295
|
+
throw new Error(
|
|
296
|
+
`[redisCacheProvider] Failed to connect to Redis at ${redactConnectionUrl(
|
|
297
|
+
config.URL,
|
|
298
|
+
)}: ${errorMessage(error)}`,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const instrumentation = createProviderInstrumentation(ports, {
|
|
303
|
+
providerName: "cache-redis",
|
|
304
|
+
watcher: "cache",
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const recordCacheEvent = (event: {
|
|
308
|
+
operation: string;
|
|
309
|
+
key: string;
|
|
310
|
+
summary: string;
|
|
311
|
+
details?: Record<string, unknown>;
|
|
312
|
+
}) => {
|
|
313
|
+
instrumentation.custom({
|
|
314
|
+
name: `cache.${event.operation}`,
|
|
315
|
+
label: `Cache ${event.operation}`,
|
|
316
|
+
summary: event.summary,
|
|
317
|
+
details: {
|
|
318
|
+
operation: event.operation,
|
|
319
|
+
key: event.key,
|
|
320
|
+
...event.details,
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// Extend ports with cache API
|
|
326
|
+
const cache: CachePort = {
|
|
327
|
+
get: async (key: string) => {
|
|
328
|
+
const startedAt = Date.now();
|
|
329
|
+
try {
|
|
330
|
+
const value = await client.get(key);
|
|
331
|
+
recordCacheEvent({
|
|
332
|
+
operation: "get",
|
|
333
|
+
key,
|
|
334
|
+
summary: value == null ? "Cache miss" : "Cache hit",
|
|
335
|
+
details: {
|
|
336
|
+
hit: value != null,
|
|
337
|
+
durationMs: Date.now() - startedAt,
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
return value;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
recordCacheEvent({
|
|
343
|
+
operation: "get.failed",
|
|
344
|
+
key,
|
|
345
|
+
summary: "Cache get failed",
|
|
346
|
+
details: {
|
|
347
|
+
durationMs: Date.now() - startedAt,
|
|
348
|
+
error: errorMessage(error),
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
|
|
355
|
+
set: async (key, value, options) => {
|
|
356
|
+
const startedAt = Date.now();
|
|
357
|
+
try {
|
|
358
|
+
if (options?.ttlSeconds != null && options.ttlSeconds > 0) {
|
|
359
|
+
await client.set(key, value, "EX", options.ttlSeconds);
|
|
360
|
+
} else {
|
|
361
|
+
await client.set(key, value);
|
|
362
|
+
}
|
|
363
|
+
recordCacheEvent({
|
|
364
|
+
operation: "set",
|
|
365
|
+
key,
|
|
366
|
+
summary: "Cache set",
|
|
367
|
+
details: {
|
|
368
|
+
ttlSeconds: options?.ttlSeconds ?? null,
|
|
369
|
+
durationMs: Date.now() - startedAt,
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
} catch (error) {
|
|
373
|
+
recordCacheEvent({
|
|
374
|
+
operation: "set.failed",
|
|
375
|
+
key,
|
|
376
|
+
summary: "Cache set failed",
|
|
377
|
+
details: {
|
|
378
|
+
ttlSeconds: options?.ttlSeconds ?? null,
|
|
379
|
+
durationMs: Date.now() - startedAt,
|
|
380
|
+
error: errorMessage(error),
|
|
381
|
+
},
|
|
382
|
+
});
|
|
383
|
+
throw error;
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
|
|
387
|
+
delete: async (key) => {
|
|
388
|
+
const startedAt = Date.now();
|
|
389
|
+
try {
|
|
390
|
+
const deleted = (await client.del(key)) > 0;
|
|
391
|
+
recordCacheEvent({
|
|
392
|
+
operation: "delete",
|
|
393
|
+
key,
|
|
394
|
+
summary: deleted ? "Cache key deleted" : "Cache key missing",
|
|
395
|
+
details: {
|
|
396
|
+
deleted,
|
|
397
|
+
durationMs: Date.now() - startedAt,
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
return deleted;
|
|
401
|
+
} catch (error) {
|
|
402
|
+
recordCacheEvent({
|
|
403
|
+
operation: "delete.failed",
|
|
404
|
+
key,
|
|
405
|
+
summary: "Cache delete failed",
|
|
406
|
+
details: {
|
|
407
|
+
durationMs: Date.now() - startedAt,
|
|
408
|
+
error: errorMessage(error),
|
|
409
|
+
},
|
|
410
|
+
});
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
|
|
415
|
+
has: async (key) => {
|
|
416
|
+
const startedAt = Date.now();
|
|
417
|
+
try {
|
|
418
|
+
const result = await client.exists(key);
|
|
419
|
+
const exists = result === 1;
|
|
420
|
+
recordCacheEvent({
|
|
421
|
+
operation: "has",
|
|
422
|
+
key,
|
|
423
|
+
summary: exists ? "Cache key exists" : "Cache key missing",
|
|
424
|
+
details: {
|
|
425
|
+
exists,
|
|
426
|
+
durationMs: Date.now() - startedAt,
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
return exists;
|
|
430
|
+
} catch (error) {
|
|
431
|
+
recordCacheEvent({
|
|
432
|
+
operation: "has.failed",
|
|
433
|
+
key,
|
|
434
|
+
summary: "Cache has failed",
|
|
435
|
+
details: {
|
|
436
|
+
durationMs: Date.now() - startedAt,
|
|
437
|
+
error: errorMessage(error),
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
throw error;
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
|
|
444
|
+
remember: async (key, factory, options) => {
|
|
445
|
+
const startedAt = Date.now();
|
|
446
|
+
try {
|
|
447
|
+
const cached = await client.get(key);
|
|
448
|
+
if (cached != null) {
|
|
449
|
+
recordCacheEvent({
|
|
450
|
+
operation: "remember",
|
|
451
|
+
key,
|
|
452
|
+
summary: "Cache remember hit",
|
|
453
|
+
details: {
|
|
454
|
+
hit: true,
|
|
455
|
+
ttlSeconds: options?.ttlSeconds ?? null,
|
|
456
|
+
durationMs: Date.now() - startedAt,
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
return cached;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const value = await factory();
|
|
463
|
+
if (options?.ttlSeconds != null && options.ttlSeconds > 0) {
|
|
464
|
+
await client.set(key, value, "EX", options.ttlSeconds);
|
|
465
|
+
} else {
|
|
466
|
+
await client.set(key, value);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
recordCacheEvent({
|
|
470
|
+
operation: "remember",
|
|
471
|
+
key,
|
|
472
|
+
summary: "Cache remember miss",
|
|
473
|
+
details: {
|
|
474
|
+
hit: false,
|
|
475
|
+
ttlSeconds: options?.ttlSeconds ?? null,
|
|
476
|
+
durationMs: Date.now() - startedAt,
|
|
477
|
+
},
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
return value;
|
|
481
|
+
} catch (error) {
|
|
482
|
+
recordCacheEvent({
|
|
483
|
+
operation: "remember.failed",
|
|
484
|
+
key,
|
|
485
|
+
summary: "Cache remember failed",
|
|
486
|
+
details: {
|
|
487
|
+
ttlSeconds: options?.ttlSeconds ?? null,
|
|
488
|
+
durationMs: Date.now() - startedAt,
|
|
489
|
+
error: errorMessage(error),
|
|
490
|
+
},
|
|
491
|
+
});
|
|
492
|
+
throw error;
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
return {
|
|
498
|
+
ports: {
|
|
499
|
+
cache,
|
|
500
|
+
redis: {
|
|
501
|
+
client,
|
|
502
|
+
checkHealth: () => checkRedisHealth(client),
|
|
503
|
+
},
|
|
504
|
+
} satisfies RedisCacheProviderPorts,
|
|
505
|
+
async stop() {
|
|
506
|
+
try {
|
|
507
|
+
await client.quit();
|
|
508
|
+
} catch {
|
|
509
|
+
// Silently ignore errors during shutdown
|
|
510
|
+
}
|
|
511
|
+
},
|
|
512
|
+
};
|
|
513
|
+
},
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Default env-backed Redis provider.
|
|
519
|
+
*
|
|
520
|
+
* Configuration via environment variables:
|
|
521
|
+
* - REDIS_URL: Redis connection URL (required)
|
|
522
|
+
* - REDIS_DB: Redis database number (optional)
|
|
523
|
+
* - REDIS_CONNECT_TIMEOUT_MS: Initial connection timeout in ms (optional)
|
|
524
|
+
* - REDIS_MAX_RETRIES_PER_REQUEST: Per-command retry limit (optional)
|
|
525
|
+
* - REDIS_CONNECT_MAX_ATTEMPTS: Connection attempts before startup fails (optional)
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* ```ts
|
|
529
|
+
* const server = await createNextServer({
|
|
530
|
+
* ports: basePorts,
|
|
531
|
+
* providers: [redisCacheProvider],
|
|
532
|
+
* // ...
|
|
533
|
+
* });
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
export const redisCacheProvider = createRedisCacheProvider();
|