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