@trieb.work/nextjs-turbo-redis-cache 1.1.3 → 1.2.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/release.yml +12 -0
- package/CHANGELOG.md +15 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +532 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +509 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +12 -9
- package/scripts/vitest-run-staged.cjs +1 -1
- package/tsup.config.ts +14 -0
|
@@ -5,6 +5,15 @@ on:
|
|
|
5
5
|
branches:
|
|
6
6
|
- main
|
|
7
7
|
- beta
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: write
|
|
11
|
+
pull-requests: write
|
|
12
|
+
deployments: write
|
|
13
|
+
packages: write
|
|
14
|
+
statuses: write
|
|
15
|
+
issues: write
|
|
16
|
+
actions: write
|
|
8
17
|
|
|
9
18
|
jobs:
|
|
10
19
|
release:
|
|
@@ -23,6 +32,9 @@ jobs:
|
|
|
23
32
|
- name: Install dependencies
|
|
24
33
|
run: npm ci
|
|
25
34
|
|
|
35
|
+
- name: Build project
|
|
36
|
+
run: npm run build
|
|
37
|
+
|
|
26
38
|
- name: Run Semantic Release
|
|
27
39
|
env:
|
|
28
40
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub token for Semantic Release
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## [1.2.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.2.0...v1.2.1) (2025-03-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* imports ([41f0cf9](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/41f0cf97506c134f8dfb37fab746cc9c066c515f))
|
|
7
|
+
|
|
8
|
+
# [1.2.0](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.1.3...v1.2.0) (2025-03-28)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add tsup ([ccf122a](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/ccf122a243fade016b6b2d544acec4098222becd))
|
|
14
|
+
* add tsup ([2dffad6](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/2dffad68401bc273cf81a0a0d06446d34b574a5e))
|
|
15
|
+
|
|
1
16
|
## [1.1.3](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.1.2...v1.1.3) (2025-03-28)
|
|
2
17
|
|
|
3
18
|
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CacheHandler, IncrementalCache, CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
|
|
2
|
+
|
|
3
|
+
type GetParams = Parameters<IncrementalCache['get']>;
|
|
4
|
+
type SetParams = Parameters<IncrementalCache['set']>;
|
|
5
|
+
type RevalidateParams = Parameters<IncrementalCache['revalidateTag']>;
|
|
6
|
+
type CreateRedisStringsHandlerOptions = {
|
|
7
|
+
database?: number;
|
|
8
|
+
keyPrefix?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
revalidateTagQuerySize?: number;
|
|
11
|
+
sharedTagsKey?: string;
|
|
12
|
+
avgResyncIntervalMs?: number;
|
|
13
|
+
redisGetDeduplication?: boolean;
|
|
14
|
+
inMemoryCachingTime?: number;
|
|
15
|
+
defaultStaleAge?: number;
|
|
16
|
+
estimateExpireAge?: (staleAge: number) => number;
|
|
17
|
+
};
|
|
18
|
+
declare class RedisStringsHandler implements CacheHandler {
|
|
19
|
+
private client;
|
|
20
|
+
private sharedTagsMap;
|
|
21
|
+
private revalidatedTagsMap;
|
|
22
|
+
private inMemoryDeduplicationCache;
|
|
23
|
+
private redisGet;
|
|
24
|
+
private redisDeduplicationHandler;
|
|
25
|
+
private deduplicatedRedisGet;
|
|
26
|
+
private timeoutMs;
|
|
27
|
+
private keyPrefix;
|
|
28
|
+
private redisGetDeduplication;
|
|
29
|
+
private inMemoryCachingTime;
|
|
30
|
+
private defaultStaleAge;
|
|
31
|
+
private estimateExpireAge;
|
|
32
|
+
constructor({ database, keyPrefix, sharedTagsKey, timeoutMs, revalidateTagQuerySize, avgResyncIntervalMs, redisGetDeduplication, inMemoryCachingTime, defaultStaleAge, estimateExpireAge, }: CreateRedisStringsHandlerOptions);
|
|
33
|
+
resetRequestCache(...args: never[]): void;
|
|
34
|
+
private assertClientIsReady;
|
|
35
|
+
get(key: GetParams[0], ctx: GetParams[1]): Promise<(CacheHandlerValue & {
|
|
36
|
+
lastModified: number;
|
|
37
|
+
}) | null>;
|
|
38
|
+
set(key: SetParams[0], data: SetParams[1] & {
|
|
39
|
+
lastModified: number;
|
|
40
|
+
}, ctx: SetParams[2]): Promise<void>;
|
|
41
|
+
revalidateTag(tagOrTags: RevalidateParams[0]): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare class CachedHandler implements CacheHandler {
|
|
45
|
+
constructor(options: CreateRedisStringsHandlerOptions);
|
|
46
|
+
get(...args: Parameters<RedisStringsHandler["get"]>): ReturnType<RedisStringsHandler["get"]>;
|
|
47
|
+
set(...args: Parameters<RedisStringsHandler["set"]>): ReturnType<RedisStringsHandler["set"]>;
|
|
48
|
+
revalidateTag(...args: Parameters<RedisStringsHandler["revalidateTag"]>): ReturnType<RedisStringsHandler["revalidateTag"]>;
|
|
49
|
+
resetRequestCache(...args: Parameters<RedisStringsHandler["resetRequestCache"]>): ReturnType<RedisStringsHandler["resetRequestCache"]>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { CachedHandler as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CacheHandler, IncrementalCache, CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
|
|
2
|
+
|
|
3
|
+
type GetParams = Parameters<IncrementalCache['get']>;
|
|
4
|
+
type SetParams = Parameters<IncrementalCache['set']>;
|
|
5
|
+
type RevalidateParams = Parameters<IncrementalCache['revalidateTag']>;
|
|
6
|
+
type CreateRedisStringsHandlerOptions = {
|
|
7
|
+
database?: number;
|
|
8
|
+
keyPrefix?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
revalidateTagQuerySize?: number;
|
|
11
|
+
sharedTagsKey?: string;
|
|
12
|
+
avgResyncIntervalMs?: number;
|
|
13
|
+
redisGetDeduplication?: boolean;
|
|
14
|
+
inMemoryCachingTime?: number;
|
|
15
|
+
defaultStaleAge?: number;
|
|
16
|
+
estimateExpireAge?: (staleAge: number) => number;
|
|
17
|
+
};
|
|
18
|
+
declare class RedisStringsHandler implements CacheHandler {
|
|
19
|
+
private client;
|
|
20
|
+
private sharedTagsMap;
|
|
21
|
+
private revalidatedTagsMap;
|
|
22
|
+
private inMemoryDeduplicationCache;
|
|
23
|
+
private redisGet;
|
|
24
|
+
private redisDeduplicationHandler;
|
|
25
|
+
private deduplicatedRedisGet;
|
|
26
|
+
private timeoutMs;
|
|
27
|
+
private keyPrefix;
|
|
28
|
+
private redisGetDeduplication;
|
|
29
|
+
private inMemoryCachingTime;
|
|
30
|
+
private defaultStaleAge;
|
|
31
|
+
private estimateExpireAge;
|
|
32
|
+
constructor({ database, keyPrefix, sharedTagsKey, timeoutMs, revalidateTagQuerySize, avgResyncIntervalMs, redisGetDeduplication, inMemoryCachingTime, defaultStaleAge, estimateExpireAge, }: CreateRedisStringsHandlerOptions);
|
|
33
|
+
resetRequestCache(...args: never[]): void;
|
|
34
|
+
private assertClientIsReady;
|
|
35
|
+
get(key: GetParams[0], ctx: GetParams[1]): Promise<(CacheHandlerValue & {
|
|
36
|
+
lastModified: number;
|
|
37
|
+
}) | null>;
|
|
38
|
+
set(key: SetParams[0], data: SetParams[1] & {
|
|
39
|
+
lastModified: number;
|
|
40
|
+
}, ctx: SetParams[2]): Promise<void>;
|
|
41
|
+
revalidateTag(tagOrTags: RevalidateParams[0]): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare class CachedHandler implements CacheHandler {
|
|
45
|
+
constructor(options: CreateRedisStringsHandlerOptions);
|
|
46
|
+
get(...args: Parameters<RedisStringsHandler["get"]>): ReturnType<RedisStringsHandler["get"]>;
|
|
47
|
+
set(...args: Parameters<RedisStringsHandler["set"]>): ReturnType<RedisStringsHandler["set"]>;
|
|
48
|
+
revalidateTag(...args: Parameters<RedisStringsHandler["revalidateTag"]>): ReturnType<RedisStringsHandler["revalidateTag"]>;
|
|
49
|
+
resetRequestCache(...args: Parameters<RedisStringsHandler["resetRequestCache"]>): ReturnType<RedisStringsHandler["resetRequestCache"]>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { CachedHandler as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/RedisStringsHandler.ts
|
|
28
|
+
var import_redis = require("redis");
|
|
29
|
+
|
|
30
|
+
// src/SyncedMap.ts
|
|
31
|
+
var SYNC_CHANNEL_SUFFIX = ":sync-channel:";
|
|
32
|
+
var SyncedMap = class {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
this.client = options.client;
|
|
35
|
+
this.keyPrefix = options.keyPrefix;
|
|
36
|
+
this.redisKey = options.redisKey;
|
|
37
|
+
this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;
|
|
38
|
+
this.database = options.database;
|
|
39
|
+
this.timeoutMs = options.timeoutMs;
|
|
40
|
+
this.querySize = options.querySize;
|
|
41
|
+
this.filterKeys = options.filterKeys;
|
|
42
|
+
this.resyncIntervalMs = options.resyncIntervalMs;
|
|
43
|
+
this.customizedSync = options.customizedSync;
|
|
44
|
+
this.map = /* @__PURE__ */ new Map();
|
|
45
|
+
this.subscriberClient = this.client.duplicate();
|
|
46
|
+
this.setupLock = new Promise((resolve) => {
|
|
47
|
+
this.setupLockResolve = resolve;
|
|
48
|
+
});
|
|
49
|
+
this.setup().catch((error) => {
|
|
50
|
+
console.error("Failed to setup SyncedMap:", error);
|
|
51
|
+
throw error;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async setup() {
|
|
55
|
+
let setupPromises = [];
|
|
56
|
+
if (!this.customizedSync?.withoutRedisHashmap) {
|
|
57
|
+
setupPromises.push(this.initialSync());
|
|
58
|
+
this.setupPeriodicResync();
|
|
59
|
+
}
|
|
60
|
+
setupPromises.push(this.setupPubSub());
|
|
61
|
+
await Promise.all(setupPromises);
|
|
62
|
+
this.setupLockResolve();
|
|
63
|
+
}
|
|
64
|
+
async initialSync() {
|
|
65
|
+
let cursor = 0;
|
|
66
|
+
const hScanOptions = { COUNT: this.querySize };
|
|
67
|
+
try {
|
|
68
|
+
do {
|
|
69
|
+
const remoteItems = await this.client.hScan(
|
|
70
|
+
getTimeoutRedisCommandOptions(this.timeoutMs),
|
|
71
|
+
this.keyPrefix + this.redisKey,
|
|
72
|
+
cursor,
|
|
73
|
+
hScanOptions
|
|
74
|
+
);
|
|
75
|
+
for (const { field, value } of remoteItems.tuples) {
|
|
76
|
+
if (this.filterKeys(field)) {
|
|
77
|
+
const parsedValue = JSON.parse(value);
|
|
78
|
+
this.map.set(field, parsedValue);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
cursor = remoteItems.cursor;
|
|
82
|
+
} while (cursor !== 0);
|
|
83
|
+
await this.cleanupKeysNotInRedis();
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error("Error during initial sync:", error);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async cleanupKeysNotInRedis() {
|
|
90
|
+
let cursor = 0;
|
|
91
|
+
const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };
|
|
92
|
+
let remoteKeys = [];
|
|
93
|
+
try {
|
|
94
|
+
do {
|
|
95
|
+
const remoteKeysPortion = await this.client.scan(
|
|
96
|
+
getTimeoutRedisCommandOptions(this.timeoutMs),
|
|
97
|
+
cursor,
|
|
98
|
+
scanOptions
|
|
99
|
+
);
|
|
100
|
+
remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);
|
|
101
|
+
cursor = remoteKeysPortion.cursor;
|
|
102
|
+
} while (cursor !== 0);
|
|
103
|
+
const remoteKeysSet = new Set(
|
|
104
|
+
remoteKeys.map((key) => key.substring(this.keyPrefix.length))
|
|
105
|
+
);
|
|
106
|
+
const keysToDelete = [];
|
|
107
|
+
for (const key of this.map.keys()) {
|
|
108
|
+
const keyStr = key;
|
|
109
|
+
if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {
|
|
110
|
+
keysToDelete.push(keyStr);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (keysToDelete.length > 0) {
|
|
114
|
+
await this.delete(keysToDelete);
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error("Error during cleanup of keys not in Redis:", error);
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
setupPeriodicResync() {
|
|
122
|
+
if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {
|
|
123
|
+
setInterval(() => {
|
|
124
|
+
this.initialSync().catch((error) => {
|
|
125
|
+
console.error("Error during periodic resync:", error);
|
|
126
|
+
});
|
|
127
|
+
}, this.resyncIntervalMs);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async setupPubSub() {
|
|
131
|
+
const syncHandler = async (message) => {
|
|
132
|
+
const syncMessage = JSON.parse(message);
|
|
133
|
+
if (syncMessage.type === "insert") {
|
|
134
|
+
if (syncMessage.key !== void 0 && syncMessage.value !== void 0) {
|
|
135
|
+
this.map.set(syncMessage.key, syncMessage.value);
|
|
136
|
+
}
|
|
137
|
+
} else if (syncMessage.type === "delete") {
|
|
138
|
+
if (syncMessage.keys) {
|
|
139
|
+
for (const key of syncMessage.keys) {
|
|
140
|
+
this.map.delete(key);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const keyEventHandler = async (_channel, message) => {
|
|
146
|
+
const key = message;
|
|
147
|
+
if (key.startsWith(this.keyPrefix)) {
|
|
148
|
+
const keyInMap = key.substring(this.keyPrefix.length);
|
|
149
|
+
if (this.filterKeys(keyInMap)) {
|
|
150
|
+
await this.delete(keyInMap, true);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
try {
|
|
155
|
+
await this.subscriberClient.connect();
|
|
156
|
+
await Promise.all([
|
|
157
|
+
// We use a custom channel for insert/delete For the following reason:
|
|
158
|
+
// With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we
|
|
159
|
+
// could get thousands of messages for one revalidateTag (For example revalidateTag("algolia") would send an enormous amount of network packages)
|
|
160
|
+
// Also we can send the value in the message for insert
|
|
161
|
+
this.subscriberClient.subscribe(this.syncChannel, syncHandler),
|
|
162
|
+
// Subscribe to Redis keyspace notifications for evicted and expired keys
|
|
163
|
+
this.subscriberClient.subscribe(
|
|
164
|
+
`__keyevent@${this.database}__:evicted`,
|
|
165
|
+
keyEventHandler
|
|
166
|
+
),
|
|
167
|
+
this.subscriberClient.subscribe(
|
|
168
|
+
`__keyevent@${this.database}__:expired`,
|
|
169
|
+
keyEventHandler
|
|
170
|
+
)
|
|
171
|
+
]);
|
|
172
|
+
this.subscriberClient.on("error", async (err) => {
|
|
173
|
+
console.error("Subscriber client error:", err);
|
|
174
|
+
try {
|
|
175
|
+
await this.subscriberClient.quit();
|
|
176
|
+
this.subscriberClient = this.client.duplicate();
|
|
177
|
+
await this.setupPubSub();
|
|
178
|
+
} catch (reconnectError) {
|
|
179
|
+
console.error(
|
|
180
|
+
"Failed to reconnect subscriber client:",
|
|
181
|
+
reconnectError
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.error("Error setting up pub/sub client:", error);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async waitUntilReady() {
|
|
191
|
+
await this.setupLock;
|
|
192
|
+
}
|
|
193
|
+
get(key) {
|
|
194
|
+
return this.map.get(key);
|
|
195
|
+
}
|
|
196
|
+
async set(key, value) {
|
|
197
|
+
this.map.set(key, value);
|
|
198
|
+
const operations = [];
|
|
199
|
+
if (this.customizedSync?.withoutSetSync) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (!this.customizedSync?.withoutRedisHashmap) {
|
|
203
|
+
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
|
|
204
|
+
operations.push(
|
|
205
|
+
this.client.hSet(
|
|
206
|
+
options,
|
|
207
|
+
this.keyPrefix + this.redisKey,
|
|
208
|
+
key,
|
|
209
|
+
JSON.stringify(value)
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const insertMessage = {
|
|
214
|
+
type: "insert",
|
|
215
|
+
key,
|
|
216
|
+
value
|
|
217
|
+
};
|
|
218
|
+
operations.push(
|
|
219
|
+
this.client.publish(this.syncChannel, JSON.stringify(insertMessage))
|
|
220
|
+
);
|
|
221
|
+
await Promise.all(operations);
|
|
222
|
+
}
|
|
223
|
+
async delete(keys, withoutSyncMessage = false) {
|
|
224
|
+
const keysArray = Array.isArray(keys) ? keys : [keys];
|
|
225
|
+
const operations = [];
|
|
226
|
+
for (const key of keysArray) {
|
|
227
|
+
this.map.delete(key);
|
|
228
|
+
}
|
|
229
|
+
if (!this.customizedSync?.withoutRedisHashmap) {
|
|
230
|
+
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
|
|
231
|
+
operations.push(
|
|
232
|
+
this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (!withoutSyncMessage) {
|
|
236
|
+
const deletionMessage = {
|
|
237
|
+
type: "delete",
|
|
238
|
+
keys: keysArray
|
|
239
|
+
};
|
|
240
|
+
operations.push(
|
|
241
|
+
this.client.publish(this.syncChannel, JSON.stringify(deletionMessage))
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
await Promise.all(operations);
|
|
245
|
+
}
|
|
246
|
+
has(key) {
|
|
247
|
+
return this.map.has(key);
|
|
248
|
+
}
|
|
249
|
+
entries() {
|
|
250
|
+
return this.map.entries();
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/DeduplicatedRequestHandler.ts
|
|
255
|
+
var DeduplicatedRequestHandler = class {
|
|
256
|
+
constructor(fn, cachingTimeMs, inMemoryDeduplicationCache) {
|
|
257
|
+
// Method to handle deduplicated requests
|
|
258
|
+
this.deduplicatedFunction = (key) => {
|
|
259
|
+
const self = this;
|
|
260
|
+
const dedupedFn = async (...args) => {
|
|
261
|
+
if (self.inMemoryDeduplicationCache && self.inMemoryDeduplicationCache.has(key)) {
|
|
262
|
+
const res = await self.inMemoryDeduplicationCache.get(key).then((v) => structuredClone(v));
|
|
263
|
+
return res;
|
|
264
|
+
}
|
|
265
|
+
const promise = self.fn(...args);
|
|
266
|
+
self.inMemoryDeduplicationCache.set(key, promise);
|
|
267
|
+
try {
|
|
268
|
+
const result = await promise;
|
|
269
|
+
return structuredClone(result);
|
|
270
|
+
} finally {
|
|
271
|
+
setTimeout(() => {
|
|
272
|
+
self.inMemoryDeduplicationCache.delete(key);
|
|
273
|
+
}, self.cachingTimeMs);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
return dedupedFn;
|
|
277
|
+
};
|
|
278
|
+
this.fn = fn;
|
|
279
|
+
this.cachingTimeMs = cachingTimeMs;
|
|
280
|
+
this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;
|
|
281
|
+
}
|
|
282
|
+
// Method to manually seed a result into the cache
|
|
283
|
+
seedRequestReturn(key, value) {
|
|
284
|
+
const resultPromise = new Promise((res) => res(value));
|
|
285
|
+
this.inMemoryDeduplicationCache.set(key, resultPromise);
|
|
286
|
+
setTimeout(() => {
|
|
287
|
+
this.inMemoryDeduplicationCache.delete(key);
|
|
288
|
+
}, this.cachingTimeMs);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// src/RedisStringsHandler.ts
|
|
293
|
+
var NEXT_CACHE_IMPLICIT_TAG_ID = "_N_T_";
|
|
294
|
+
var REVALIDATED_TAGS_KEY = "__revalidated_tags__";
|
|
295
|
+
function isImplicitTag(tag) {
|
|
296
|
+
return tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID);
|
|
297
|
+
}
|
|
298
|
+
function getTimeoutRedisCommandOptions(timeoutMs) {
|
|
299
|
+
return (0, import_redis.commandOptions)({ signal: AbortSignal.timeout(timeoutMs) });
|
|
300
|
+
}
|
|
301
|
+
var RedisStringsHandler = class {
|
|
302
|
+
constructor({
|
|
303
|
+
database = process.env.VERCEL_ENV === "production" ? 0 : 1,
|
|
304
|
+
keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
|
|
305
|
+
sharedTagsKey = "__sharedTags__",
|
|
306
|
+
timeoutMs = 5e3,
|
|
307
|
+
revalidateTagQuerySize = 250,
|
|
308
|
+
avgResyncIntervalMs = 60 * 60 * 1e3,
|
|
309
|
+
redisGetDeduplication = true,
|
|
310
|
+
inMemoryCachingTime = 1e4,
|
|
311
|
+
defaultStaleAge = 60 * 60 * 24 * 14,
|
|
312
|
+
estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === "preview" ? staleAge * 1.2 : staleAge * 2
|
|
313
|
+
}) {
|
|
314
|
+
this.keyPrefix = keyPrefix;
|
|
315
|
+
this.timeoutMs = timeoutMs;
|
|
316
|
+
this.redisGetDeduplication = redisGetDeduplication;
|
|
317
|
+
this.inMemoryCachingTime = inMemoryCachingTime;
|
|
318
|
+
this.defaultStaleAge = defaultStaleAge;
|
|
319
|
+
this.estimateExpireAge = estimateExpireAge;
|
|
320
|
+
try {
|
|
321
|
+
this.client = (0, import_redis.createClient)({
|
|
322
|
+
...database !== 0 ? { database } : {},
|
|
323
|
+
url: process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379"
|
|
324
|
+
});
|
|
325
|
+
this.client.on("error", (error) => {
|
|
326
|
+
console.error("Redis client error", error);
|
|
327
|
+
});
|
|
328
|
+
this.client.connect().then(() => {
|
|
329
|
+
console.info("Redis client connected.");
|
|
330
|
+
}).catch((error) => {
|
|
331
|
+
console.error("Failed to connect Redis client:", error);
|
|
332
|
+
this.client.disconnect();
|
|
333
|
+
});
|
|
334
|
+
} catch (error) {
|
|
335
|
+
console.error("Failed to initialize Redis client");
|
|
336
|
+
throw error;
|
|
337
|
+
}
|
|
338
|
+
const filterKeys = (key) => key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;
|
|
339
|
+
this.sharedTagsMap = new SyncedMap({
|
|
340
|
+
client: this.client,
|
|
341
|
+
keyPrefix,
|
|
342
|
+
redisKey: sharedTagsKey,
|
|
343
|
+
database,
|
|
344
|
+
timeoutMs,
|
|
345
|
+
querySize: revalidateTagQuerySize,
|
|
346
|
+
filterKeys,
|
|
347
|
+
resyncIntervalMs: avgResyncIntervalMs - avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
|
|
348
|
+
});
|
|
349
|
+
this.revalidatedTagsMap = new SyncedMap({
|
|
350
|
+
client: this.client,
|
|
351
|
+
keyPrefix,
|
|
352
|
+
redisKey: REVALIDATED_TAGS_KEY,
|
|
353
|
+
database,
|
|
354
|
+
timeoutMs,
|
|
355
|
+
querySize: revalidateTagQuerySize,
|
|
356
|
+
filterKeys,
|
|
357
|
+
resyncIntervalMs: avgResyncIntervalMs + avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
|
|
358
|
+
});
|
|
359
|
+
this.inMemoryDeduplicationCache = new SyncedMap({
|
|
360
|
+
client: this.client,
|
|
361
|
+
keyPrefix,
|
|
362
|
+
redisKey: "inMemoryDeduplicationCache",
|
|
363
|
+
database,
|
|
364
|
+
timeoutMs,
|
|
365
|
+
querySize: revalidateTagQuerySize,
|
|
366
|
+
filterKeys,
|
|
367
|
+
customizedSync: {
|
|
368
|
+
withoutRedisHashmap: true,
|
|
369
|
+
withoutSetSync: true
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
const redisGet = this.client.get.bind(this.client);
|
|
373
|
+
this.redisDeduplicationHandler = new DeduplicatedRequestHandler(
|
|
374
|
+
redisGet,
|
|
375
|
+
inMemoryCachingTime,
|
|
376
|
+
this.inMemoryDeduplicationCache
|
|
377
|
+
);
|
|
378
|
+
this.redisGet = redisGet;
|
|
379
|
+
this.deduplicatedRedisGet = this.redisDeduplicationHandler.deduplicatedFunction;
|
|
380
|
+
}
|
|
381
|
+
resetRequestCache(...args) {
|
|
382
|
+
console.warn("WARNING resetRequestCache() was called", args);
|
|
383
|
+
}
|
|
384
|
+
async assertClientIsReady() {
|
|
385
|
+
await Promise.all([
|
|
386
|
+
this.sharedTagsMap.waitUntilReady(),
|
|
387
|
+
this.revalidatedTagsMap.waitUntilReady()
|
|
388
|
+
]);
|
|
389
|
+
if (!this.client.isReady) {
|
|
390
|
+
throw new Error("Redis client is not ready yet or connection is lost.");
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async get(key, ctx) {
|
|
394
|
+
await this.assertClientIsReady();
|
|
395
|
+
const clientGet = this.redisGetDeduplication ? this.deduplicatedRedisGet(key) : this.redisGet;
|
|
396
|
+
const result = await clientGet(
|
|
397
|
+
getTimeoutRedisCommandOptions(this.timeoutMs),
|
|
398
|
+
this.keyPrefix + key
|
|
399
|
+
);
|
|
400
|
+
if (!result) {
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
const cacheValue = JSON.parse(result);
|
|
404
|
+
if (!cacheValue) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
if (cacheValue.value?.kind === "FETCH") {
|
|
408
|
+
cacheValue.value.data.body = Buffer.from(
|
|
409
|
+
cacheValue.value.data.body
|
|
410
|
+
).toString("base64");
|
|
411
|
+
}
|
|
412
|
+
const combinedTags = /* @__PURE__ */ new Set([
|
|
413
|
+
...ctx?.softTags || [],
|
|
414
|
+
...ctx?.tags || []
|
|
415
|
+
]);
|
|
416
|
+
if (combinedTags.size === 0) {
|
|
417
|
+
return cacheValue;
|
|
418
|
+
}
|
|
419
|
+
for (const tag of combinedTags) {
|
|
420
|
+
const revalidationTime = this.revalidatedTagsMap.get(tag);
|
|
421
|
+
if (revalidationTime && revalidationTime > cacheValue.lastModified) {
|
|
422
|
+
const redisKey = this.keyPrefix + key;
|
|
423
|
+
this.client.unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey).catch((err) => {
|
|
424
|
+
console.error(
|
|
425
|
+
"Error occurred while unlinking stale data. Retrying now. Error was:",
|
|
426
|
+
err
|
|
427
|
+
);
|
|
428
|
+
this.client.unlink(
|
|
429
|
+
getTimeoutRedisCommandOptions(this.timeoutMs),
|
|
430
|
+
redisKey
|
|
431
|
+
);
|
|
432
|
+
}).finally(async () => {
|
|
433
|
+
await this.sharedTagsMap.delete(key);
|
|
434
|
+
await this.revalidatedTagsMap.delete(tag);
|
|
435
|
+
});
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return cacheValue;
|
|
440
|
+
}
|
|
441
|
+
async set(key, data, ctx) {
|
|
442
|
+
if (data.kind === "FETCH") {
|
|
443
|
+
console.time("encoding" + key);
|
|
444
|
+
data.data.body = Buffer.from(data.data.body, "base64").toString();
|
|
445
|
+
console.timeEnd("encoding" + key);
|
|
446
|
+
}
|
|
447
|
+
await this.assertClientIsReady();
|
|
448
|
+
data.lastModified = Date.now();
|
|
449
|
+
const value = JSON.stringify(data);
|
|
450
|
+
if (this.redisGetDeduplication) {
|
|
451
|
+
this.redisDeduplicationHandler.seedRequestReturn(key, value);
|
|
452
|
+
}
|
|
453
|
+
const expireAt = ctx.revalidate && Number.isSafeInteger(ctx.revalidate) && ctx.revalidate > 0 ? this.estimateExpireAge(ctx.revalidate) : this.estimateExpireAge(this.defaultStaleAge);
|
|
454
|
+
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
|
|
455
|
+
const setOperation = this.client.set(
|
|
456
|
+
options,
|
|
457
|
+
this.keyPrefix + key,
|
|
458
|
+
value,
|
|
459
|
+
{
|
|
460
|
+
EX: expireAt
|
|
461
|
+
}
|
|
462
|
+
);
|
|
463
|
+
let setTagsOperation;
|
|
464
|
+
if (ctx.tags && ctx.tags.length > 0) {
|
|
465
|
+
const currentTags = this.sharedTagsMap.get(key);
|
|
466
|
+
const currentIsSameAsNew = currentTags?.length === ctx.tags.length && currentTags.every((v) => ctx.tags.includes(v)) && ctx.tags.every((v) => currentTags.includes(v));
|
|
467
|
+
if (!currentIsSameAsNew) {
|
|
468
|
+
setTagsOperation = this.sharedTagsMap.set(
|
|
469
|
+
key,
|
|
470
|
+
structuredClone(ctx.tags)
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
await Promise.all([setOperation, setTagsOperation]);
|
|
475
|
+
}
|
|
476
|
+
async revalidateTag(tagOrTags) {
|
|
477
|
+
const tags = new Set([tagOrTags || []].flat());
|
|
478
|
+
await this.assertClientIsReady();
|
|
479
|
+
for (const tag of tags) {
|
|
480
|
+
if (isImplicitTag(tag)) {
|
|
481
|
+
const now = Date.now();
|
|
482
|
+
await this.revalidatedTagsMap.set(tag, now);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const keysToDelete = [];
|
|
486
|
+
for (const [key, sharedTags] of this.sharedTagsMap.entries()) {
|
|
487
|
+
if (sharedTags.some((tag) => tags.has(tag))) {
|
|
488
|
+
keysToDelete.push(key);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (keysToDelete.length === 0) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const fullRedisKeys = keysToDelete.map((key) => this.keyPrefix + key);
|
|
495
|
+
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
|
|
496
|
+
const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);
|
|
497
|
+
if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {
|
|
498
|
+
for (const key of keysToDelete) {
|
|
499
|
+
this.inMemoryDeduplicationCache.delete(key);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const deleteTagsOperation = this.sharedTagsMap.delete(keysToDelete);
|
|
503
|
+
await Promise.all([deleteKeysOperation, deleteTagsOperation]);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// src/CachedHandler.ts
|
|
508
|
+
var cachedHandler;
|
|
509
|
+
var CachedHandler = class {
|
|
510
|
+
constructor(options) {
|
|
511
|
+
if (!cachedHandler) {
|
|
512
|
+
console.log("created cached handler");
|
|
513
|
+
cachedHandler = new RedisStringsHandler(options);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
get(...args) {
|
|
517
|
+
return cachedHandler.get(...args);
|
|
518
|
+
}
|
|
519
|
+
set(...args) {
|
|
520
|
+
return cachedHandler.set(...args);
|
|
521
|
+
}
|
|
522
|
+
revalidateTag(...args) {
|
|
523
|
+
return cachedHandler.revalidateTag(...args);
|
|
524
|
+
}
|
|
525
|
+
resetRequestCache(...args) {
|
|
526
|
+
return cachedHandler.resetRequestCache(...args);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// src/index.ts
|
|
531
|
+
var index_default = CachedHandler;
|
|
532
|
+
//# sourceMappingURL=index.js.map
|