@modern-js/runtime-utils 0.0.0-nightly-20250424160335 → 0.0.0-nightly-20250428160307
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.
|
@@ -49,14 +49,14 @@ let lruCache;
|
|
|
49
49
|
let cacheConfig = {
|
|
50
50
|
maxSize: CacheSize.GB
|
|
51
51
|
};
|
|
52
|
-
const
|
|
53
|
-
function
|
|
54
|
-
let
|
|
55
|
-
if (!
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
const tagKeyMap = /* @__PURE__ */ new Map();
|
|
53
|
+
function addTagKeyRelation(tag, key) {
|
|
54
|
+
let keys = tagKeyMap.get(tag);
|
|
55
|
+
if (!keys) {
|
|
56
|
+
keys = /* @__PURE__ */ new Set();
|
|
57
|
+
tagKeyMap.set(tag, keys);
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
keys.add(key);
|
|
60
60
|
}
|
|
61
61
|
function configureCache(config) {
|
|
62
62
|
cacheConfig = {
|
|
@@ -125,18 +125,33 @@ function generateKey(args) {
|
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
127
|
function cache(fn, options) {
|
|
128
|
-
const { tag = "default", maxAge = CacheTime.MINUTE * 5, revalidate = 0 } = options || {};
|
|
128
|
+
const { tag = "default", maxAge = CacheTime.MINUTE * 5, revalidate = 0, customKey, onCache } = options || {};
|
|
129
129
|
const store = getLRUCache();
|
|
130
130
|
const tags = Array.isArray(tag) ? tag : [
|
|
131
131
|
tag
|
|
132
132
|
];
|
|
133
|
-
|
|
133
|
+
const getCacheKey = (args, generatedKey) => {
|
|
134
|
+
return customKey ? customKey({
|
|
135
|
+
params: args,
|
|
136
|
+
fn,
|
|
137
|
+
generatedKey
|
|
138
|
+
}) : fn;
|
|
139
|
+
};
|
|
134
140
|
return async (...args) => {
|
|
135
141
|
if (isServer && typeof options === "undefined") {
|
|
136
142
|
var _storage_useContext;
|
|
137
143
|
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
138
144
|
const request = storage === null || storage === void 0 ? void 0 : (_storage_useContext = storage.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
|
|
139
145
|
if (request) {
|
|
146
|
+
let shouldDisableCaching = false;
|
|
147
|
+
if (cacheConfig.unstable_shouldDisable) {
|
|
148
|
+
shouldDisableCaching = await Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
149
|
+
request
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
if (shouldDisableCaching) {
|
|
153
|
+
return fn(...args);
|
|
154
|
+
}
|
|
140
155
|
let requestCache = requestCacheMap.get(request);
|
|
141
156
|
if (!requestCache) {
|
|
142
157
|
requestCache = /* @__PURE__ */ new Map();
|
|
@@ -162,30 +177,61 @@ function cache(fn, options) {
|
|
|
162
177
|
}
|
|
163
178
|
}
|
|
164
179
|
} else if (typeof options !== "undefined") {
|
|
165
|
-
|
|
166
|
-
if (!tagCache) {
|
|
167
|
-
tagCache = /* @__PURE__ */ new Map();
|
|
168
|
-
}
|
|
169
|
-
const key = generateKey(args);
|
|
170
|
-
const cached = tagCache.get(key);
|
|
180
|
+
const genKey = generateKey(args);
|
|
171
181
|
const now = Date.now();
|
|
172
|
-
|
|
182
|
+
const cacheKey = getCacheKey(args, genKey);
|
|
183
|
+
const finalKey = typeof cacheKey === "function" ? genKey : cacheKey;
|
|
184
|
+
tags.forEach((t) => addTagKeyRelation(t, cacheKey));
|
|
185
|
+
let cacheStore = store.get(cacheKey);
|
|
186
|
+
if (!cacheStore) {
|
|
187
|
+
cacheStore = /* @__PURE__ */ new Map();
|
|
188
|
+
}
|
|
189
|
+
const storeKey = customKey && typeof cacheKey === "symbol" ? "symbol-key" : genKey;
|
|
190
|
+
let shouldDisableCaching = false;
|
|
191
|
+
if (isServer && cacheConfig.unstable_shouldDisable) {
|
|
192
|
+
var _storage_useContext1;
|
|
193
|
+
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
194
|
+
const request = storage === null || storage === void 0 ? void 0 : (_storage_useContext1 = storage.useContext()) === null || _storage_useContext1 === void 0 ? void 0 : _storage_useContext1.request;
|
|
195
|
+
if (request) {
|
|
196
|
+
shouldDisableCaching = await Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
197
|
+
request
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const cached = cacheStore.get(storeKey);
|
|
202
|
+
if (cached && !shouldDisableCaching) {
|
|
173
203
|
const age = now - cached.timestamp;
|
|
174
204
|
if (age < maxAge) {
|
|
205
|
+
if (onCache) {
|
|
206
|
+
onCache({
|
|
207
|
+
status: "hit",
|
|
208
|
+
key: finalKey,
|
|
209
|
+
params: args,
|
|
210
|
+
result: cached.data
|
|
211
|
+
});
|
|
212
|
+
}
|
|
175
213
|
return cached.data;
|
|
176
214
|
}
|
|
177
215
|
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
216
|
+
if (onCache) {
|
|
217
|
+
onCache({
|
|
218
|
+
status: "stale",
|
|
219
|
+
key: finalKey,
|
|
220
|
+
params: args,
|
|
221
|
+
result: cached.data
|
|
222
|
+
});
|
|
223
|
+
}
|
|
178
224
|
if (!cached.isRevalidating) {
|
|
179
225
|
cached.isRevalidating = true;
|
|
180
226
|
Promise.resolve().then(async () => {
|
|
181
227
|
try {
|
|
182
228
|
const newData = await fn(...args);
|
|
183
|
-
|
|
229
|
+
cacheStore.set(storeKey, {
|
|
184
230
|
data: newData,
|
|
185
231
|
timestamp: Date.now(),
|
|
186
232
|
isRevalidating: false
|
|
187
233
|
});
|
|
188
|
-
store.set(
|
|
234
|
+
store.set(cacheKey, cacheStore);
|
|
189
235
|
} catch (error) {
|
|
190
236
|
cached.isRevalidating = false;
|
|
191
237
|
if (isServer) {
|
|
@@ -202,12 +248,22 @@ function cache(fn, options) {
|
|
|
202
248
|
}
|
|
203
249
|
}
|
|
204
250
|
const data = await fn(...args);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
251
|
+
if (!shouldDisableCaching) {
|
|
252
|
+
cacheStore.set(storeKey, {
|
|
253
|
+
data,
|
|
254
|
+
timestamp: now,
|
|
255
|
+
isRevalidating: false
|
|
256
|
+
});
|
|
257
|
+
store.set(cacheKey, cacheStore);
|
|
258
|
+
}
|
|
259
|
+
if (onCache) {
|
|
260
|
+
onCache({
|
|
261
|
+
status: "miss",
|
|
262
|
+
key: finalKey,
|
|
263
|
+
params: args,
|
|
264
|
+
result: data
|
|
265
|
+
});
|
|
266
|
+
}
|
|
211
267
|
return data;
|
|
212
268
|
} else {
|
|
213
269
|
console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
|
|
@@ -227,17 +283,17 @@ function withRequestCache(handler) {
|
|
|
227
283
|
};
|
|
228
284
|
}
|
|
229
285
|
function revalidateTag(tag) {
|
|
230
|
-
const
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(
|
|
286
|
+
const keys = tagKeyMap.get(tag);
|
|
287
|
+
if (keys) {
|
|
288
|
+
keys.forEach((key) => {
|
|
289
|
+
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(key);
|
|
234
290
|
});
|
|
235
291
|
}
|
|
236
292
|
}
|
|
237
293
|
function clearStore() {
|
|
238
294
|
lruCache === null || lruCache === void 0 ? void 0 : lruCache.clear();
|
|
239
295
|
lruCache = void 0;
|
|
240
|
-
|
|
296
|
+
tagKeyMap.clear();
|
|
241
297
|
}
|
|
242
298
|
// Annotate the CommonJS export names for ESM import in node:
|
|
243
299
|
0 && (module.exports = {
|
|
@@ -26,14 +26,14 @@ var lruCache;
|
|
|
26
26
|
var cacheConfig = {
|
|
27
27
|
maxSize: CacheSize.GB
|
|
28
28
|
};
|
|
29
|
-
var
|
|
30
|
-
function
|
|
31
|
-
var
|
|
32
|
-
if (!
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
var tagKeyMap = /* @__PURE__ */ new Map();
|
|
30
|
+
function addTagKeyRelation(tag, key) {
|
|
31
|
+
var keys = tagKeyMap.get(tag);
|
|
32
|
+
if (!keys) {
|
|
33
|
+
keys = /* @__PURE__ */ new Set();
|
|
34
|
+
tagKeyMap.set(tag, keys);
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
keys.add(key);
|
|
37
37
|
}
|
|
38
38
|
function configureCache(config) {
|
|
39
39
|
cacheConfig = _object_spread({}, cacheConfig, config);
|
|
@@ -121,16 +121,20 @@ function generateKey(args) {
|
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
123
|
function cache(fn, options) {
|
|
124
|
-
var _ref = options || {}, _ref_tag = _ref.tag, tag = _ref_tag === void 0 ? "default" : _ref_tag, _ref_maxAge = _ref.maxAge, maxAge = _ref_maxAge === void 0 ? CacheTime.MINUTE * 5 : _ref_maxAge, _ref_revalidate = _ref.revalidate, revalidate = _ref_revalidate === void 0 ? 0 : _ref_revalidate;
|
|
124
|
+
var _ref = options || {}, _ref_tag = _ref.tag, tag = _ref_tag === void 0 ? "default" : _ref_tag, _ref_maxAge = _ref.maxAge, maxAge = _ref_maxAge === void 0 ? CacheTime.MINUTE * 5 : _ref_maxAge, _ref_revalidate = _ref.revalidate, revalidate = _ref_revalidate === void 0 ? 0 : _ref_revalidate, customKey = _ref.customKey, onCache = _ref.onCache;
|
|
125
125
|
var store = getLRUCache();
|
|
126
126
|
var tags = Array.isArray(tag) ? tag : [
|
|
127
127
|
tag
|
|
128
128
|
];
|
|
129
|
-
|
|
130
|
-
return
|
|
131
|
-
|
|
129
|
+
var getCacheKey = function(args, generatedKey) {
|
|
130
|
+
return customKey ? customKey({
|
|
131
|
+
params: args,
|
|
132
|
+
fn,
|
|
133
|
+
generatedKey
|
|
134
|
+
}) : fn;
|
|
135
|
+
};
|
|
132
136
|
return /* @__PURE__ */ _async_to_generator(function() {
|
|
133
|
-
var _len, args, _key, _storage_useContext, storage, request, requestCache, fnCache, key, promise, data, error,
|
|
137
|
+
var _len, args, _key, _storage_useContext, storage, request, shouldDisableCaching, requestCache, fnCache, key, promise, data, error, genKey, now, cacheKey, finalKey, cacheStore, storeKey, shouldDisableCaching1, _storage_useContext1, storage1, request1, cached, age, data1;
|
|
134
138
|
var _arguments = arguments;
|
|
135
139
|
return _ts_generator(this, function(_state) {
|
|
136
140
|
switch (_state.label) {
|
|
@@ -141,15 +145,37 @@ function cache(fn, options) {
|
|
|
141
145
|
if (!(isServer && typeof options === "undefined"))
|
|
142
146
|
return [
|
|
143
147
|
3,
|
|
144
|
-
|
|
148
|
+
7
|
|
145
149
|
];
|
|
146
150
|
storage = getAsyncLocalStorage();
|
|
147
151
|
request = storage === null || storage === void 0 ? void 0 : (_storage_useContext = storage.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
|
|
148
152
|
if (!request)
|
|
149
153
|
return [
|
|
150
154
|
3,
|
|
151
|
-
|
|
155
|
+
6
|
|
156
|
+
];
|
|
157
|
+
shouldDisableCaching = false;
|
|
158
|
+
if (!cacheConfig.unstable_shouldDisable)
|
|
159
|
+
return [
|
|
160
|
+
3,
|
|
161
|
+
2
|
|
152
162
|
];
|
|
163
|
+
return [
|
|
164
|
+
4,
|
|
165
|
+
Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
166
|
+
request
|
|
167
|
+
}))
|
|
168
|
+
];
|
|
169
|
+
case 1:
|
|
170
|
+
shouldDisableCaching = _state.sent();
|
|
171
|
+
_state.label = 2;
|
|
172
|
+
case 2:
|
|
173
|
+
if (shouldDisableCaching) {
|
|
174
|
+
return [
|
|
175
|
+
2,
|
|
176
|
+
fn.apply(void 0, _to_consumable_array(args))
|
|
177
|
+
];
|
|
178
|
+
}
|
|
153
179
|
requestCache = requestCacheMap.get(request);
|
|
154
180
|
if (!requestCache) {
|
|
155
181
|
requestCache = /* @__PURE__ */ new Map();
|
|
@@ -169,55 +195,100 @@ function cache(fn, options) {
|
|
|
169
195
|
}
|
|
170
196
|
promise = fn.apply(void 0, _to_consumable_array(args));
|
|
171
197
|
fnCache.set(key, promise);
|
|
172
|
-
_state.label =
|
|
173
|
-
case
|
|
198
|
+
_state.label = 3;
|
|
199
|
+
case 3:
|
|
174
200
|
_state.trys.push([
|
|
175
|
-
1,
|
|
176
201
|
3,
|
|
202
|
+
5,
|
|
177
203
|
,
|
|
178
|
-
|
|
204
|
+
6
|
|
179
205
|
]);
|
|
180
206
|
return [
|
|
181
207
|
4,
|
|
182
208
|
promise
|
|
183
209
|
];
|
|
184
|
-
case
|
|
210
|
+
case 4:
|
|
185
211
|
data = _state.sent();
|
|
186
212
|
return [
|
|
187
213
|
2,
|
|
188
214
|
data
|
|
189
215
|
];
|
|
190
|
-
case
|
|
216
|
+
case 5:
|
|
191
217
|
error = _state.sent();
|
|
192
218
|
fnCache.delete(key);
|
|
193
219
|
throw error;
|
|
194
|
-
case
|
|
220
|
+
case 6:
|
|
195
221
|
return [
|
|
196
222
|
3,
|
|
197
|
-
|
|
223
|
+
12
|
|
198
224
|
];
|
|
199
|
-
case
|
|
225
|
+
case 7:
|
|
200
226
|
if (!(typeof options !== "undefined"))
|
|
201
227
|
return [
|
|
202
228
|
3,
|
|
203
|
-
|
|
229
|
+
11
|
|
204
230
|
];
|
|
205
|
-
|
|
206
|
-
if (!tagCache) {
|
|
207
|
-
tagCache = /* @__PURE__ */ new Map();
|
|
208
|
-
}
|
|
209
|
-
key1 = generateKey(args);
|
|
210
|
-
cached = tagCache.get(key1);
|
|
231
|
+
genKey = generateKey(args);
|
|
211
232
|
now = Date.now();
|
|
212
|
-
|
|
233
|
+
cacheKey = getCacheKey(args, genKey);
|
|
234
|
+
finalKey = typeof cacheKey === "function" ? genKey : cacheKey;
|
|
235
|
+
tags.forEach(function(t) {
|
|
236
|
+
return addTagKeyRelation(t, cacheKey);
|
|
237
|
+
});
|
|
238
|
+
cacheStore = store.get(cacheKey);
|
|
239
|
+
if (!cacheStore) {
|
|
240
|
+
cacheStore = /* @__PURE__ */ new Map();
|
|
241
|
+
}
|
|
242
|
+
storeKey = customKey && (typeof cacheKey === "undefined" ? "undefined" : _type_of(cacheKey)) === "symbol" ? "symbol-key" : genKey;
|
|
243
|
+
shouldDisableCaching1 = false;
|
|
244
|
+
if (!(isServer && cacheConfig.unstable_shouldDisable))
|
|
245
|
+
return [
|
|
246
|
+
3,
|
|
247
|
+
9
|
|
248
|
+
];
|
|
249
|
+
storage1 = getAsyncLocalStorage();
|
|
250
|
+
request1 = storage1 === null || storage1 === void 0 ? void 0 : (_storage_useContext1 = storage1.useContext()) === null || _storage_useContext1 === void 0 ? void 0 : _storage_useContext1.request;
|
|
251
|
+
if (!request1)
|
|
252
|
+
return [
|
|
253
|
+
3,
|
|
254
|
+
9
|
|
255
|
+
];
|
|
256
|
+
return [
|
|
257
|
+
4,
|
|
258
|
+
Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
259
|
+
request: request1
|
|
260
|
+
}))
|
|
261
|
+
];
|
|
262
|
+
case 8:
|
|
263
|
+
shouldDisableCaching1 = _state.sent();
|
|
264
|
+
_state.label = 9;
|
|
265
|
+
case 9:
|
|
266
|
+
cached = cacheStore.get(storeKey);
|
|
267
|
+
if (cached && !shouldDisableCaching1) {
|
|
213
268
|
age = now - cached.timestamp;
|
|
214
269
|
if (age < maxAge) {
|
|
270
|
+
if (onCache) {
|
|
271
|
+
onCache({
|
|
272
|
+
status: "hit",
|
|
273
|
+
key: finalKey,
|
|
274
|
+
params: args,
|
|
275
|
+
result: cached.data
|
|
276
|
+
});
|
|
277
|
+
}
|
|
215
278
|
return [
|
|
216
279
|
2,
|
|
217
280
|
cached.data
|
|
218
281
|
];
|
|
219
282
|
}
|
|
220
283
|
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
284
|
+
if (onCache) {
|
|
285
|
+
onCache({
|
|
286
|
+
status: "stale",
|
|
287
|
+
key: finalKey,
|
|
288
|
+
params: args,
|
|
289
|
+
result: cached.data
|
|
290
|
+
});
|
|
291
|
+
}
|
|
221
292
|
if (!cached.isRevalidating) {
|
|
222
293
|
cached.isRevalidating = true;
|
|
223
294
|
Promise.resolve().then(/* @__PURE__ */ _async_to_generator(function() {
|
|
@@ -237,12 +308,12 @@ function cache(fn, options) {
|
|
|
237
308
|
];
|
|
238
309
|
case 1:
|
|
239
310
|
newData = _state2.sent();
|
|
240
|
-
|
|
311
|
+
cacheStore.set(storeKey, {
|
|
241
312
|
data: newData,
|
|
242
313
|
timestamp: Date.now(),
|
|
243
314
|
isRevalidating: false
|
|
244
315
|
});
|
|
245
|
-
store.set(
|
|
316
|
+
store.set(cacheKey, cacheStore);
|
|
246
317
|
return [
|
|
247
318
|
3,
|
|
248
319
|
3
|
|
@@ -279,25 +350,35 @@ function cache(fn, options) {
|
|
|
279
350
|
4,
|
|
280
351
|
fn.apply(void 0, _to_consumable_array(args))
|
|
281
352
|
];
|
|
282
|
-
case
|
|
353
|
+
case 10:
|
|
283
354
|
data1 = _state.sent();
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
355
|
+
if (!shouldDisableCaching1) {
|
|
356
|
+
cacheStore.set(storeKey, {
|
|
357
|
+
data: data1,
|
|
358
|
+
timestamp: now,
|
|
359
|
+
isRevalidating: false
|
|
360
|
+
});
|
|
361
|
+
store.set(cacheKey, cacheStore);
|
|
362
|
+
}
|
|
363
|
+
if (onCache) {
|
|
364
|
+
onCache({
|
|
365
|
+
status: "miss",
|
|
366
|
+
key: finalKey,
|
|
367
|
+
params: args,
|
|
368
|
+
result: data1
|
|
369
|
+
});
|
|
370
|
+
}
|
|
290
371
|
return [
|
|
291
372
|
2,
|
|
292
373
|
data1
|
|
293
374
|
];
|
|
294
|
-
case
|
|
375
|
+
case 11:
|
|
295
376
|
console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
|
|
296
377
|
return [
|
|
297
378
|
2,
|
|
298
379
|
fn.apply(void 0, _to_consumable_array(args))
|
|
299
380
|
];
|
|
300
|
-
case
|
|
381
|
+
case 12:
|
|
301
382
|
return [
|
|
302
383
|
2
|
|
303
384
|
];
|
|
@@ -336,17 +417,17 @@ function withRequestCache(handler) {
|
|
|
336
417
|
}();
|
|
337
418
|
}
|
|
338
419
|
function revalidateTag(tag) {
|
|
339
|
-
var
|
|
340
|
-
if (
|
|
341
|
-
|
|
342
|
-
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(
|
|
420
|
+
var keys = tagKeyMap.get(tag);
|
|
421
|
+
if (keys) {
|
|
422
|
+
keys.forEach(function(key) {
|
|
423
|
+
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(key);
|
|
343
424
|
});
|
|
344
425
|
}
|
|
345
426
|
}
|
|
346
427
|
function clearStore() {
|
|
347
428
|
lruCache === null || lruCache === void 0 ? void 0 : lruCache.clear();
|
|
348
429
|
lruCache = void 0;
|
|
349
|
-
|
|
430
|
+
tagKeyMap.clear();
|
|
350
431
|
}
|
|
351
432
|
export {
|
|
352
433
|
CacheSize,
|
|
@@ -19,14 +19,14 @@ let lruCache;
|
|
|
19
19
|
let cacheConfig = {
|
|
20
20
|
maxSize: CacheSize.GB
|
|
21
21
|
};
|
|
22
|
-
const
|
|
23
|
-
function
|
|
24
|
-
let
|
|
25
|
-
if (!
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
const tagKeyMap = /* @__PURE__ */ new Map();
|
|
23
|
+
function addTagKeyRelation(tag, key) {
|
|
24
|
+
let keys = tagKeyMap.get(tag);
|
|
25
|
+
if (!keys) {
|
|
26
|
+
keys = /* @__PURE__ */ new Set();
|
|
27
|
+
tagKeyMap.set(tag, keys);
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
keys.add(key);
|
|
30
30
|
}
|
|
31
31
|
function configureCache(config) {
|
|
32
32
|
cacheConfig = {
|
|
@@ -95,18 +95,33 @@ function generateKey(args) {
|
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
function cache(fn, options) {
|
|
98
|
-
const { tag = "default", maxAge = CacheTime.MINUTE * 5, revalidate = 0 } = options || {};
|
|
98
|
+
const { tag = "default", maxAge = CacheTime.MINUTE * 5, revalidate = 0, customKey, onCache } = options || {};
|
|
99
99
|
const store = getLRUCache();
|
|
100
100
|
const tags = Array.isArray(tag) ? tag : [
|
|
101
101
|
tag
|
|
102
102
|
];
|
|
103
|
-
|
|
103
|
+
const getCacheKey = (args, generatedKey) => {
|
|
104
|
+
return customKey ? customKey({
|
|
105
|
+
params: args,
|
|
106
|
+
fn,
|
|
107
|
+
generatedKey
|
|
108
|
+
}) : fn;
|
|
109
|
+
};
|
|
104
110
|
return async (...args) => {
|
|
105
111
|
if (isServer && typeof options === "undefined") {
|
|
106
112
|
var _storage_useContext;
|
|
107
113
|
const storage = getAsyncLocalStorage();
|
|
108
114
|
const request = storage === null || storage === void 0 ? void 0 : (_storage_useContext = storage.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
|
|
109
115
|
if (request) {
|
|
116
|
+
let shouldDisableCaching = false;
|
|
117
|
+
if (cacheConfig.unstable_shouldDisable) {
|
|
118
|
+
shouldDisableCaching = await Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
119
|
+
request
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
if (shouldDisableCaching) {
|
|
123
|
+
return fn(...args);
|
|
124
|
+
}
|
|
110
125
|
let requestCache = requestCacheMap.get(request);
|
|
111
126
|
if (!requestCache) {
|
|
112
127
|
requestCache = /* @__PURE__ */ new Map();
|
|
@@ -132,30 +147,61 @@ function cache(fn, options) {
|
|
|
132
147
|
}
|
|
133
148
|
}
|
|
134
149
|
} else if (typeof options !== "undefined") {
|
|
135
|
-
|
|
136
|
-
if (!tagCache) {
|
|
137
|
-
tagCache = /* @__PURE__ */ new Map();
|
|
138
|
-
}
|
|
139
|
-
const key = generateKey(args);
|
|
140
|
-
const cached = tagCache.get(key);
|
|
150
|
+
const genKey = generateKey(args);
|
|
141
151
|
const now = Date.now();
|
|
142
|
-
|
|
152
|
+
const cacheKey = getCacheKey(args, genKey);
|
|
153
|
+
const finalKey = typeof cacheKey === "function" ? genKey : cacheKey;
|
|
154
|
+
tags.forEach((t) => addTagKeyRelation(t, cacheKey));
|
|
155
|
+
let cacheStore = store.get(cacheKey);
|
|
156
|
+
if (!cacheStore) {
|
|
157
|
+
cacheStore = /* @__PURE__ */ new Map();
|
|
158
|
+
}
|
|
159
|
+
const storeKey = customKey && typeof cacheKey === "symbol" ? "symbol-key" : genKey;
|
|
160
|
+
let shouldDisableCaching = false;
|
|
161
|
+
if (isServer && cacheConfig.unstable_shouldDisable) {
|
|
162
|
+
var _storage_useContext1;
|
|
163
|
+
const storage = getAsyncLocalStorage();
|
|
164
|
+
const request = storage === null || storage === void 0 ? void 0 : (_storage_useContext1 = storage.useContext()) === null || _storage_useContext1 === void 0 ? void 0 : _storage_useContext1.request;
|
|
165
|
+
if (request) {
|
|
166
|
+
shouldDisableCaching = await Promise.resolve(cacheConfig.unstable_shouldDisable({
|
|
167
|
+
request
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const cached = cacheStore.get(storeKey);
|
|
172
|
+
if (cached && !shouldDisableCaching) {
|
|
143
173
|
const age = now - cached.timestamp;
|
|
144
174
|
if (age < maxAge) {
|
|
175
|
+
if (onCache) {
|
|
176
|
+
onCache({
|
|
177
|
+
status: "hit",
|
|
178
|
+
key: finalKey,
|
|
179
|
+
params: args,
|
|
180
|
+
result: cached.data
|
|
181
|
+
});
|
|
182
|
+
}
|
|
145
183
|
return cached.data;
|
|
146
184
|
}
|
|
147
185
|
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
186
|
+
if (onCache) {
|
|
187
|
+
onCache({
|
|
188
|
+
status: "stale",
|
|
189
|
+
key: finalKey,
|
|
190
|
+
params: args,
|
|
191
|
+
result: cached.data
|
|
192
|
+
});
|
|
193
|
+
}
|
|
148
194
|
if (!cached.isRevalidating) {
|
|
149
195
|
cached.isRevalidating = true;
|
|
150
196
|
Promise.resolve().then(async () => {
|
|
151
197
|
try {
|
|
152
198
|
const newData = await fn(...args);
|
|
153
|
-
|
|
199
|
+
cacheStore.set(storeKey, {
|
|
154
200
|
data: newData,
|
|
155
201
|
timestamp: Date.now(),
|
|
156
202
|
isRevalidating: false
|
|
157
203
|
});
|
|
158
|
-
store.set(
|
|
204
|
+
store.set(cacheKey, cacheStore);
|
|
159
205
|
} catch (error) {
|
|
160
206
|
cached.isRevalidating = false;
|
|
161
207
|
if (isServer) {
|
|
@@ -172,12 +218,22 @@ function cache(fn, options) {
|
|
|
172
218
|
}
|
|
173
219
|
}
|
|
174
220
|
const data = await fn(...args);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
221
|
+
if (!shouldDisableCaching) {
|
|
222
|
+
cacheStore.set(storeKey, {
|
|
223
|
+
data,
|
|
224
|
+
timestamp: now,
|
|
225
|
+
isRevalidating: false
|
|
226
|
+
});
|
|
227
|
+
store.set(cacheKey, cacheStore);
|
|
228
|
+
}
|
|
229
|
+
if (onCache) {
|
|
230
|
+
onCache({
|
|
231
|
+
status: "miss",
|
|
232
|
+
key: finalKey,
|
|
233
|
+
params: args,
|
|
234
|
+
result: data
|
|
235
|
+
});
|
|
236
|
+
}
|
|
181
237
|
return data;
|
|
182
238
|
} else {
|
|
183
239
|
console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
|
|
@@ -197,17 +253,17 @@ function withRequestCache(handler) {
|
|
|
197
253
|
};
|
|
198
254
|
}
|
|
199
255
|
function revalidateTag(tag) {
|
|
200
|
-
const
|
|
201
|
-
if (
|
|
202
|
-
|
|
203
|
-
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(
|
|
256
|
+
const keys = tagKeyMap.get(tag);
|
|
257
|
+
if (keys) {
|
|
258
|
+
keys.forEach((key) => {
|
|
259
|
+
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(key);
|
|
204
260
|
});
|
|
205
261
|
}
|
|
206
262
|
}
|
|
207
263
|
function clearStore() {
|
|
208
264
|
lruCache === null || lruCache === void 0 ? void 0 : lruCache.clear();
|
|
209
265
|
lruCache = void 0;
|
|
210
|
-
|
|
266
|
+
tagKeyMap.clear();
|
|
211
267
|
}
|
|
212
268
|
export {
|
|
213
269
|
CacheSize,
|
|
@@ -11,13 +11,29 @@ export declare const CacheTime: {
|
|
|
11
11
|
readonly WEEK: number;
|
|
12
12
|
readonly MONTH: number;
|
|
13
13
|
};
|
|
14
|
+
export type CacheStatus = 'hit' | 'stale' | 'miss';
|
|
15
|
+
export interface CacheStatsInfo {
|
|
16
|
+
status: CacheStatus;
|
|
17
|
+
key: string | symbol;
|
|
18
|
+
params: any[];
|
|
19
|
+
result: any;
|
|
20
|
+
}
|
|
14
21
|
interface CacheOptions {
|
|
15
22
|
tag?: string | string[];
|
|
16
23
|
maxAge?: number;
|
|
17
24
|
revalidate?: number;
|
|
25
|
+
customKey?: <Args extends any[]>(options: {
|
|
26
|
+
params: Args;
|
|
27
|
+
fn: (...args: Args) => any;
|
|
28
|
+
generatedKey: string;
|
|
29
|
+
}) => string | symbol;
|
|
30
|
+
onCache?: (info: CacheStatsInfo) => void;
|
|
18
31
|
}
|
|
19
32
|
interface CacheConfig {
|
|
20
33
|
maxSize: number;
|
|
34
|
+
unstable_shouldDisable?: ({ request, }: {
|
|
35
|
+
request: Request;
|
|
36
|
+
}) => boolean | Promise<boolean>;
|
|
21
37
|
}
|
|
22
38
|
export declare function configureCache(config: CacheConfig): void;
|
|
23
39
|
export declare function generateKey(args: unknown[]): string;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-nightly-
|
|
18
|
+
"version": "0.0.0-nightly-20250428160307",
|
|
19
19
|
"_comment": "Provide ESM and CJS exports, ESM is used by runtime package, for treeshaking",
|
|
20
20
|
"exports": {
|
|
21
21
|
"./router": {
|
|
@@ -145,8 +145,8 @@
|
|
|
145
145
|
"lru-cache": "^10.4.3",
|
|
146
146
|
"react-router-dom": "6.27.0",
|
|
147
147
|
"serialize-javascript": "^6.0.0",
|
|
148
|
-
"@modern-js/types": "0.0.0-nightly-
|
|
149
|
-
"@modern-js/utils": "0.0.0-nightly-
|
|
148
|
+
"@modern-js/types": "0.0.0-nightly-20250428160307",
|
|
149
|
+
"@modern-js/utils": "0.0.0-nightly-20250428160307"
|
|
150
150
|
},
|
|
151
151
|
"peerDependencies": {
|
|
152
152
|
"react": ">=17.0.0",
|
|
@@ -168,8 +168,8 @@
|
|
|
168
168
|
"react": "^18.3.1",
|
|
169
169
|
"react-dom": "^18.3.1",
|
|
170
170
|
"typescript": "^5",
|
|
171
|
-
"@scripts/build": "0.0.0-nightly-
|
|
172
|
-
"@scripts/jest-config": "0.0.0-nightly-
|
|
171
|
+
"@scripts/build": "0.0.0-nightly-20250428160307",
|
|
172
|
+
"@scripts/jest-config": "0.0.0-nightly-20250428160307"
|
|
173
173
|
},
|
|
174
174
|
"sideEffects": false,
|
|
175
175
|
"scripts": {
|