cacheable 1.7.1 → 1.8.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.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv';
2
+ export { Keyv, KeyvHooks, KeyvOptions, KeyvStoreAdapter } from 'keyv';
2
3
  import { Hookified } from 'hookified';
3
4
 
4
5
  type CacheableOptions$1 = {
@@ -16,16 +17,58 @@ declare class CacheableStats {
16
17
  private _count;
17
18
  private _enabled;
18
19
  constructor(options?: CacheableOptions$1);
20
+ /**
21
+ * @returns {boolean} - Whether the stats are enabled
22
+ */
19
23
  get enabled(): boolean;
24
+ /**
25
+ * @param {boolean} enabled - Whether to enable the stats
26
+ */
20
27
  set enabled(enabled: boolean);
28
+ /**
29
+ * @returns {number} - The number of hits
30
+ * @readonly
31
+ */
21
32
  get hits(): number;
33
+ /**
34
+ * @returns {number} - The number of misses
35
+ * @readonly
36
+ */
22
37
  get misses(): number;
38
+ /**
39
+ * @returns {number} - The number of gets
40
+ * @readonly
41
+ */
23
42
  get gets(): number;
43
+ /**
44
+ * @returns {number} - The number of sets
45
+ * @readonly
46
+ */
24
47
  get sets(): number;
48
+ /**
49
+ * @returns {number} - The number of deletes
50
+ * @readonly
51
+ */
25
52
  get deletes(): number;
53
+ /**
54
+ * @returns {number} - The number of clears
55
+ * @readonly
56
+ */
26
57
  get clears(): number;
58
+ /**
59
+ * @returns {number} - The vsize (value size) of the cache instance
60
+ * @readonly
61
+ */
27
62
  get vsize(): number;
63
+ /**
64
+ * @returns {number} - The ksize (key size) of the cache instance
65
+ * @readonly
66
+ */
28
67
  get ksize(): number;
68
+ /**
69
+ * @returns {number} - The count of the cache instance
70
+ * @readonly
71
+ */
29
72
  get count(): number;
30
73
  incrementHits(): void;
31
74
  incrementMisses(): void;
@@ -46,6 +89,15 @@ declare class CacheableStats {
46
89
  resetStoreValues(): void;
47
90
  }
48
91
 
92
+ /**
93
+ * CacheableItem
94
+ * @typedef {Object} CacheableItem
95
+ * @property {string} key - The key of the cacheable item
96
+ * @property {any} value - The value of the cacheable item
97
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
98
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
99
+ * undefined then it will not have a time-to-live.
100
+ */
49
101
  type CacheableItem = {
50
102
  key: string;
51
103
  value: any;
@@ -57,6 +109,15 @@ type CacheableStoreItem = {
57
109
  expires?: number;
58
110
  };
59
111
 
112
+ /**
113
+ * @typedef {Object} CacheableMemoryOptions
114
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
115
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
116
+ * undefined then it will not have a time-to-live.
117
+ * @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
118
+ * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
119
+ * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
120
+ */
60
121
  type CacheableMemoryOptions = {
61
122
  ttl?: number | string;
62
123
  useClone?: boolean;
@@ -81,39 +142,213 @@ declare class CacheableMemory {
81
142
  private _lruSize;
82
143
  private _checkInterval;
83
144
  private _interval;
145
+ /**
146
+ * @constructor
147
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
148
+ */
84
149
  constructor(options?: CacheableMemoryOptions);
150
+ /**
151
+ * Gets the time-to-live
152
+ * @returns {number|string|undefined} - The time-to-live in miliseconds or a human-readable format. If undefined, it will not have a time-to-live.
153
+ */
85
154
  get ttl(): number | string | undefined;
155
+ /**
156
+ * Sets the time-to-live
157
+ * @param {number|string|undefined} value - The time-to-live in miliseconds or a human-readable format (example '1s' = 1 second, '1h' = 1 hour). If undefined, it will not have a time-to-live.
158
+ */
86
159
  set ttl(value: number | string | undefined);
160
+ /**
161
+ * Gets whether to use clone
162
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
163
+ */
87
164
  get useClone(): boolean;
165
+ /**
166
+ * Sets whether to use clone
167
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
168
+ */
88
169
  set useClone(value: boolean);
170
+ /**
171
+ * Gets the size of the LRU cache
172
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
173
+ */
89
174
  get lruSize(): number;
175
+ /**
176
+ * Sets the size of the LRU cache
177
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
178
+ */
90
179
  set lruSize(value: number);
180
+ /**
181
+ * Gets the check interval
182
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
183
+ */
91
184
  get checkInterval(): number;
185
+ /**
186
+ * Sets the check interval
187
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
188
+ */
92
189
  set checkInterval(value: number);
190
+ /**
191
+ * Gets the size of the cache
192
+ * @returns {number} - The size of the cache
193
+ */
93
194
  get size(): number;
195
+ /**
196
+ * Gets the keys
197
+ * @returns {IterableIterator<string>} - The keys
198
+ */
94
199
  get keys(): IterableIterator<string>;
200
+ /**
201
+ * Gets the items
202
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
203
+ */
95
204
  get items(): IterableIterator<CacheableStoreItem>;
96
- get<T>(key: string): any;
97
- getMany<T>(keys: string[]): any[];
205
+ /**
206
+ * Gets the value of the key
207
+ * @param {string} key - The key to get the value
208
+ * @returns {T | undefined} - The value of the key
209
+ */
210
+ get<T>(key: string): T | undefined;
211
+ /**
212
+ * Gets the values of the keys
213
+ * @param {string[]} keys - The keys to get the values
214
+ * @returns {T[]} - The values of the keys
215
+ */
216
+ getMany<T>(keys: string[]): T[];
217
+ /**
218
+ * Gets the raw value of the key
219
+ * @param {string} key - The key to get the value
220
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
221
+ */
98
222
  getRaw(key: string): CacheableStoreItem | undefined;
223
+ /**
224
+ * Gets the raw values of the keys
225
+ * @param {string[]} keys - The keys to get the values
226
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
227
+ */
99
228
  getManyRaw(keys: string[]): Array<CacheableStoreItem | undefined>;
229
+ /**
230
+ * Sets the value of the key
231
+ * @param {string} key - The key to set the value
232
+ * @param {any} value - The value to set
233
+ * @param {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable.
234
+ * If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
235
+ * @returns {void}
236
+ */
100
237
  set(key: string, value: any, ttl?: number | string): void;
238
+ /**
239
+ * Sets the values of the keys
240
+ * @param {CacheableItem[]} items - The items to set
241
+ * @returns {void}
242
+ */
101
243
  setMany(items: CacheableItem[]): void;
244
+ /**
245
+ * Checks if the key exists
246
+ * @param {string} key - The key to check
247
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
248
+ */
102
249
  has(key: string): boolean;
103
- take<T>(key: string): any;
104
- takeMany<T>(keys: string[]): any[];
250
+ /**
251
+ * @function hasMany
252
+ * @param {string[]} keys - The keys to check
253
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
254
+ */
255
+ hasMany(keys: string[]): boolean[];
256
+ /**
257
+ * Take will get the key and delete the entry from cache
258
+ * @param {string} key - The key to take
259
+ * @returns {T | undefined} - The value of the key
260
+ */
261
+ take<T>(key: string): T | undefined;
262
+ /**
263
+ * TakeMany will get the keys and delete the entries from cache
264
+ * @param {string[]} keys - The keys to take
265
+ * @returns {T[]} - The values of the keys
266
+ */
267
+ takeMany<T>(keys: string[]): T[];
268
+ /**
269
+ * Delete the key
270
+ * @param {string} key - The key to delete
271
+ * @returns {void}
272
+ */
105
273
  delete(key: string): void;
274
+ /**
275
+ * Delete the keys
276
+ * @param {string[]} keys - The keys to delete
277
+ * @returns {void}
278
+ */
106
279
  deleteMany(keys: string[]): void;
280
+ /**
281
+ * Clear the cache
282
+ * @returns {void}
283
+ */
107
284
  clear(): void;
285
+ /**
286
+ * Hash the key. this is used to determine which store to use (internal use)
287
+ * @param {string} key - The key to hash
288
+ * @returns {number} - The hash number
289
+ */
108
290
  hashKey(key: string): number;
291
+ /**
292
+ * Get the store based on the key (internal use)
293
+ * @param {string} key - The key to get the store
294
+ * @returns {Map<string, any>} - The store
295
+ */
109
296
  getStore(key: string): Map<string, any>;
297
+ /**
298
+ * Clone the value. This is for internal use
299
+ * @param {any} value - The value to clone
300
+ * @returns {any} - The cloned value
301
+ */
110
302
  clone(value: any): any;
303
+ /**
304
+ * Add to the front of the LRU cache. This is for internal use
305
+ * @param {string} key - The key to add to the front
306
+ * @returns {void}
307
+ */
111
308
  lruAddToFront(key: string): void;
309
+ /**
310
+ * Move to the front of the LRU cache. This is for internal use
311
+ * @param {string} key - The key to move to the front
312
+ * @returns {void}
313
+ */
112
314
  lruMoveToFront(key: string): void;
315
+ /**
316
+ * Resize the LRU cache. This is for internal use
317
+ * @returns {void}
318
+ */
113
319
  lruResize(): void;
320
+ /**
321
+ * Check for expiration. This is for internal use
322
+ * @returns {void}
323
+ */
114
324
  checkExpiration(): void;
325
+ /**
326
+ * Start the interval check. This is for internal use
327
+ * @returns {void}
328
+ */
115
329
  startIntervalCheck(): void;
330
+ /**
331
+ * Stop the interval check. This is for internal use
332
+ * @returns {void}
333
+ */
116
334
  stopIntervalCheck(): void;
335
+ /**
336
+ * Hash the object. This is for internal use
337
+ * @param {any} object - The object to hash
338
+ * @param {string} [algorithm='sha256'] - The algorithm to hash
339
+ * @returns {string} - The hashed string
340
+ */
341
+ hash(object: any, algorithm?: string): string;
342
+ /**
343
+ * Wrap the function for caching
344
+ * @param {Function} function_ - The function to wrap
345
+ * @param {Object} [options] - The options to wrap
346
+ * @returns {Function} - The wrapped function
347
+ */
348
+ wrap<T>(function_: (...arguments_: any[]) => T, options?: {
349
+ ttl?: number;
350
+ key?: string;
351
+ }): (...arguments_: any[]) => T;
117
352
  private isPrimitive;
118
353
  private concatStores;
119
354
  private setTtl;
@@ -142,6 +377,20 @@ declare class KeyvCacheableMemory implements KeyvStoreAdapter {
142
377
  declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
143
378
  declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
144
379
 
380
+ type WrapOptions = {
381
+ ttl?: number | string;
382
+ key?: string;
383
+ cache: Cacheable;
384
+ };
385
+ type WrapSyncOptions = {
386
+ ttl?: number | string;
387
+ key?: string;
388
+ cache: CacheableMemory;
389
+ };
390
+ type AnyFunction = (...arguments_: any[]) => any;
391
+ declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
392
+ declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
393
+
145
394
  declare enum CacheableHooks {
146
395
  BEFORE_SET = "BEFORE_SET",
147
396
  AFTER_SET = "AFTER_SET",
@@ -168,34 +417,201 @@ declare class Cacheable extends Hookified {
168
417
  private _nonBlocking;
169
418
  private _ttl?;
170
419
  private readonly _stats;
420
+ /**
421
+ * Creates a new cacheable instance
422
+ * @param {CacheableOptions} [options] The options for the cacheable instance
423
+ */
171
424
  constructor(options?: CacheableOptions);
425
+ /**
426
+ * The statistics for the cacheable instance
427
+ * @returns {CacheableStats} The statistics for the cacheable instance
428
+ */
172
429
  get stats(): CacheableStats;
430
+ /**
431
+ * The primary store for the cacheable instance
432
+ * @returns {Keyv} The primary store for the cacheable instance
433
+ */
173
434
  get primary(): Keyv;
435
+ /**
436
+ * Sets the primary store for the cacheable instance
437
+ * @param {Keyv} primary The primary store for the cacheable instance
438
+ */
174
439
  set primary(primary: Keyv);
440
+ /**
441
+ * The secondary store for the cacheable instance
442
+ * @returns {Keyv | undefined} The secondary store for the cacheable instance
443
+ */
175
444
  get secondary(): Keyv | undefined;
445
+ /**
446
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
447
+ * @param {Keyv | undefined} secondary The secondary store for the cacheable instance
448
+ * @returns {void}
449
+ */
176
450
  set secondary(secondary: Keyv | undefined);
451
+ /**
452
+ * Gets whether the secondary store is non-blocking mode. It is set to false by default.
453
+ * If it is set to true then the secondary store will not block the primary store.
454
+ *
455
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
456
+ *
457
+ * @returns {boolean} Whether the cacheable instance is non-blocking
458
+ */
177
459
  get nonBlocking(): boolean;
460
+ /**
461
+ * Sets whether the secondary store is non-blocking mode. It is set to false by default.
462
+ * If it is set to true then the secondary store will not block the primary store.
463
+ *
464
+ * [Learn more about non-blocking mode](https://cacheable.org/docs/cacheable/#non-blocking-operations).
465
+ *
466
+ * @param {boolean} nonBlocking Whether the cacheable instance is non-blocking
467
+ * @returns {void}
468
+ */
178
469
  set nonBlocking(nonBlocking: boolean);
470
+ /**
471
+ * The time-to-live for the cacheable instance and will be used as the default value.
472
+ * can be a number in milliseconds or a human-readable format such as `1s` for 1 second or `1h` for 1 hour
473
+ * or undefined if there is no time-to-live.
474
+ *
475
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
476
+ *
477
+ * @returns {number | string | undefined} The time-to-live for the cacheable instance in milliseconds, human-readable format or undefined
478
+ * @example
479
+ * ```typescript
480
+ * const cacheable = new Cacheable({ ttl: '1h' });
481
+ * console.log(cacheable.ttl); // 1h
482
+ * ```
483
+ */
179
484
  get ttl(): number | string | undefined;
485
+ /**
486
+ * Sets the time-to-live for the cacheable instance and will be used as the default value.
487
+ * If you set a number it is miliseconds, if you set a string it is a human-readable
488
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that
489
+ * there is no time-to-live.
490
+ *
491
+ * [Learn more about time-to-live](https://cacheable.org/docs/cacheable/#shorthand-for-time-to-live-ttl).
492
+ *
493
+ * @param {number | string | undefined} ttl The time-to-live for the cacheable instance
494
+ * @example
495
+ * ```typescript
496
+ * const cacheable = new Cacheable();
497
+ * cacheable.ttl = '1h'; // Set the time-to-live to 1 hour
498
+ * ```
499
+ * or setting the time-to-live in milliseconds
500
+ * ```typescript
501
+ * const cacheable = new Cacheable();
502
+ * cacheable.ttl = 3600000; // Set the time-to-live to 1 hour
503
+ * ```
504
+ */
180
505
  set ttl(ttl: number | string | undefined);
506
+ /**
507
+ * Sets the primary store for the cacheable instance
508
+ * @param {Keyv | KeyvStoreAdapter} primary The primary store for the cacheable instance
509
+ * @returns {void}
510
+ */
181
511
  setPrimary(primary: Keyv | KeyvStoreAdapter): void;
512
+ /**
513
+ * Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
514
+ * @param {Keyv | KeyvStoreAdapter} secondary The secondary store for the cacheable instance
515
+ * @returns {void}
516
+ */
182
517
  setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
518
+ /**
519
+ * Gets the value of the key. If the key does not exist in the primary store then it will check the secondary store.
520
+ * @param {string} key The key to get the value of
521
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
522
+ */
183
523
  get<T>(key: string): Promise<T | undefined>;
524
+ /**
525
+ * Gets the values of the keys. If the key does not exist in the primary store then it will check the secondary store.
526
+ * @param {string[]} keys The keys to get the values of
527
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
528
+ */
184
529
  getMany<T>(keys: string[]): Promise<Array<T | undefined>>;
185
- set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
530
+ /**
531
+ * Sets the value of the key. If the secondary store is set then it will also set the value in the secondary store.
532
+ * @param {string} key the key to set the value of
533
+ * @param {T} value The value to set
534
+ * @param {number | string} [ttl] Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
535
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
536
+ * undefined then it will not have a time-to-live.
537
+ * @returns {boolean} Whether the value was set
538
+ */
539
+ set<T>(key: string, value: T, ttl?: number | string): Promise<boolean>;
540
+ /**
541
+ * Sets the values of the keys. If the secondary store is set then it will also set the values in the secondary store.
542
+ * @param {CacheableItem[]} items The items to set
543
+ * @returns {boolean} Whether the values were set
544
+ */
186
545
  setMany(items: CacheableItem[]): Promise<boolean>;
546
+ /**
547
+ * Takes the value of the key and deletes the key. If the key does not exist then it will return undefined.
548
+ * @param {string} key The key to take the value of
549
+ * @returns {Promise<T | undefined>} The value of the key or undefined if the key does not exist
550
+ */
187
551
  take<T>(key: string): Promise<T | undefined>;
552
+ /**
553
+ * Takes the values of the keys and deletes the keys. If the key does not exist then it will return undefined.
554
+ * @param {string[]} keys The keys to take the values of
555
+ * @returns {Promise<Array<T | undefined>>} The values of the keys or undefined if the key does not exist
556
+ */
188
557
  takeMany<T>(keys: string[]): Promise<Array<T | undefined>>;
558
+ /**
559
+ * Checks if the key exists in the primary store. If it does not exist then it will check the secondary store.
560
+ * @param {string} key The key to check
561
+ * @returns {Promise<boolean>} Whether the key exists
562
+ */
189
563
  has(key: string): Promise<boolean>;
564
+ /**
565
+ * Checks if the keys exist in the primary store. If it does not exist then it will check the secondary store.
566
+ * @param {string[]} keys The keys to check
567
+ * @returns {Promise<boolean[]>} Whether the keys exist
568
+ */
190
569
  hasMany(keys: string[]): Promise<boolean[]>;
570
+ /**
571
+ * Deletes the key from the primary store. If the secondary store is set then it will also delete the key from the secondary store.
572
+ * @param {string} key The key to delete
573
+ * @returns {Promise<boolean>} Whether the key was deleted
574
+ */
191
575
  delete(key: string): Promise<boolean>;
576
+ /**
577
+ * Deletes the keys from the primary store. If the secondary store is set then it will also delete the keys from the secondary store.
578
+ * @param {string[]} keys The keys to delete
579
+ * @returns {Promise<boolean>} Whether the keys were deleted
580
+ */
192
581
  deleteMany(keys: string[]): Promise<boolean>;
582
+ /**
583
+ * Clears the primary store. If the secondary store is set then it will also clear the secondary store.
584
+ * @returns {Promise<void>}
585
+ */
193
586
  clear(): Promise<void>;
587
+ /**
588
+ * Disconnects the primary store. If the secondary store is set then it will also disconnect the secondary store.
589
+ * @returns {Promise<void>}
590
+ */
194
591
  disconnect(): Promise<void>;
592
+ /**
593
+ * Wraps a function with caching
594
+ *
595
+ * [Learn more about wrapping functions](https://cacheable.org/docs/cacheable/#wrap--memoization-for-sync-and-async-functions).
596
+ * @param {Function} function_ The function to wrap
597
+ * @param {WrapOptions} [options] The options for the wrap function
598
+ * @returns {Function} The wrapped function
599
+ */
600
+ wrap<T>(function_: (...arguments_: any[]) => T, options?: {
601
+ ttl?: number;
602
+ key?: string;
603
+ }): (...arguments_: any[]) => T;
604
+ /**
605
+ * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
606
+ * @param {any} object the object to hash
607
+ * @param {string} algorithm the hash algorithm to use. The default is 'sha256'
608
+ * @returns {string} the hash of the object
609
+ */
610
+ hash(object: any, algorithm?: string): string;
195
611
  private deleteManyKeyv;
196
612
  private setManyKeyv;
197
613
  private hasManyKeyv;
198
614
  private setTtl;
199
615
  }
200
616
 
201
- export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats, KeyvCacheableMemory, shorthandToMilliseconds, shorthandToTime };
617
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync };