@sebspark/promise-cache 2.1.0 → 2.1.2
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/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -6
- package/dist/index.mjs +7 -6
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -75,10 +75,10 @@ declare class PromiseCache<U> {
|
|
|
75
75
|
* @param key Cache key.
|
|
76
76
|
* @param delegate The function to execute if the key is not in the cache.
|
|
77
77
|
* @param ttlInSeconds Time to live in seconds.
|
|
78
|
-
* @param
|
|
78
|
+
* @param ttlKeyInSeconds The key in the response object that contains the TTL.
|
|
79
79
|
* @returns The result of the delegate function.
|
|
80
80
|
*/
|
|
81
|
-
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number,
|
|
81
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
declare class LocalStorage {
|
package/dist/index.d.ts
CHANGED
|
@@ -75,10 +75,10 @@ declare class PromiseCache<U> {
|
|
|
75
75
|
* @param key Cache key.
|
|
76
76
|
* @param delegate The function to execute if the key is not in the cache.
|
|
77
77
|
* @param ttlInSeconds Time to live in seconds.
|
|
78
|
-
* @param
|
|
78
|
+
* @param ttlKeyInSeconds The key in the response object that contains the TTL.
|
|
79
79
|
* @returns The result of the delegate function.
|
|
80
80
|
*/
|
|
81
|
-
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number,
|
|
81
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
declare class LocalStorage {
|
package/dist/index.js
CHANGED
|
@@ -283,26 +283,27 @@ var PromiseCache = class {
|
|
|
283
283
|
* @param key Cache key.
|
|
284
284
|
* @param delegate The function to execute if the key is not in the cache.
|
|
285
285
|
* @param ttlInSeconds Time to live in seconds.
|
|
286
|
-
* @param
|
|
286
|
+
* @param ttlKeyInSeconds The key in the response object that contains the TTL.
|
|
287
287
|
* @returns The result of the delegate function.
|
|
288
288
|
*/
|
|
289
|
-
async wrap(key, delegate, ttlInSeconds,
|
|
289
|
+
async wrap(key, delegate, ttlInSeconds, ttlKeyInSeconds) {
|
|
290
290
|
const now = Date.now();
|
|
291
291
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
292
292
|
let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
293
293
|
const cached = await this.persistor.get(effectiveKey);
|
|
294
294
|
if (cached) {
|
|
295
|
-
if (!
|
|
295
|
+
if (!ttlKeyInSeconds && cached.ttl !== effectiveTTL) {
|
|
296
296
|
console.error(
|
|
297
|
-
|
|
297
|
+
"WARNING: TTL mismatch for key. It is recommended to use the same TTL for the same key."
|
|
298
298
|
);
|
|
299
299
|
}
|
|
300
300
|
return cached.value;
|
|
301
301
|
}
|
|
302
302
|
const response = await delegate();
|
|
303
|
-
if (
|
|
303
|
+
if (ttlKeyInSeconds) {
|
|
304
304
|
const responseDict = response;
|
|
305
|
-
|
|
305
|
+
const responseTTL = Number(responseDict[ttlKeyInSeconds]) * 1e3;
|
|
306
|
+
effectiveTTL = responseTTL || effectiveTTL;
|
|
306
307
|
}
|
|
307
308
|
this.persistor.set(effectiveKey, {
|
|
308
309
|
value: response,
|
package/dist/index.mjs
CHANGED
|
@@ -254,26 +254,27 @@ var PromiseCache = class {
|
|
|
254
254
|
* @param key Cache key.
|
|
255
255
|
* @param delegate The function to execute if the key is not in the cache.
|
|
256
256
|
* @param ttlInSeconds Time to live in seconds.
|
|
257
|
-
* @param
|
|
257
|
+
* @param ttlKeyInSeconds The key in the response object that contains the TTL.
|
|
258
258
|
* @returns The result of the delegate function.
|
|
259
259
|
*/
|
|
260
|
-
async wrap(key, delegate, ttlInSeconds,
|
|
260
|
+
async wrap(key, delegate, ttlInSeconds, ttlKeyInSeconds) {
|
|
261
261
|
const now = Date.now();
|
|
262
262
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
263
263
|
let effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
264
264
|
const cached = await this.persistor.get(effectiveKey);
|
|
265
265
|
if (cached) {
|
|
266
|
-
if (!
|
|
266
|
+
if (!ttlKeyInSeconds && cached.ttl !== effectiveTTL) {
|
|
267
267
|
console.error(
|
|
268
|
-
|
|
268
|
+
"WARNING: TTL mismatch for key. It is recommended to use the same TTL for the same key."
|
|
269
269
|
);
|
|
270
270
|
}
|
|
271
271
|
return cached.value;
|
|
272
272
|
}
|
|
273
273
|
const response = await delegate();
|
|
274
|
-
if (
|
|
274
|
+
if (ttlKeyInSeconds) {
|
|
275
275
|
const responseDict = response;
|
|
276
|
-
|
|
276
|
+
const responseTTL = Number(responseDict[ttlKeyInSeconds]) * 1e3;
|
|
277
|
+
effectiveTTL = responseTTL || effectiveTTL;
|
|
277
278
|
}
|
|
278
279
|
this.persistor.set(effectiveKey, {
|
|
279
280
|
value: response,
|