@trieb.work/nextjs-turbo-redis-cache 1.10.0 → 1.11.0-beta.1
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/.github/workflows/ci.yml +27 -11
- package/CHANGELOG.md +9 -0
- package/README.md +94 -0
- package/dist/index.d.mts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +318 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +315 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/playwright.config.ts +8 -1
- package/src/CacheComponentsHandler.ts +471 -0
- package/src/index.test.ts +1 -1
- package/src/index.ts +5 -0
- package/test/cache-components/cache-components.integration.spec.ts +188 -0
- package/test/integration/next-app-15-4-7/next.config.js +3 -0
- package/test/integration/next-app-15-4-7/pnpm-lock.yaml +1 -1
- package/test/integration/next-app-16-0-3/next.config.ts +3 -0
- package/test/integration/next-app-16-1-1-cache-components/README.md +36 -0
- package/test/integration/next-app-16-1-1-cache-components/cache-handler.js +3 -0
- package/test/integration/next-app-16-1-1-cache-components/eslint.config.mjs +18 -0
- package/test/integration/next-app-16-1-1-cache-components/next.config.ts +13 -0
- package/test/integration/next-app-16-1-1-cache-components/package.json +28 -0
- package/test/integration/next-app-16-1-1-cache-components/pnpm-lock.yaml +4128 -0
- package/test/integration/next-app-16-1-1-cache-components/postcss.config.mjs +7 -0
- package/test/integration/next-app-16-1-1-cache-components/public/file.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/globe.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/next.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/public/file.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/public/globe.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/public/next.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/public/vercel.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/public/window.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/vercel.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/public/window.svg +1 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-static-fetch/route.ts +19 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-with-tag/route.ts +21 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidate-tag/route.ts +19 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidated-fetch/route.ts +19 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/cachelife-short/page.tsx +110 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/page.tsx +90 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +127 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +130 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +127 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +110 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/favicon.ico +0 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/globals.css +26 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/layout.tsx +57 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/page.tsx +755 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/revalidation-interface.tsx +267 -0
- package/test/integration/next-app-16-1-1-cache-components/src/app/update-tag-test/page.tsx +22 -0
- package/test/integration/next-app-16-1-1-cache-components/tsconfig.json +34 -0
- package/tests/cache-lab.spec.ts +157 -0
- package/vitest.cache-components.config.ts +16 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -25,11 +25,15 @@ jobs:
|
|
|
25
25
|
pull-requests: write # Grant write access to pull request comments
|
|
26
26
|
strategy:
|
|
27
27
|
matrix:
|
|
28
|
-
|
|
29
|
-
- next-app-15-0-3
|
|
30
|
-
|
|
31
|
-
- next-app-15-
|
|
32
|
-
|
|
28
|
+
include:
|
|
29
|
+
- next-test-app: next-app-15-0-3
|
|
30
|
+
run-cache-components: false
|
|
31
|
+
- next-test-app: next-app-15-3-2
|
|
32
|
+
run-cache-components: false
|
|
33
|
+
- next-test-app: next-app-15-4-7
|
|
34
|
+
run-cache-components: false
|
|
35
|
+
- next-test-app: next-app-16-0-3
|
|
36
|
+
run-cache-components: true
|
|
33
37
|
|
|
34
38
|
|
|
35
39
|
steps:
|
|
@@ -78,18 +82,30 @@ jobs:
|
|
|
78
82
|
SKIP_BUILD: true
|
|
79
83
|
NEXT_TEST_APP: ${{ matrix.next-test-app }}
|
|
80
84
|
|
|
81
|
-
- name: Install
|
|
82
|
-
if: matrix.
|
|
85
|
+
- name: Install Cache Components Integration Test Project
|
|
86
|
+
if: matrix.run-cache-components
|
|
87
|
+
run: cd test/integration/next-app-16-1-1-cache-components && pnpm install
|
|
88
|
+
|
|
89
|
+
- name: Build Cache Components Integration Test Project
|
|
90
|
+
if: matrix.run-cache-components
|
|
91
|
+
run: cd test/integration/next-app-16-1-1-cache-components && pnpm build
|
|
92
|
+
|
|
93
|
+
- name: Run Cache Components integration tests
|
|
94
|
+
if: matrix.run-cache-components
|
|
95
|
+
run: pnpm test:cache-components
|
|
96
|
+
|
|
97
|
+
- name: Install Playwright browsers
|
|
98
|
+
if: matrix.run-cache-components
|
|
83
99
|
run: pnpm exec playwright install --with-deps
|
|
84
100
|
|
|
85
|
-
- name: Run Playwright E2E tests
|
|
86
|
-
if: matrix.
|
|
101
|
+
- name: Run Playwright E2E tests
|
|
102
|
+
if: matrix.run-cache-components
|
|
87
103
|
run: pnpm test:e2e
|
|
88
104
|
env:
|
|
89
|
-
PLAYWRIGHT_BASE_URL: http://localhost:
|
|
105
|
+
PLAYWRIGHT_BASE_URL: http://localhost:3101
|
|
90
106
|
|
|
91
107
|
- name: Code Coverage Comments
|
|
92
|
-
if: github.event_name == 'pull_request'
|
|
108
|
+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
|
|
93
109
|
uses: kcjpop/coverage-comments@v2.2
|
|
94
110
|
with:
|
|
95
111
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# [1.11.0-beta.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.10.0...v1.11.0-beta.1) (2025-12-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add Next.js 16.0.3 integration test environment and build artifacts for cache components ([f2b3088](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/f2b3088c5845865cb69e5ee691cd09ede5735119))
|
|
7
|
+
* add Next.js 16.0.3 integration test environment and build artifacts for cache components ([01fb22a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/01fb22a9ae0fbbb6fba44bb225ab46fdcc009a85))
|
|
8
|
+
* cache components lab + Next.js 16.1.x support ([be2e9b3](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/be2e9b3b7af441df112fc83483994877a4415443))
|
|
9
|
+
|
|
1
10
|
# [1.10.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.9.1...v1.10.0) (2025-12-27)
|
|
2
11
|
|
|
3
12
|
|
package/README.md
CHANGED
|
@@ -214,6 +214,12 @@ To run all tests you can use the following command:
|
|
|
214
214
|
pnpm build && pnpm test
|
|
215
215
|
```
|
|
216
216
|
|
|
217
|
+
Folder layout / runners:
|
|
218
|
+
|
|
219
|
+
- **Vitest** (unit + integration) lives in `src/**/*.test.ts(x)` and `test/**`.
|
|
220
|
+
- **Playwright** (E2E) lives in `tests/**` (see `playwright.config.ts`).
|
|
221
|
+
- `test/browser/**` contains Vitest tests that hit a running Next.js app over HTTP. Despite the folder name, this is not Playwright and does not use Vitest browser mode.
|
|
222
|
+
|
|
217
223
|
### Unit tests
|
|
218
224
|
|
|
219
225
|
To run unit tests you can use the following command:
|
|
@@ -230,6 +236,14 @@ To run integration tests you can use the following command:
|
|
|
230
236
|
pnpm build && pnpm test:integration
|
|
231
237
|
```
|
|
232
238
|
|
|
239
|
+
### E2E tests (Playwright)
|
|
240
|
+
|
|
241
|
+
To run Playwright tests (`tests/**`) you can use:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
pnpm test:e2e
|
|
245
|
+
```
|
|
246
|
+
|
|
233
247
|
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:
|
|
234
248
|
|
|
235
249
|
- SKIP_BUILD: If set to true, the integration tests will not build the Next.js app. Therefore the nextjs app needs to be built before running the tests. Or you execute the test once without skip build and the re-execute `pnpm test:integration` with skip build set to true.
|
|
@@ -238,6 +252,86 @@ The integration tests will start a Next.js server and test the caching handler.
|
|
|
238
252
|
|
|
239
253
|
Integration tests may have dependencies between test cases, so individual test failures should be evaluated in the context of the full test suite rather than in isolation.
|
|
240
254
|
|
|
255
|
+
## Cache Components handler (Next.js 16+)
|
|
256
|
+
|
|
257
|
+
This package can be used as a Cache Components handler (Node.js runtime) for Next.js apps that enable Cache Components.
|
|
258
|
+
|
|
259
|
+
### Enable Cache Components + cache handler
|
|
260
|
+
|
|
261
|
+
Install the package in your Next.js app:
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
pnpm add @trieb.work/nextjs-turbo-redis-cache redis
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
In your Next.js app, enable Cache Components and point `cacheHandlers.default` to a module that exports the handler instance:
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
// next.config.ts
|
|
271
|
+
import type { NextConfig } from 'next';
|
|
272
|
+
|
|
273
|
+
const nextConfig: NextConfig = {
|
|
274
|
+
cacheComponents: true,
|
|
275
|
+
cacheHandlers: {
|
|
276
|
+
default: require.resolve('./cache-handler.js'),
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
export default nextConfig;
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
```js
|
|
284
|
+
// cache-handler.js
|
|
285
|
+
const { redisCacheHandler } = require('@trieb.work/nextjs-turbo-redis-cache');
|
|
286
|
+
|
|
287
|
+
module.exports = redisCacheHandler;
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
If you prefer ESM:
|
|
291
|
+
|
|
292
|
+
```js
|
|
293
|
+
// cache-handler.js
|
|
294
|
+
import { redisCacheHandler } from '@trieb.work/nextjs-turbo-redis-cache';
|
|
295
|
+
|
|
296
|
+
export default redisCacheHandler;
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Required environment variables
|
|
300
|
+
|
|
301
|
+
- `REDIS_URL` (recommended): e.g. `redis://localhost:6379`
|
|
302
|
+
|
|
303
|
+
Optional:
|
|
304
|
+
|
|
305
|
+
- `VERCEL_URL`: used as a key prefix for multi-tenant isolation (also useful in tests). If unset, a default prefix is used.
|
|
306
|
+
- `REDIS_COMMAND_TIMEOUT_MS`: timeout (ms) for Redis commands used by the handler.
|
|
307
|
+
|
|
308
|
+
### Local manual testing (Cache Lab)
|
|
309
|
+
|
|
310
|
+
This repo includes a dedicated Next.js Cache Components integration app with real pages for manual testing.
|
|
311
|
+
|
|
312
|
+
1. Start Redis locally.
|
|
313
|
+
1. Install + start the Cache Components test app:
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
pnpm -C test/integration/next-app-16-1-1-cache-components install
|
|
317
|
+
pnpm -C test/integration/next-app-16-1-1-cache-components dev
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
Then open the Cache Lab pages:
|
|
321
|
+
|
|
322
|
+
- `/cache-lab`
|
|
323
|
+
- `/cache-lab/use-cache-nondeterministic`
|
|
324
|
+
- `/cache-lab/cachelife-short`
|
|
325
|
+
- `/cache-lab/tag-invalidation`
|
|
326
|
+
- `/cache-lab/stale-while-revalidate`
|
|
327
|
+
- `/cache-lab/runtime-data-suspense`
|
|
328
|
+
|
|
329
|
+
To run the Playwright E2E tests against a running dev server:
|
|
330
|
+
|
|
331
|
+
```bash
|
|
332
|
+
PLAYWRIGHT_BASE_URL=http://localhost:3101 pnpm test:e2e
|
|
333
|
+
```
|
|
334
|
+
|
|
241
335
|
## Some words on nextjs caching internals
|
|
242
336
|
|
|
243
337
|
Nextjs will use different caching objects for different pages and api routes. Currently supported are kind: APP_ROUTE and APP_PAGE.
|
package/dist/index.d.mts
CHANGED
|
@@ -149,4 +149,25 @@ declare class CachedHandler {
|
|
|
149
149
|
resetRequestCache(...args: Parameters<RedisStringsHandler['resetRequestCache']>): ReturnType<RedisStringsHandler['resetRequestCache']>;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
interface CacheComponentsEntry {
|
|
153
|
+
value: ReadableStream<Uint8Array>;
|
|
154
|
+
tags: string[];
|
|
155
|
+
stale: number;
|
|
156
|
+
timestamp: number;
|
|
157
|
+
expire: number;
|
|
158
|
+
revalidate: number;
|
|
159
|
+
}
|
|
160
|
+
interface CacheComponentsHandler {
|
|
161
|
+
get(cacheKey: string, softTags: string[]): Promise<CacheComponentsEntry | undefined>;
|
|
162
|
+
set(cacheKey: string, pendingEntry: Promise<CacheComponentsEntry>): Promise<void>;
|
|
163
|
+
refreshTags(): Promise<void>;
|
|
164
|
+
getExpiration(tags: string[]): Promise<number>;
|
|
165
|
+
updateTags(tags: string[], durations?: {
|
|
166
|
+
expire?: number;
|
|
167
|
+
}): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
type CreateCacheComponentsHandlerOptions = CreateRedisStringsHandlerOptions;
|
|
170
|
+
declare function getRedisCacheComponentsHandler(options?: CreateCacheComponentsHandlerOptions): CacheComponentsHandler;
|
|
171
|
+
declare const redisCacheHandler: CacheComponentsHandler;
|
|
172
|
+
|
|
173
|
+
export { RedisStringsHandler, CachedHandler as default, getRedisCacheComponentsHandler, redisCacheHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -149,4 +149,25 @@ declare class CachedHandler {
|
|
|
149
149
|
resetRequestCache(...args: Parameters<RedisStringsHandler['resetRequestCache']>): ReturnType<RedisStringsHandler['resetRequestCache']>;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
interface CacheComponentsEntry {
|
|
153
|
+
value: ReadableStream<Uint8Array>;
|
|
154
|
+
tags: string[];
|
|
155
|
+
stale: number;
|
|
156
|
+
timestamp: number;
|
|
157
|
+
expire: number;
|
|
158
|
+
revalidate: number;
|
|
159
|
+
}
|
|
160
|
+
interface CacheComponentsHandler {
|
|
161
|
+
get(cacheKey: string, softTags: string[]): Promise<CacheComponentsEntry | undefined>;
|
|
162
|
+
set(cacheKey: string, pendingEntry: Promise<CacheComponentsEntry>): Promise<void>;
|
|
163
|
+
refreshTags(): Promise<void>;
|
|
164
|
+
getExpiration(tags: string[]): Promise<number>;
|
|
165
|
+
updateTags(tags: string[], durations?: {
|
|
166
|
+
expire?: number;
|
|
167
|
+
}): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
type CreateCacheComponentsHandlerOptions = CreateRedisStringsHandlerOptions;
|
|
170
|
+
declare function getRedisCacheComponentsHandler(options?: CreateCacheComponentsHandlerOptions): CacheComponentsHandler;
|
|
171
|
+
declare const redisCacheHandler: CacheComponentsHandler;
|
|
172
|
+
|
|
173
|
+
export { RedisStringsHandler, CachedHandler as default, getRedisCacheComponentsHandler, redisCacheHandler };
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
RedisStringsHandler: () => RedisStringsHandler,
|
|
24
|
-
default: () => index_default
|
|
24
|
+
default: () => index_default,
|
|
25
|
+
getRedisCacheComponentsHandler: () => getRedisCacheComponentsHandler,
|
|
26
|
+
redisCacheHandler: () => redisCacheHandler
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(index_exports);
|
|
27
29
|
|
|
@@ -977,10 +979,324 @@ var CachedHandler = class {
|
|
|
977
979
|
}
|
|
978
980
|
};
|
|
979
981
|
|
|
982
|
+
// src/CacheComponentsHandler.ts
|
|
983
|
+
var import_redis2 = require("redis");
|
|
984
|
+
var REVALIDATED_TAGS_KEY2 = "__cacheComponents_revalidated_tags__";
|
|
985
|
+
var SHARED_TAGS_KEY = "__cacheComponents_sharedTags__";
|
|
986
|
+
var killContainerOnErrorCount2 = 0;
|
|
987
|
+
async function streamToBuffer(stream) {
|
|
988
|
+
const reader = stream.getReader();
|
|
989
|
+
const chunks = [];
|
|
990
|
+
while (true) {
|
|
991
|
+
const { value, done } = await reader.read();
|
|
992
|
+
if (done) break;
|
|
993
|
+
if (value) {
|
|
994
|
+
chunks.push(value);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
if (chunks.length === 1) {
|
|
998
|
+
return chunks[0];
|
|
999
|
+
}
|
|
1000
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
1001
|
+
const result = new Uint8Array(totalLength);
|
|
1002
|
+
let offset = 0;
|
|
1003
|
+
for (const chunk of chunks) {
|
|
1004
|
+
result.set(chunk, offset);
|
|
1005
|
+
offset += chunk.byteLength;
|
|
1006
|
+
}
|
|
1007
|
+
return result;
|
|
1008
|
+
}
|
|
1009
|
+
function bufferToReadableStream(buffer) {
|
|
1010
|
+
return new ReadableStream({
|
|
1011
|
+
start(controller) {
|
|
1012
|
+
controller.enqueue(buffer);
|
|
1013
|
+
controller.close();
|
|
1014
|
+
}
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
var RedisCacheComponentsHandler = class {
|
|
1018
|
+
constructor({
|
|
1019
|
+
redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379",
|
|
1020
|
+
database = process.env.VERCEL_ENV === "production" ? 0 : 1,
|
|
1021
|
+
keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
|
|
1022
|
+
getTimeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS ? Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500 : 500,
|
|
1023
|
+
revalidateTagQuerySize = 250,
|
|
1024
|
+
avgResyncIntervalMs = 60 * 60 * 1e3,
|
|
1025
|
+
socketOptions,
|
|
1026
|
+
clientOptions,
|
|
1027
|
+
killContainerOnErrorThreshold = process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD ? Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0
|
|
1028
|
+
}) {
|
|
1029
|
+
try {
|
|
1030
|
+
this.keyPrefix = keyPrefix;
|
|
1031
|
+
this.getTimeoutMs = getTimeoutMs;
|
|
1032
|
+
this.client = (0, import_redis2.createClient)({
|
|
1033
|
+
url: redisUrl,
|
|
1034
|
+
pingInterval: 1e4,
|
|
1035
|
+
...database !== 0 ? { database } : {},
|
|
1036
|
+
...socketOptions ? { socket: { ...socketOptions } } : {},
|
|
1037
|
+
...clientOptions || {}
|
|
1038
|
+
});
|
|
1039
|
+
this.client.on("error", (error) => {
|
|
1040
|
+
console.error(
|
|
1041
|
+
"RedisCacheComponentsHandler client error",
|
|
1042
|
+
error,
|
|
1043
|
+
killContainerOnErrorCount2++
|
|
1044
|
+
);
|
|
1045
|
+
setTimeout(
|
|
1046
|
+
() => this.client.connect().catch((err) => {
|
|
1047
|
+
console.error(
|
|
1048
|
+
"Failed to reconnect RedisCacheComponentsHandler client after connection loss:",
|
|
1049
|
+
err
|
|
1050
|
+
);
|
|
1051
|
+
}),
|
|
1052
|
+
1e3
|
|
1053
|
+
);
|
|
1054
|
+
if (killContainerOnErrorThreshold > 0 && killContainerOnErrorCount2 >= killContainerOnErrorThreshold) {
|
|
1055
|
+
console.error(
|
|
1056
|
+
"RedisCacheComponentsHandler client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)",
|
|
1057
|
+
error,
|
|
1058
|
+
killContainerOnErrorCount2++
|
|
1059
|
+
);
|
|
1060
|
+
this.client.disconnect();
|
|
1061
|
+
this.client.quit();
|
|
1062
|
+
setTimeout(() => {
|
|
1063
|
+
process.exit(1);
|
|
1064
|
+
}, 500);
|
|
1065
|
+
}
|
|
1066
|
+
});
|
|
1067
|
+
this.client.connect().then(() => {
|
|
1068
|
+
debug("green", "RedisCacheComponentsHandler client connected.");
|
|
1069
|
+
}).catch(() => {
|
|
1070
|
+
this.client.connect().catch((error) => {
|
|
1071
|
+
console.error(
|
|
1072
|
+
"Failed to connect RedisCacheComponentsHandler client:",
|
|
1073
|
+
error
|
|
1074
|
+
);
|
|
1075
|
+
this.client.disconnect();
|
|
1076
|
+
throw error;
|
|
1077
|
+
});
|
|
1078
|
+
});
|
|
1079
|
+
const filterKeys = (key) => key !== REVALIDATED_TAGS_KEY2 && key !== SHARED_TAGS_KEY;
|
|
1080
|
+
this.revalidatedTagsMap = new SyncedMap({
|
|
1081
|
+
client: this.client,
|
|
1082
|
+
keyPrefix,
|
|
1083
|
+
redisKey: REVALIDATED_TAGS_KEY2,
|
|
1084
|
+
database,
|
|
1085
|
+
querySize: revalidateTagQuerySize,
|
|
1086
|
+
filterKeys,
|
|
1087
|
+
resyncIntervalMs: avgResyncIntervalMs + avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
|
|
1088
|
+
});
|
|
1089
|
+
this.sharedTagsMap = new SyncedMap({
|
|
1090
|
+
client: this.client,
|
|
1091
|
+
keyPrefix,
|
|
1092
|
+
redisKey: SHARED_TAGS_KEY,
|
|
1093
|
+
database,
|
|
1094
|
+
querySize: revalidateTagQuerySize,
|
|
1095
|
+
filterKeys,
|
|
1096
|
+
resyncIntervalMs: avgResyncIntervalMs - avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
|
|
1097
|
+
});
|
|
1098
|
+
} catch (error) {
|
|
1099
|
+
console.error("RedisCacheComponentsHandler constructor error", error);
|
|
1100
|
+
throw error;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
async assertClientIsReady() {
|
|
1104
|
+
if (!this.client.isReady && !this.client.isOpen) {
|
|
1105
|
+
await this.client.connect().catch((error) => {
|
|
1106
|
+
console.error(
|
|
1107
|
+
"RedisCacheComponentsHandler assertClientIsReady reconnect error:",
|
|
1108
|
+
error
|
|
1109
|
+
);
|
|
1110
|
+
throw error;
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1113
|
+
await Promise.all([
|
|
1114
|
+
this.revalidatedTagsMap.waitUntilReady(),
|
|
1115
|
+
this.sharedTagsMap.waitUntilReady()
|
|
1116
|
+
]);
|
|
1117
|
+
}
|
|
1118
|
+
async computeMaxRevalidation(tags) {
|
|
1119
|
+
let max = 0;
|
|
1120
|
+
for (const tag of tags) {
|
|
1121
|
+
const ts = this.revalidatedTagsMap.get(tag);
|
|
1122
|
+
if (ts && ts > max) {
|
|
1123
|
+
max = ts;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
return max;
|
|
1127
|
+
}
|
|
1128
|
+
async get(cacheKey, softTags) {
|
|
1129
|
+
const redisKey = `${this.keyPrefix}${cacheKey}`;
|
|
1130
|
+
try {
|
|
1131
|
+
await this.assertClientIsReady();
|
|
1132
|
+
const serialized = await redisErrorHandler(
|
|
1133
|
+
"RedisCacheComponentsHandler.get(), operation: get " + this.getTimeoutMs + "ms " + redisKey,
|
|
1134
|
+
this.client.get(
|
|
1135
|
+
(0, import_redis2.commandOptions)({ signal: AbortSignal.timeout(this.getTimeoutMs) }),
|
|
1136
|
+
redisKey
|
|
1137
|
+
)
|
|
1138
|
+
);
|
|
1139
|
+
if (!serialized) {
|
|
1140
|
+
return void 0;
|
|
1141
|
+
}
|
|
1142
|
+
const stored = JSON.parse(serialized);
|
|
1143
|
+
const now = Date.now();
|
|
1144
|
+
const expiryTime = stored.timestamp + stored.expire * 1e3;
|
|
1145
|
+
if (Number.isFinite(stored.expire) && stored.expire > 0 && now > expiryTime) {
|
|
1146
|
+
await this.client.unlink(redisKey).catch(() => {
|
|
1147
|
+
});
|
|
1148
|
+
await this.sharedTagsMap.delete(cacheKey).catch(() => {
|
|
1149
|
+
});
|
|
1150
|
+
return void 0;
|
|
1151
|
+
}
|
|
1152
|
+
const maxRevalidation = await this.computeMaxRevalidation([
|
|
1153
|
+
...stored.tags || [],
|
|
1154
|
+
...softTags || []
|
|
1155
|
+
]);
|
|
1156
|
+
if (maxRevalidation > 0 && maxRevalidation > stored.timestamp) {
|
|
1157
|
+
await this.client.unlink(redisKey).catch(() => {
|
|
1158
|
+
});
|
|
1159
|
+
await this.sharedTagsMap.delete(cacheKey).catch(() => {
|
|
1160
|
+
});
|
|
1161
|
+
return void 0;
|
|
1162
|
+
}
|
|
1163
|
+
const valueBuffer = typeof stored.value === "string" ? new Uint8Array(Buffer.from(stored.value, "base64")) : stored.value;
|
|
1164
|
+
return {
|
|
1165
|
+
...stored,
|
|
1166
|
+
value: bufferToReadableStream(valueBuffer)
|
|
1167
|
+
};
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
console.error(
|
|
1170
|
+
"RedisCacheComponentsHandler.get() Error occurred while getting cache entry. Returning undefined so site can continue to serve content while cache is disabled. The original error was:",
|
|
1171
|
+
error,
|
|
1172
|
+
killContainerOnErrorCount2++
|
|
1173
|
+
);
|
|
1174
|
+
return void 0;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
async set(cacheKey, pendingEntry) {
|
|
1178
|
+
try {
|
|
1179
|
+
await this.assertClientIsReady();
|
|
1180
|
+
const entry = await pendingEntry;
|
|
1181
|
+
const [storeStream] = entry.value.tee();
|
|
1182
|
+
const buffer = await streamToBuffer(storeStream);
|
|
1183
|
+
const stored = {
|
|
1184
|
+
value: Buffer.from(buffer).toString("base64"),
|
|
1185
|
+
tags: entry.tags || [],
|
|
1186
|
+
stale: entry.stale,
|
|
1187
|
+
timestamp: entry.timestamp,
|
|
1188
|
+
expire: entry.expire,
|
|
1189
|
+
revalidate: entry.revalidate
|
|
1190
|
+
};
|
|
1191
|
+
let serialized;
|
|
1192
|
+
try {
|
|
1193
|
+
const cleanStored = {
|
|
1194
|
+
value: stored.value,
|
|
1195
|
+
tags: Array.isArray(stored.tags) ? [...stored.tags] : [],
|
|
1196
|
+
stale: Number(stored.stale),
|
|
1197
|
+
timestamp: Number(stored.timestamp),
|
|
1198
|
+
expire: Number(stored.expire),
|
|
1199
|
+
revalidate: Number(stored.revalidate)
|
|
1200
|
+
};
|
|
1201
|
+
serialized = JSON.stringify(cleanStored);
|
|
1202
|
+
} catch (jsonError) {
|
|
1203
|
+
console.error("JSON.stringify error:", jsonError);
|
|
1204
|
+
console.error("Stored object:", stored);
|
|
1205
|
+
throw jsonError;
|
|
1206
|
+
}
|
|
1207
|
+
const ttlSeconds = Number.isFinite(stored.expire) && stored.expire > 0 ? Math.floor(stored.expire) : void 0;
|
|
1208
|
+
const redisKey = `${this.keyPrefix}${cacheKey}`;
|
|
1209
|
+
const setOperation = redisErrorHandler(
|
|
1210
|
+
"RedisCacheComponentsHandler.set(), operation: set " + redisKey,
|
|
1211
|
+
this.client.set(redisKey, serialized, {
|
|
1212
|
+
...ttlSeconds ? { EX: ttlSeconds } : {}
|
|
1213
|
+
})
|
|
1214
|
+
);
|
|
1215
|
+
let tagsOperation;
|
|
1216
|
+
const tags = stored.tags || [];
|
|
1217
|
+
if (tags.length > 0) {
|
|
1218
|
+
const currentTags = this.sharedTagsMap.get(cacheKey);
|
|
1219
|
+
const currentIsSameAsNew = currentTags?.length === tags.length && currentTags.every((v) => tags.includes(v)) && tags.every((v) => currentTags.includes(v));
|
|
1220
|
+
if (!currentIsSameAsNew) {
|
|
1221
|
+
tagsOperation = this.sharedTagsMap.set(cacheKey, [...tags]);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
await Promise.all([setOperation, tagsOperation]);
|
|
1225
|
+
} catch (error) {
|
|
1226
|
+
console.error(
|
|
1227
|
+
"RedisCacheComponentsHandler.set() Error occurred while setting cache entry. The original error was:",
|
|
1228
|
+
error,
|
|
1229
|
+
killContainerOnErrorCount2++
|
|
1230
|
+
);
|
|
1231
|
+
throw error;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
async refreshTags() {
|
|
1235
|
+
await this.assertClientIsReady();
|
|
1236
|
+
}
|
|
1237
|
+
async getExpiration(tags) {
|
|
1238
|
+
try {
|
|
1239
|
+
await this.assertClientIsReady();
|
|
1240
|
+
return this.computeMaxRevalidation(tags || []);
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
console.error(
|
|
1243
|
+
"RedisCacheComponentsHandler.getExpiration() Error occurred while getting expiration for tags. The original error was:",
|
|
1244
|
+
error
|
|
1245
|
+
);
|
|
1246
|
+
return 0;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
async updateTags(tags, _durations) {
|
|
1250
|
+
try {
|
|
1251
|
+
void _durations;
|
|
1252
|
+
await this.assertClientIsReady();
|
|
1253
|
+
const now = Date.now();
|
|
1254
|
+
const tagsSet = new Set(tags || []);
|
|
1255
|
+
for (const tag of tagsSet) {
|
|
1256
|
+
await this.revalidatedTagsMap.set(tag, now);
|
|
1257
|
+
}
|
|
1258
|
+
const keysToDelete = /* @__PURE__ */ new Set();
|
|
1259
|
+
for (const [key, storedTags] of this.sharedTagsMap.entries()) {
|
|
1260
|
+
if (storedTags.some((tag) => tagsSet.has(tag))) {
|
|
1261
|
+
keysToDelete.add(key);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
if (keysToDelete.size === 0) {
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
const cacheKeys = Array.from(keysToDelete);
|
|
1268
|
+
const fullRedisKeys = cacheKeys.map((key) => `${this.keyPrefix}${key}`);
|
|
1269
|
+
await redisErrorHandler(
|
|
1270
|
+
"RedisCacheComponentsHandler.updateTags(), operation: unlink",
|
|
1271
|
+
this.client.unlink(fullRedisKeys)
|
|
1272
|
+
);
|
|
1273
|
+
const deleteTagsOperation = this.sharedTagsMap.delete(cacheKeys);
|
|
1274
|
+
await deleteTagsOperation;
|
|
1275
|
+
} catch (error) {
|
|
1276
|
+
console.error(
|
|
1277
|
+
"RedisCacheComponentsHandler.updateTags() Error occurred while updating tags. The original error was:",
|
|
1278
|
+
error,
|
|
1279
|
+
killContainerOnErrorCount2++
|
|
1280
|
+
);
|
|
1281
|
+
throw error;
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
};
|
|
1285
|
+
var singletonHandler;
|
|
1286
|
+
function getRedisCacheComponentsHandler(options = {}) {
|
|
1287
|
+
if (!singletonHandler) {
|
|
1288
|
+
singletonHandler = new RedisCacheComponentsHandler(options);
|
|
1289
|
+
}
|
|
1290
|
+
return singletonHandler;
|
|
1291
|
+
}
|
|
1292
|
+
var redisCacheHandler = getRedisCacheComponentsHandler();
|
|
1293
|
+
|
|
980
1294
|
// src/index.ts
|
|
981
1295
|
var index_default = CachedHandler;
|
|
982
1296
|
// Annotate the CommonJS export names for ESM import in node:
|
|
983
1297
|
0 && (module.exports = {
|
|
984
|
-
RedisStringsHandler
|
|
1298
|
+
RedisStringsHandler,
|
|
1299
|
+
getRedisCacheComponentsHandler,
|
|
1300
|
+
redisCacheHandler
|
|
985
1301
|
});
|
|
986
1302
|
//# sourceMappingURL=index.js.map
|