@trieb.work/nextjs-turbo-redis-cache 1.5.1-beta.0 → 1.6.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/CHANGELOG.md +24 -0
- package/README.md +10 -35
- package/dist/index.d.mts +3 -13
- package/dist/index.d.ts +3 -13
- package/dist/index.js +15 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +15 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -16
- package/scripts/prepare.sh +0 -0
- package/scripts/setup-git.sh +0 -0
- package/scripts/setup-repo-name.sh +0 -0
- package/scripts/vitest-run-staged.cjs +0 -0
- package/src/RedisStringsHandler.ts +5 -18
- package/src/SyncedMap.ts +20 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# [1.6.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.5.1...v1.6.0) (2025-05-26)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* config ([d326ef4](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/d326ef46298a96535720f78d562a20b27e7b3c8f))
|
|
7
|
+
* config ([311d492](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/311d492c41d8c05b89d675753d79413632fc0019))
|
|
8
|
+
* process.env.redisUrl ([a57fd6a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/a57fd6a6b641015726edf11a00fa5c020dea0cca))
|
|
9
|
+
* readme ([2901ab0](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/2901ab0e0d646839e1779107aa9bea80deacf6c8))
|
|
10
|
+
* readme test startup ([e291666](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/e291666ed7b9d77c0f3baa68c3638bbad264a868))
|
|
11
|
+
* rename redis_url to redisUrl to align with other config option naming convention + fix: readme ([2e3f20d](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/2e3f20dce31a678b781913d230581461b3d67a07))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* disable keyspace config check ([0b8878d](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/0b8878d321808d00140664f51fc1d2d904cc4664))
|
|
17
|
+
|
|
18
|
+
## [1.5.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.5.0...v1.5.1) (2025-05-23)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* Update package.json ([63e6ffd](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/63e6ffdc21032cfb1c5ecb507276fd832ee8252a))
|
|
24
|
+
|
|
1
25
|
# [1.5.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.4.0...v1.5.0) (2025-05-16)
|
|
2
26
|
|
|
3
27
|
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@trieb.work/nextjs-turbo-redis-cache)
|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
The ultimate Redis caching solution for Next.js. Built for production-ready, large-scale projects, it delivers unparalleled performance and efficiency with features tailored for high-traffic applications. This package has been created after extensibly testing the @neshca package and finding several major issues with it.
|
|
6
|
+
The ultimate Redis caching solution for Next.js 15 and the app router. Built for production-ready, large-scale projects, it delivers unparalleled performance and efficiency with features tailored for high-traffic applications. This package has been created after extensibly testing the @neshca package and finding several major issues with it.
|
|
7
7
|
|
|
8
8
|
Key Features:
|
|
9
9
|
|
|
@@ -13,6 +13,8 @@ Key Features:
|
|
|
13
13
|
- _Efficient Tag Management_: in-memory tags map for lightning-fast revalidate operations with minimal Redis overhead.
|
|
14
14
|
- _Intelligent Key-Space Notifications_: Automatic update of in-memory tags map for expired or evicted keys.
|
|
15
15
|
|
|
16
|
+
This library offers you an easy and high performant caching solution for docker, Kubernetes or Google Cloud Run deployments of Next.js.
|
|
17
|
+
|
|
16
18
|
## Compatibility
|
|
17
19
|
|
|
18
20
|
This package is compatible with Next.js 15.0.3 and above while using App Router. It is not compatible with Next.js 14.x. or 15-canary or if you are using Pages Router.
|
|
@@ -46,6 +48,8 @@ REDISHOST and REDISPORT environment variables are required.
|
|
|
46
48
|
VERCEL_URL, VERCEL_ENV are optional. VERCEL_URL is used to create a key prefix for the redis keys. VERCEL_ENV is used to determine the database to use. Only VERCEL_ENV=production will show up in DB 0 (redis default db). All other values of VERCEL_ENV will use DB 1, use `redis-cli -n 1` to connect to different DB 1. This is another protection feature to avoid that different environments (e.g. staging and production) will overwrite each other.
|
|
47
49
|
Furthermore there exists the DEBUG_CACHE_HANDLER environment variable to enable debug logging of the caching handler once it is set to true.
|
|
48
50
|
|
|
51
|
+
There exists also the SKIP_KEYSPACE_CONFIG_CHECK environment variable to skip the check for the keyspace configuration. This is useful if you are using redis in a cloud environment that forbids access to config commands. If you set SKIP_KEYSPACE_CONFIG_CHECK=true the check will be skipped and the keyspace configuration will be assumed to be correct (e.g. notify-keyspace-events Exe).
|
|
52
|
+
|
|
49
53
|
### Option A: minimum implementation with default options
|
|
50
54
|
|
|
51
55
|
extend `next.config.js` with:
|
|
@@ -117,7 +121,7 @@ A working example of above can be found in the `test/integration/next-app-custom
|
|
|
117
121
|
|
|
118
122
|
| Option | Description | Default Value |
|
|
119
123
|
| ---------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
120
|
-
|
|
|
124
|
+
| redisUrl | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? redis://${process.env.REDISHOST}:${process.env.REDISPORT} : 'redis://localhost:6379'` |
|
|
121
125
|
| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` |
|
|
122
126
|
| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` |
|
|
123
127
|
| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` |
|
|
@@ -128,33 +132,6 @@ A working example of above can be found in the `test/integration/next-app-custom
|
|
|
128
132
|
| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` |
|
|
129
133
|
| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) |
|
|
130
134
|
| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`<br> Other: `staleAge * 1.2` |
|
|
131
|
-
| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `undefined` |
|
|
132
|
-
| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` |
|
|
133
|
-
|
|
134
|
-
## TLS Configuration
|
|
135
|
-
|
|
136
|
-
To connect to Redis using TLS/SSL (e.g., when using Redis over `rediss://` URLs), you can configure the socket options. Here's an example:
|
|
137
|
-
|
|
138
|
-
```javascript
|
|
139
|
-
const { RedisStringsHandler } = require('@trieb.work/nextjs-turbo-redis-cache');
|
|
140
|
-
|
|
141
|
-
let cachedHandler;
|
|
142
|
-
|
|
143
|
-
module.exports = class CustomizedCacheHandler {
|
|
144
|
-
constructor() {
|
|
145
|
-
if (!cachedHandler) {
|
|
146
|
-
cachedHandler = new RedisStringsHandler({
|
|
147
|
-
redis_url: 'rediss://your-redis-host:6380', // Note the rediss:// protocol
|
|
148
|
-
socketOptions: {
|
|
149
|
-
tls: true,
|
|
150
|
-
rejectUnauthorized: false, // Only use this if you want to skip certificate validation
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
// ... rest of the handler implementation
|
|
156
|
-
};
|
|
157
|
-
```
|
|
158
135
|
|
|
159
136
|
## Consistency of Redis and this caching implementation
|
|
160
137
|
|
|
@@ -197,12 +174,10 @@ By accepting and tolerating this eventual consistency, the performance of the ca
|
|
|
197
174
|
|
|
198
175
|
## Testing
|
|
199
176
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
To run all tests you can use the following command in a second terminal:
|
|
177
|
+
To run all tests you can use the following command:
|
|
203
178
|
|
|
204
179
|
```bash
|
|
205
|
-
pnpm test
|
|
180
|
+
pnpm build && pnpm test
|
|
206
181
|
```
|
|
207
182
|
|
|
208
183
|
### Unit tests
|
|
@@ -210,7 +185,7 @@ pnpm test
|
|
|
210
185
|
To run unit tests you can use the following command:
|
|
211
186
|
|
|
212
187
|
```bash
|
|
213
|
-
pnpm test:unit
|
|
188
|
+
pnpm build && pnpm test:unit
|
|
214
189
|
```
|
|
215
190
|
|
|
216
191
|
### Integration tests
|
|
@@ -218,7 +193,7 @@ pnpm test:unit
|
|
|
218
193
|
To run integration tests you can use the following command:
|
|
219
194
|
|
|
220
195
|
```bash
|
|
221
|
-
pnpm test:integration
|
|
196
|
+
pnpm build && pnpm test:integration
|
|
222
197
|
```
|
|
223
198
|
|
|
224
199
|
The integration tests will start a Next.js server and test the caching handler. You can modify testing behavior by setting the following environment variables:
|
package/dist/index.d.mts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import { RedisClientOptions } from 'redis';
|
|
2
|
-
|
|
3
1
|
type CacheEntry = {
|
|
4
2
|
value: unknown;
|
|
5
3
|
lastModified: number;
|
|
6
4
|
tags: string[];
|
|
7
5
|
};
|
|
8
6
|
type CreateRedisStringsHandlerOptions = {
|
|
9
|
-
/** Redis
|
|
7
|
+
/** Redis redisUrl to use.
|
|
10
8
|
* @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
|
|
11
9
|
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
|
|
12
10
|
: 'redis://localhost:6379'
|
|
13
11
|
*/
|
|
14
|
-
|
|
12
|
+
redisUrl?: string;
|
|
15
13
|
/** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
|
|
16
14
|
* @default process.env.VERCEL_ENV === 'production' ? 0 : 1
|
|
17
15
|
*/
|
|
@@ -52,14 +50,6 @@ type CreateRedisStringsHandlerOptions = {
|
|
|
52
50
|
* @default Production: staleAge * 2, Other: staleAge * 1.2
|
|
53
51
|
*/
|
|
54
52
|
estimateExpireAge?: (staleAge: number) => number;
|
|
55
|
-
/** Additional Redis client socket options
|
|
56
|
-
* @example { tls: true, rejectUnauthorized: false }
|
|
57
|
-
*/
|
|
58
|
-
socketOptions?: RedisClientOptions['socket'];
|
|
59
|
-
/** Additional Redis client options to be passed directly to createClient
|
|
60
|
-
* @example { username: 'user', password: 'pass' }
|
|
61
|
-
*/
|
|
62
|
-
clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;
|
|
63
53
|
};
|
|
64
54
|
declare class RedisStringsHandler {
|
|
65
55
|
private client;
|
|
@@ -75,7 +65,7 @@ declare class RedisStringsHandler {
|
|
|
75
65
|
private inMemoryCachingTime;
|
|
76
66
|
private defaultStaleAge;
|
|
77
67
|
private estimateExpireAge;
|
|
78
|
-
constructor({
|
|
68
|
+
constructor({ redisUrl, database, keyPrefix, sharedTagsKey, timeoutMs, revalidateTagQuerySize, avgResyncIntervalMs, redisGetDeduplication, inMemoryCachingTime, defaultStaleAge, estimateExpireAge, }: CreateRedisStringsHandlerOptions);
|
|
79
69
|
resetRequestCache(): void;
|
|
80
70
|
private assertClientIsReady;
|
|
81
71
|
get(key: string, ctx: {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import { RedisClientOptions } from 'redis';
|
|
2
|
-
|
|
3
1
|
type CacheEntry = {
|
|
4
2
|
value: unknown;
|
|
5
3
|
lastModified: number;
|
|
6
4
|
tags: string[];
|
|
7
5
|
};
|
|
8
6
|
type CreateRedisStringsHandlerOptions = {
|
|
9
|
-
/** Redis
|
|
7
|
+
/** Redis redisUrl to use.
|
|
10
8
|
* @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
|
|
11
9
|
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
|
|
12
10
|
: 'redis://localhost:6379'
|
|
13
11
|
*/
|
|
14
|
-
|
|
12
|
+
redisUrl?: string;
|
|
15
13
|
/** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
|
|
16
14
|
* @default process.env.VERCEL_ENV === 'production' ? 0 : 1
|
|
17
15
|
*/
|
|
@@ -52,14 +50,6 @@ type CreateRedisStringsHandlerOptions = {
|
|
|
52
50
|
* @default Production: staleAge * 2, Other: staleAge * 1.2
|
|
53
51
|
*/
|
|
54
52
|
estimateExpireAge?: (staleAge: number) => number;
|
|
55
|
-
/** Additional Redis client socket options
|
|
56
|
-
* @example { tls: true, rejectUnauthorized: false }
|
|
57
|
-
*/
|
|
58
|
-
socketOptions?: RedisClientOptions['socket'];
|
|
59
|
-
/** Additional Redis client options to be passed directly to createClient
|
|
60
|
-
* @example { username: 'user', password: 'pass' }
|
|
61
|
-
*/
|
|
62
|
-
clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;
|
|
63
53
|
};
|
|
64
54
|
declare class RedisStringsHandler {
|
|
65
55
|
private client;
|
|
@@ -75,7 +65,7 @@ declare class RedisStringsHandler {
|
|
|
75
65
|
private inMemoryCachingTime;
|
|
76
66
|
private defaultStaleAge;
|
|
77
67
|
private estimateExpireAge;
|
|
78
|
-
constructor({
|
|
68
|
+
constructor({ redisUrl, database, keyPrefix, sharedTagsKey, timeoutMs, revalidateTagQuerySize, avgResyncIntervalMs, redisGetDeduplication, inMemoryCachingTime, defaultStaleAge, estimateExpireAge, }: CreateRedisStringsHandlerOptions);
|
|
79
69
|
resetRequestCache(): void;
|
|
80
70
|
private assertClientIsReady;
|
|
81
71
|
get(key: string, ctx: {
|
package/dist/index.js
CHANGED
|
@@ -196,16 +196,18 @@ var SyncedMap = class {
|
|
|
196
196
|
await this.subscriberClient.connect().catch(async () => {
|
|
197
197
|
await this.subscriberClient.connect();
|
|
198
198
|
});
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
199
|
+
if ((process.env.SKIP_KEYSPACE_CONFIG_CHECK || "").toUpperCase() !== "TRUE") {
|
|
200
|
+
const keyspaceEventConfig = (await this.subscriberClient.configGet("notify-keyspace-events"))?.["notify-keyspace-events"];
|
|
201
|
+
if (!keyspaceEventConfig.includes("E")) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`"
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (!keyspaceEventConfig.includes("A") && !(keyspaceEventConfig.includes("x") && keyspaceEventConfig.includes("e"))) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`"
|
|
209
|
+
);
|
|
210
|
+
}
|
|
209
211
|
}
|
|
210
212
|
await Promise.all([
|
|
211
213
|
// We use a custom channel for insert/delete For the following reason:
|
|
@@ -441,7 +443,7 @@ function getTimeoutRedisCommandOptions(timeoutMs) {
|
|
|
441
443
|
}
|
|
442
444
|
var RedisStringsHandler = class {
|
|
443
445
|
constructor({
|
|
444
|
-
|
|
446
|
+
redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379",
|
|
445
447
|
database = process.env.VERCEL_ENV === "production" ? 0 : 1,
|
|
446
448
|
keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
|
|
447
449
|
sharedTagsKey = "__sharedTags__",
|
|
@@ -451,9 +453,7 @@ var RedisStringsHandler = class {
|
|
|
451
453
|
redisGetDeduplication = true,
|
|
452
454
|
inMemoryCachingTime = 1e4,
|
|
453
455
|
defaultStaleAge = 60 * 60 * 24 * 14,
|
|
454
|
-
estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === "production" ? staleAge * 2 : staleAge * 1.2
|
|
455
|
-
socketOptions,
|
|
456
|
-
clientOptions
|
|
456
|
+
estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === "production" ? staleAge * 2 : staleAge * 1.2
|
|
457
457
|
}) {
|
|
458
458
|
this.keyPrefix = keyPrefix;
|
|
459
459
|
this.timeoutMs = timeoutMs;
|
|
@@ -464,9 +464,7 @@ var RedisStringsHandler = class {
|
|
|
464
464
|
try {
|
|
465
465
|
this.client = (0, import_redis.createClient)({
|
|
466
466
|
...database !== 0 ? { database } : {},
|
|
467
|
-
url:
|
|
468
|
-
...socketOptions ? { socket: socketOptions } : {},
|
|
469
|
-
...clientOptions || {}
|
|
467
|
+
url: redisUrl
|
|
470
468
|
});
|
|
471
469
|
this.client.on("error", (error) => {
|
|
472
470
|
console.error("Redis client error", error);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts"],"sourcesContent":["import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n","import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redis_url to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redis_url?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n\n constructor({\n redis_url = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n ...(database !== 0 ? { database } : {}),\n url: redis_url,\n ...(socketOptions ? { socket: socketOptions } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error('Redis client error', error);\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n }\n\n resetRequestCache(): void {}\n\n private async assertClientIsReady(): Promise<void> {\n await Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]);\n if (!this.client.isReady) {\n throw new Error('Redis client is not ready yet or connection is lost.');\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n await this.subscriberClient.connect();\n });\n\n // Check if keyspace event configuration is set correctly\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n \"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') && keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n \"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n // /api/revalidated-fetch\n // true\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAiE;;;ACA1D,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AACA,MAAI,QAAQ,IAAI,qBAAqB;AACnC,YAAQ,IAAI,UAAU,KAAK,GAAG,yBAAyB,GAAG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eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ANrCA,IAAO,gBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts"],"sourcesContent":["import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n","import { commandOptions, createClient } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n }: CreateRedisStringsHandlerOptions) {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n\n try {\n this.client = createClient({\n ...(database !== 0 ? { database } : {}),\n url: redisUrl,\n });\n\n this.client.on('error', (error) => {\n console.error('Redis client error', error);\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n }\n\n resetRequestCache(): void {}\n\n private async assertClientIsReady(): Promise<void> {\n await Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]);\n if (!this.client.isReady) {\n throw new Error('Redis client is not ready yet or connection is lost.');\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n await this.subscriberClient.connect();\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n \"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n \"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n // /api/revalidated-fetch\n // true\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA6C;;;ACAtC,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AACA,MAAI,QAAQ,IAAI,qBAAqB;AACnC,YAAQ,IAAI,UAAU,KAAK,GAAG,yBAAyB,GAAG,IAAI;AAAA,EAChE;AACF;AAEO,SAAS,aAAa,UAAkB,MAAiB;AAC9D,MAAI,QAAQ,IAAI,qCAAqC;AACnD,YAAQ,IAAI,YAAY,sBAAsB,GAAG,IAAI;AAAA,EACvD;AACF;;;ACDA,IAAM,sBAAsB;AACrB,IAAM,YAAN,MAAmB;AAAA,EAiBxB,YAAY,SAA2B;AACrC,SAAK,SAAS,QAAQ;AACtB,SAAK,YAAY,QAAQ;AACzB,SAAK,WAAW,QAAQ;AACxB,SAAK,cAAc,GA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AI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ANrCA,IAAO,gBAAQ;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -169,16 +169,18 @@ var SyncedMap = class {
|
|
|
169
169
|
await this.subscriberClient.connect().catch(async () => {
|
|
170
170
|
await this.subscriberClient.connect();
|
|
171
171
|
});
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
172
|
+
if ((process.env.SKIP_KEYSPACE_CONFIG_CHECK || "").toUpperCase() !== "TRUE") {
|
|
173
|
+
const keyspaceEventConfig = (await this.subscriberClient.configGet("notify-keyspace-events"))?.["notify-keyspace-events"];
|
|
174
|
+
if (!keyspaceEventConfig.includes("E")) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`"
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
if (!keyspaceEventConfig.includes("A") && !(keyspaceEventConfig.includes("x") && keyspaceEventConfig.includes("e"))) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`"
|
|
182
|
+
);
|
|
183
|
+
}
|
|
182
184
|
}
|
|
183
185
|
await Promise.all([
|
|
184
186
|
// We use a custom channel for insert/delete For the following reason:
|
|
@@ -414,7 +416,7 @@ function getTimeoutRedisCommandOptions(timeoutMs) {
|
|
|
414
416
|
}
|
|
415
417
|
var RedisStringsHandler = class {
|
|
416
418
|
constructor({
|
|
417
|
-
|
|
419
|
+
redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379",
|
|
418
420
|
database = process.env.VERCEL_ENV === "production" ? 0 : 1,
|
|
419
421
|
keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
|
|
420
422
|
sharedTagsKey = "__sharedTags__",
|
|
@@ -424,9 +426,7 @@ var RedisStringsHandler = class {
|
|
|
424
426
|
redisGetDeduplication = true,
|
|
425
427
|
inMemoryCachingTime = 1e4,
|
|
426
428
|
defaultStaleAge = 60 * 60 * 24 * 14,
|
|
427
|
-
estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === "production" ? staleAge * 2 : staleAge * 1.2
|
|
428
|
-
socketOptions,
|
|
429
|
-
clientOptions
|
|
429
|
+
estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === "production" ? staleAge * 2 : staleAge * 1.2
|
|
430
430
|
}) {
|
|
431
431
|
this.keyPrefix = keyPrefix;
|
|
432
432
|
this.timeoutMs = timeoutMs;
|
|
@@ -437,9 +437,7 @@ var RedisStringsHandler = class {
|
|
|
437
437
|
try {
|
|
438
438
|
this.client = createClient({
|
|
439
439
|
...database !== 0 ? { database } : {},
|
|
440
|
-
url:
|
|
441
|
-
...socketOptions ? { socket: socketOptions } : {},
|
|
442
|
-
...clientOptions || {}
|
|
440
|
+
url: redisUrl
|
|
443
441
|
});
|
|
444
442
|
this.client.on("error", (error) => {
|
|
445
443
|
console.error("Redis client error", error);
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts","../src/index.ts"],"sourcesContent":["import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redis_url to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redis_url?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n\n constructor({\n redis_url = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n ...(database !== 0 ? { database } : {}),\n url: redis_url,\n ...(socketOptions ? { socket: socketOptions } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error('Redis client error', error);\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n }\n\n resetRequestCache(): void {}\n\n private async assertClientIsReady(): Promise<void> {\n await Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]);\n if (!this.client.isReady) {\n throw new Error('Redis client is not ready yet or connection is lost.');\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n await this.subscriberClient.connect();\n });\n\n // Check if keyspace event configuration is set correctly\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n \"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') && keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n \"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n // /api/revalidated-fetch\n // true\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n","import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n"],"mappings":";AAAA,SAAS,gBAAgB,oB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AG;AAAA,MAC5C;AAAA,IACF;AAGA,UAAM,sBAAsB,KAAK,cAAc,OAAO,SAAS;AAG/D,UAAM,QAAQ,IAAI,CAAC,qBAAqB,mBAAmB,CAAC;AAC5D;AAAA,MACE;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;;;AKplBA,IAAI;AAEJ,IAAqB,gBAArB,MAAmC;AAAA,EACjC,YAAY,SAA2C;AACrD,QAAI,CAAC,eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ACrCA,IAAO,gBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts","../src/index.ts"],"sourcesContent":["import { commandOptions, createClient } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n }: CreateRedisStringsHandlerOptions) {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n\n try {\n this.client = createClient({\n ...(database !== 0 ? { database } : {}),\n url: redisUrl,\n });\n\n this.client.on('error', (error) => {\n console.error('Redis client error', error);\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n }\n\n resetRequestCache(): void {}\n\n private async assertClientIsReady(): Promise<void> {\n await Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]);\n if (!this.client.isReady) {\n throw new Error('Redis client is not ready yet or connection is lost.');\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n await this.subscriberClient.connect();\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n \"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n \"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n // /api/revalidated-fetch\n // true\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n","import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n"],"mappings":";AAAA,SAAS,gBAAgB,oBAAoB;;;ACAtC,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AACA,MAAI,QAAQ,IAAI,qBAAqB;AACnC,YAAQ,IAAI,UAAU,KAAK,GAAG,yBAAyB,GAAG,IAAI;AAAA,EAChE;AACF;AAEO,SAAS,aAAa,UAAkB,MAAiB;AAC9D,MAAI,QAAQ,IAAI,qCAAqC;AACnD,YAAQ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O,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ACrCA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trieb.work/nextjs-turbo-redis-cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git"
|
|
7
7
|
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "pnpm test",
|
|
10
|
+
"run-dev-server": "pnpm run-dev-server-15-3-2",
|
|
11
|
+
"run-dev-server-15-3-2": "cd ./test/integration/next-app-15-3-2 && pnpm dev",
|
|
12
|
+
"run-dev-server-15-0-3": "cd ./test/integration/next-app-15-0-3 && pnpm dev",
|
|
13
|
+
"build": "tsup",
|
|
14
|
+
"lint": "eslint -c eslint.config.mjs --fix",
|
|
15
|
+
"format": "prettier --write 'src/**/*.ts' 'src/*.ts'",
|
|
16
|
+
"test": "vitest --coverage --config vite.config.ts",
|
|
17
|
+
"test:ui": "vitest --ui --config vite.config.ts",
|
|
18
|
+
"test:unit": "vitest --config vite.config.ts src/**/*.test.ts src/**/*.test.tsx",
|
|
19
|
+
"test:integration": "vitest --config vite.config.ts ./test/integration/nextjs-cache-handler.integration.test.ts",
|
|
20
|
+
"prepare": "./scripts/prepare.sh"
|
|
21
|
+
},
|
|
8
22
|
"main": "dist/index.js",
|
|
9
23
|
"module": "dist/index.mjs",
|
|
10
24
|
"types": "dist/index.d.ts",
|
|
@@ -37,7 +51,7 @@
|
|
|
37
51
|
],
|
|
38
52
|
"author": "Designed for speed, scalability, and optimized performance, nextjs-turbo-redis-cache is your go-to solution for Next.js caching in demanding production environments.",
|
|
39
53
|
"license": "ISC",
|
|
40
|
-
"description": "",
|
|
54
|
+
"description": "Next.js redis cache handler",
|
|
41
55
|
"publishConfig": {
|
|
42
56
|
"access": "public"
|
|
43
57
|
},
|
|
@@ -75,17 +89,5 @@
|
|
|
75
89
|
"next": ">=15.0.3 <= 15.3.2",
|
|
76
90
|
"redis": "4.7.0"
|
|
77
91
|
},
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
"run-dev-server": "pnpm run-dev-server-15-3-2",
|
|
81
|
-
"run-dev-server-15-3-2": "cd ./test/integration/next-app-15-3-2 && pnpm dev",
|
|
82
|
-
"run-dev-server-15-0-3": "cd ./test/integration/next-app-15-0-3 && pnpm dev",
|
|
83
|
-
"build": "tsup",
|
|
84
|
-
"lint": "eslint -c eslint.config.mjs --fix",
|
|
85
|
-
"format": "prettier --write 'src/**/*.ts' 'src/*.ts'",
|
|
86
|
-
"test": "vitest --coverage --config vite.config.ts",
|
|
87
|
-
"test:ui": "vitest --ui --config vite.config.ts",
|
|
88
|
-
"test:unit": "vitest --config vite.config.ts src/**/*.test.ts src/**/*.test.tsx",
|
|
89
|
-
"test:integration": "vitest --config vite.config.ts ./test/integration/nextjs-cache-handler.integration.test.ts"
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
+
"packageManager": "pnpm@9.15.9+sha512.68046141893c66fad01c079231128e9afb89ef87e2691d69e4d40eee228988295fd4682181bae55b58418c3a253bde65a505ec7c5f9403ece5cc3cd37dcf2531"
|
|
93
|
+
}
|
package/scripts/prepare.sh
CHANGED
|
File without changes
|
package/scripts/setup-git.sh
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { commandOptions, createClient
|
|
1
|
+
import { commandOptions, createClient } from 'redis';
|
|
2
2
|
import { SyncedMap } from './SyncedMap';
|
|
3
3
|
import { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';
|
|
4
4
|
import { debug } from './utils/debug';
|
|
@@ -14,12 +14,12 @@ export type CacheEntry = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export type CreateRedisStringsHandlerOptions = {
|
|
17
|
-
/** Redis
|
|
17
|
+
/** Redis redisUrl to use.
|
|
18
18
|
* @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
|
|
19
19
|
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
|
|
20
20
|
: 'redis://localhost:6379'
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
redisUrl?: string;
|
|
23
23
|
/** Redis database number to use. Uses DB 0 for production, DB 1 otherwise
|
|
24
24
|
* @default process.env.VERCEL_ENV === 'production' ? 0 : 1
|
|
25
25
|
*/
|
|
@@ -60,14 +60,6 @@ export type CreateRedisStringsHandlerOptions = {
|
|
|
60
60
|
* @default Production: staleAge * 2, Other: staleAge * 1.2
|
|
61
61
|
*/
|
|
62
62
|
estimateExpireAge?: (staleAge: number) => number;
|
|
63
|
-
/** Additional Redis client socket options
|
|
64
|
-
* @example { tls: true, rejectUnauthorized: false }
|
|
65
|
-
*/
|
|
66
|
-
socketOptions?: RedisClientOptions['socket'];
|
|
67
|
-
/** Additional Redis client options to be passed directly to createClient
|
|
68
|
-
* @example { username: 'user', password: 'pass' }
|
|
69
|
-
*/
|
|
70
|
-
clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;
|
|
71
63
|
};
|
|
72
64
|
|
|
73
65
|
// Identifier prefix used by Next.js to mark automatically generated cache tags
|
|
@@ -105,7 +97,7 @@ export default class RedisStringsHandler {
|
|
|
105
97
|
private estimateExpireAge: (staleAge: number) => number;
|
|
106
98
|
|
|
107
99
|
constructor({
|
|
108
|
-
|
|
100
|
+
redisUrl = process.env.REDIS_URL
|
|
109
101
|
? process.env.REDIS_URL
|
|
110
102
|
: process.env.REDISHOST
|
|
111
103
|
? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`
|
|
@@ -121,8 +113,6 @@ export default class RedisStringsHandler {
|
|
|
121
113
|
defaultStaleAge = 60 * 60 * 24 * 14,
|
|
122
114
|
estimateExpireAge = (staleAge) =>
|
|
123
115
|
process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,
|
|
124
|
-
socketOptions,
|
|
125
|
-
clientOptions,
|
|
126
116
|
}: CreateRedisStringsHandlerOptions) {
|
|
127
117
|
this.keyPrefix = keyPrefix;
|
|
128
118
|
this.timeoutMs = timeoutMs;
|
|
@@ -132,12 +122,9 @@ export default class RedisStringsHandler {
|
|
|
132
122
|
this.estimateExpireAge = estimateExpireAge;
|
|
133
123
|
|
|
134
124
|
try {
|
|
135
|
-
// Create Redis client with properly typed configuration
|
|
136
125
|
this.client = createClient({
|
|
137
126
|
...(database !== 0 ? { database } : {}),
|
|
138
|
-
url:
|
|
139
|
-
...(socketOptions ? { socket: socketOptions } : {}),
|
|
140
|
-
...(clientOptions || {}),
|
|
127
|
+
url: redisUrl,
|
|
141
128
|
});
|
|
142
129
|
|
|
143
130
|
this.client.on('error', (error) => {
|
package/src/SyncedMap.ts
CHANGED
|
@@ -206,23 +206,28 @@ export class SyncedMap<V> {
|
|
|
206
206
|
});
|
|
207
207
|
|
|
208
208
|
// Check if keyspace event configuration is set correctly
|
|
209
|
-
const keyspaceEventConfig = (
|
|
210
|
-
await this.subscriberClient.configGet('notify-keyspace-events')
|
|
211
|
-
)?.['notify-keyspace-events'];
|
|
212
|
-
if (!keyspaceEventConfig.includes('E')) {
|
|
213
|
-
throw new Error(
|
|
214
|
-
"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`",
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
209
|
if (
|
|
218
|
-
|
|
219
|
-
!(
|
|
220
|
-
keyspaceEventConfig.includes('x') && keyspaceEventConfig.includes('e')
|
|
221
|
-
)
|
|
210
|
+
(process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'
|
|
222
211
|
) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
);
|
|
212
|
+
const keyspaceEventConfig = (
|
|
213
|
+
await this.subscriberClient.configGet('notify-keyspace-events')
|
|
214
|
+
)?.['notify-keyspace-events'];
|
|
215
|
+
if (!keyspaceEventConfig.includes('E')) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
"Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`",
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (
|
|
221
|
+
!keyspaceEventConfig.includes('A') &&
|
|
222
|
+
!(
|
|
223
|
+
keyspaceEventConfig.includes('x') &&
|
|
224
|
+
keyspaceEventConfig.includes('e')
|
|
225
|
+
)
|
|
226
|
+
) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
"Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`",
|
|
229
|
+
);
|
|
230
|
+
}
|
|
226
231
|
}
|
|
227
232
|
|
|
228
233
|
await Promise.all([
|