cacheable 1.8.6 → 1.8.8

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.cjs CHANGED
@@ -28,8 +28,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
33
  Cacheable: () => Cacheable,
34
34
  CacheableEvents: () => CacheableEvents,
35
35
  CacheableHooks: () => CacheableHooks,
@@ -44,7 +44,7 @@ __export(src_exports, {
44
44
  wrap: () => wrap,
45
45
  wrapSync: () => wrapSync
46
46
  });
47
- module.exports = __toCommonJS(src_exports);
47
+ module.exports = __toCommonJS(index_exports);
48
48
  var import_keyv2 = require("keyv");
49
49
  var import_hookified2 = require("hookified");
50
50
 
@@ -215,7 +215,9 @@ function wrap(function_, options) {
215
215
  const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
216
216
  value = await cache.get(cacheKey);
217
217
  if (value === void 0) {
218
- value = await coalesceAsync(cacheKey, async () => {
218
+ const cacheId = options.cacheId ?? "default";
219
+ const coalesceKey = `${cacheId}::${cacheKey}`;
220
+ value = await coalesceAsync(coalesceKey, async () => {
219
221
  try {
220
222
  const result = await function_(...arguments_);
221
223
  await cache.set(cacheKey, result, ttl);
@@ -1169,6 +1171,7 @@ var Cacheable = class extends import_hookified2.Hookified {
1169
1171
  _ttl;
1170
1172
  _stats = new CacheableStats({ enabled: false });
1171
1173
  _namespace;
1174
+ _cacheId = Math.random().toString(36).slice(2);
1172
1175
  /**
1173
1176
  * Creates a new cacheable instance
1174
1177
  * @param {CacheableOptions} [options] The options for the cacheable instance
@@ -1190,6 +1193,9 @@ var Cacheable = class extends import_hookified2.Hookified {
1190
1193
  if (options?.ttl) {
1191
1194
  this.setTtl(options.ttl);
1192
1195
  }
1196
+ if (options?.cacheId) {
1197
+ this._cacheId = options.cacheId;
1198
+ }
1193
1199
  if (options?.namespace) {
1194
1200
  this._namespace = options.namespace;
1195
1201
  this._primary.namespace = this.getNameSpace();
@@ -1316,6 +1322,22 @@ var Cacheable = class extends import_hookified2.Hookified {
1316
1322
  set ttl(ttl) {
1317
1323
  this.setTtl(ttl);
1318
1324
  }
1325
+ /**
1326
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
1327
+ * If it is not set then it will be a random string that is generated
1328
+ * @returns {string} The cacheId for the cacheable instance
1329
+ */
1330
+ get cacheId() {
1331
+ return this._cacheId;
1332
+ }
1333
+ /**
1334
+ * Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
1335
+ * If it is not set then it will be a random string that is generated
1336
+ * @param {string} cacheId The cacheId for the cacheable instance
1337
+ */
1338
+ set cacheId(cacheId) {
1339
+ this._cacheId = cacheId;
1340
+ }
1319
1341
  /**
1320
1342
  * Sets the primary store for the cacheable instance
1321
1343
  * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
@@ -1355,9 +1377,10 @@ var Cacheable = class extends import_hookified2.Hookified {
1355
1377
  await this.hook("BEFORE_GET" /* BEFORE_GET */, key);
1356
1378
  result = await this._primary.get(key);
1357
1379
  if (!result && this._secondary) {
1358
- result = await this._secondary.get(key);
1359
- if (result) {
1360
- const finalTtl = shorthandToMilliseconds(this._ttl);
1380
+ const rawResult = await this._secondary.get(key, { raw: true });
1381
+ if (rawResult) {
1382
+ result = rawResult.value;
1383
+ const finalTtl = rawResult.expires ?? void 0;
1361
1384
  await this._primary.set(key, result, finalTtl);
1362
1385
  }
1363
1386
  }
@@ -1640,7 +1663,8 @@ var Cacheable = class extends import_hookified2.Hookified {
1640
1663
  const wrapOptions = {
1641
1664
  ttl: options?.ttl ?? this._ttl,
1642
1665
  keyPrefix: options?.keyPrefix,
1643
- cache: this
1666
+ cache: this,
1667
+ cacheId: this._cacheId
1644
1668
  };
1645
1669
  return wrap(function_, wrapOptions);
1646
1670
  }
package/dist/index.d.cts CHANGED
@@ -113,6 +113,7 @@ type WrapFunctionOptions = {
113
113
  ttl?: number | string;
114
114
  keyPrefix?: string;
115
115
  cacheErrors?: boolean;
116
+ cacheId?: string;
116
117
  };
117
118
  type WrapOptions = WrapFunctionOptions & {
118
119
  cache: Cacheable;
@@ -371,7 +372,7 @@ declare class CacheableMemory extends Hookified {
371
372
  * @param {Object} [options] - The options to wrap
372
373
  * @returns {Function} - The wrapped function
373
374
  */
374
- wrap<T>(function_: (...arguments_: any[]) => T, options?: WrapFunctionOptions): (...arguments_: any[]) => T;
375
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
375
376
  private isPrimitive;
376
377
  private concatStores;
377
378
  private setTtl;
@@ -428,12 +429,38 @@ declare enum CacheableEvents {
428
429
  ERROR = "error"
429
430
  }
430
431
  type CacheableOptions = {
432
+ /**
433
+ * The primary store for the cacheable instance
434
+ */
431
435
  primary?: Keyv | KeyvStoreAdapter;
436
+ /**
437
+ * The secondary store for the cacheable instance
438
+ */
432
439
  secondary?: Keyv | KeyvStoreAdapter;
440
+ /**
441
+ * Whether to enable statistics for the cacheable instance
442
+ */
433
443
  stats?: boolean;
444
+ /**
445
+ * Whether the secondary store is non-blocking mode. It is set to false by default.
446
+ * If it is set to true then the secondary store will not block the primary store.
447
+ */
434
448
  nonBlocking?: boolean;
449
+ /**
450
+ * The time-to-live for the cacheable instance and will be used as the default value.
451
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
452
+ * or undefined if there is no time-to-live.
453
+ */
435
454
  ttl?: number | string;
455
+ /**
456
+ * The namespace for the cacheable instance. It can be a string or a function that returns a string.
457
+ */
436
458
  namespace?: string | (() => string);
459
+ /**
460
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
461
+ * If it is not set then it will be a random string that is generated
462
+ */
463
+ cacheId?: string;
437
464
  };
438
465
  declare class Cacheable extends Hookified {
439
466
  private _primary;
@@ -442,6 +469,7 @@ declare class Cacheable extends Hookified {
442
469
  private _ttl?;
443
470
  private readonly _stats;
444
471
  private _namespace?;
472
+ private _cacheId;
445
473
  /**
446
474
  * Creates a new cacheable instance
447
475
  * @param {CacheableOptions} [options] The options for the cacheable instance
@@ -539,6 +567,18 @@ declare class Cacheable extends Hookified {
539
567
  * ```
540
568
  */
541
569
  set ttl(ttl: number | string | undefined);
570
+ /**
571
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
572
+ * If it is not set then it will be a random string that is generated
573
+ * @returns {string} The cacheId for the cacheable instance
574
+ */
575
+ get cacheId(): string;
576
+ /**
577
+ * Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
578
+ * If it is not set then it will be a random string that is generated
579
+ * @param {string} cacheId The cacheId for the cacheable instance
580
+ */
581
+ set cacheId(cacheId: string);
542
582
  /**
543
583
  * Sets the primary store for the cacheable instance
544
584
  * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
@@ -633,7 +673,7 @@ declare class Cacheable extends Hookified {
633
673
  * @param {WrapOptions} [options] The options for the wrap function
634
674
  * @returns {Function} The wrapped function
635
675
  */
636
- wrap<T>(function_: (...arguments_: any[]) => T, options?: WrapFunctionOptions): (...arguments_: any[]) => T;
676
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
637
677
  /**
638
678
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
639
679
  * @param {any} object the object to hash
package/dist/index.d.ts CHANGED
@@ -113,6 +113,7 @@ type WrapFunctionOptions = {
113
113
  ttl?: number | string;
114
114
  keyPrefix?: string;
115
115
  cacheErrors?: boolean;
116
+ cacheId?: string;
116
117
  };
117
118
  type WrapOptions = WrapFunctionOptions & {
118
119
  cache: Cacheable;
@@ -371,7 +372,7 @@ declare class CacheableMemory extends Hookified {
371
372
  * @param {Object} [options] - The options to wrap
372
373
  * @returns {Function} - The wrapped function
373
374
  */
374
- wrap<T>(function_: (...arguments_: any[]) => T, options?: WrapFunctionOptions): (...arguments_: any[]) => T;
375
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
375
376
  private isPrimitive;
376
377
  private concatStores;
377
378
  private setTtl;
@@ -428,12 +429,38 @@ declare enum CacheableEvents {
428
429
  ERROR = "error"
429
430
  }
430
431
  type CacheableOptions = {
432
+ /**
433
+ * The primary store for the cacheable instance
434
+ */
431
435
  primary?: Keyv | KeyvStoreAdapter;
436
+ /**
437
+ * The secondary store for the cacheable instance
438
+ */
432
439
  secondary?: Keyv | KeyvStoreAdapter;
440
+ /**
441
+ * Whether to enable statistics for the cacheable instance
442
+ */
433
443
  stats?: boolean;
444
+ /**
445
+ * Whether the secondary store is non-blocking mode. It is set to false by default.
446
+ * If it is set to true then the secondary store will not block the primary store.
447
+ */
434
448
  nonBlocking?: boolean;
449
+ /**
450
+ * The time-to-live for the cacheable instance and will be used as the default value.
451
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
452
+ * or undefined if there is no time-to-live.
453
+ */
435
454
  ttl?: number | string;
455
+ /**
456
+ * The namespace for the cacheable instance. It can be a string or a function that returns a string.
457
+ */
436
458
  namespace?: string | (() => string);
459
+ /**
460
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
461
+ * If it is not set then it will be a random string that is generated
462
+ */
463
+ cacheId?: string;
437
464
  };
438
465
  declare class Cacheable extends Hookified {
439
466
  private _primary;
@@ -442,6 +469,7 @@ declare class Cacheable extends Hookified {
442
469
  private _ttl?;
443
470
  private readonly _stats;
444
471
  private _namespace?;
472
+ private _cacheId;
445
473
  /**
446
474
  * Creates a new cacheable instance
447
475
  * @param {CacheableOptions} [options] The options for the cacheable instance
@@ -539,6 +567,18 @@ declare class Cacheable extends Hookified {
539
567
  * ```
540
568
  */
541
569
  set ttl(ttl: number | string | undefined);
570
+ /**
571
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
572
+ * If it is not set then it will be a random string that is generated
573
+ * @returns {string} The cacheId for the cacheable instance
574
+ */
575
+ get cacheId(): string;
576
+ /**
577
+ * Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
578
+ * If it is not set then it will be a random string that is generated
579
+ * @param {string} cacheId The cacheId for the cacheable instance
580
+ */
581
+ set cacheId(cacheId: string);
542
582
  /**
543
583
  * Sets the primary store for the cacheable instance
544
584
  * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
@@ -633,7 +673,7 @@ declare class Cacheable extends Hookified {
633
673
  * @param {WrapOptions} [options] The options for the wrap function
634
674
  * @returns {Function} The wrapped function
635
675
  */
636
- wrap<T>(function_: (...arguments_: any[]) => T, options?: WrapFunctionOptions): (...arguments_: any[]) => T;
676
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
637
677
  /**
638
678
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
639
679
  * @param {any} object the object to hash
package/dist/index.js CHANGED
@@ -171,7 +171,9 @@ function wrap(function_, options) {
171
171
  const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
172
172
  value = await cache.get(cacheKey);
173
173
  if (value === void 0) {
174
- value = await coalesceAsync(cacheKey, async () => {
174
+ const cacheId = options.cacheId ?? "default";
175
+ const coalesceKey = `${cacheId}::${cacheKey}`;
176
+ value = await coalesceAsync(coalesceKey, async () => {
175
177
  try {
176
178
  const result = await function_(...arguments_);
177
179
  await cache.set(cacheKey, result, ttl);
@@ -1128,6 +1130,7 @@ var Cacheable = class extends Hookified2 {
1128
1130
  _ttl;
1129
1131
  _stats = new CacheableStats({ enabled: false });
1130
1132
  _namespace;
1133
+ _cacheId = Math.random().toString(36).slice(2);
1131
1134
  /**
1132
1135
  * Creates a new cacheable instance
1133
1136
  * @param {CacheableOptions} [options] The options for the cacheable instance
@@ -1149,6 +1152,9 @@ var Cacheable = class extends Hookified2 {
1149
1152
  if (options?.ttl) {
1150
1153
  this.setTtl(options.ttl);
1151
1154
  }
1155
+ if (options?.cacheId) {
1156
+ this._cacheId = options.cacheId;
1157
+ }
1152
1158
  if (options?.namespace) {
1153
1159
  this._namespace = options.namespace;
1154
1160
  this._primary.namespace = this.getNameSpace();
@@ -1275,6 +1281,22 @@ var Cacheable = class extends Hookified2 {
1275
1281
  set ttl(ttl) {
1276
1282
  this.setTtl(ttl);
1277
1283
  }
1284
+ /**
1285
+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
1286
+ * If it is not set then it will be a random string that is generated
1287
+ * @returns {string} The cacheId for the cacheable instance
1288
+ */
1289
+ get cacheId() {
1290
+ return this._cacheId;
1291
+ }
1292
+ /**
1293
+ * Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
1294
+ * If it is not set then it will be a random string that is generated
1295
+ * @param {string} cacheId The cacheId for the cacheable instance
1296
+ */
1297
+ set cacheId(cacheId) {
1298
+ this._cacheId = cacheId;
1299
+ }
1278
1300
  /**
1279
1301
  * Sets the primary store for the cacheable instance
1280
1302
  * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
@@ -1314,9 +1336,10 @@ var Cacheable = class extends Hookified2 {
1314
1336
  await this.hook("BEFORE_GET" /* BEFORE_GET */, key);
1315
1337
  result = await this._primary.get(key);
1316
1338
  if (!result && this._secondary) {
1317
- result = await this._secondary.get(key);
1318
- if (result) {
1319
- const finalTtl = shorthandToMilliseconds(this._ttl);
1339
+ const rawResult = await this._secondary.get(key, { raw: true });
1340
+ if (rawResult) {
1341
+ result = rawResult.value;
1342
+ const finalTtl = rawResult.expires ?? void 0;
1320
1343
  await this._primary.set(key, result, finalTtl);
1321
1344
  }
1322
1345
  }
@@ -1599,7 +1622,8 @@ var Cacheable = class extends Hookified2 {
1599
1622
  const wrapOptions = {
1600
1623
  ttl: options?.ttl ?? this._ttl,
1601
1624
  keyPrefix: options?.keyPrefix,
1602
- cache: this
1625
+ cache: this,
1626
+ cacheId: this._cacheId
1603
1627
  };
1604
1628
  return wrap(function_, wrapOptions);
1605
1629
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cacheable",
3
- "version": "1.8.6",
3
+ "version": "1.8.8",
4
4
  "description": "High Performance Layer 1 / Layer 2 Caching with Keyv Storage",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -21,19 +21,19 @@
21
21
  "license": "MIT",
22
22
  "private": false,
23
23
  "devDependencies": {
24
- "@keyv/redis": "^4.0.2",
25
- "@types/node": "^22.10.2",
26
- "@vitest/coverage-v8": "^2.1.8",
24
+ "@keyv/redis": "^4.2.0",
25
+ "@types/node": "^22.10.9",
26
+ "@vitest/coverage-v8": "^3.0.4",
27
27
  "lru-cache": "^11.0.2",
28
28
  "rimraf": "^6.0.1",
29
29
  "tsup": "^8.3.5",
30
- "typescript": "^5.7.2",
31
- "vitest": "^2.1.8",
30
+ "typescript": "^5.7.3",
31
+ "vitest": "^3.0.4",
32
32
  "xo": "^0.60.0"
33
33
  },
34
34
  "dependencies": {
35
- "hookified": "^1.5.1",
36
- "keyv": "^5.2.1"
35
+ "hookified": "^1.7.0",
36
+ "keyv": "^5.2.3"
37
37
  },
38
38
  "keywords": [
39
39
  "cacheable",