@sebspark/promise-cache 2.0.10 → 2.1.0

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 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 ttlKey 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, ttlKey?: 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 ttlKey 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, ttlKey?: 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 ttlKey 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, ttlKey) {
289
290
  const now = Date.now();
290
291
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
291
- const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
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 (!ttlKey && 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,10 @@ var PromiseCache = class {
299
300
  return cached.value;
300
301
  }
301
302
  const response = await delegate();
303
+ if (ttlKey) {
304
+ const responseDict = response;
305
+ effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
306
+ }
302
307
  this.persistor.set(effectiveKey, {
303
308
  value: response,
304
309
  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 ttlKey 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, ttlKey) {
260
261
  const now = Date.now();
261
262
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
262
- const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
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 (!ttlKey && 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,10 @@ var PromiseCache = class {
270
271
  return cached.value;
271
272
  }
272
273
  const response = await delegate();
274
+ if (ttlKey) {
275
+ const responseDict = response;
276
+ effectiveTTL = Number(responseDict[ttlKey]) || effectiveTTL;
277
+ }
273
278
  this.persistor.set(effectiveKey, {
274
279
  value: response,
275
280
  timestamp: now,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.10",
3
+ "version": "2.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",