cacheable 1.9.0 → 1.10.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.cts CHANGED
@@ -109,6 +109,10 @@ type CacheableStoreItem = {
109
109
  expires?: number;
110
110
  };
111
111
 
112
+ type GetOrSetFunctionOptions = {
113
+ ttl?: number | string;
114
+ cacheErrors?: boolean;
115
+ };
112
116
  type WrapFunctionOptions = {
113
117
  ttl?: number | string;
114
118
  keyPrefix?: string;
@@ -125,20 +129,30 @@ type AnyFunction = (...arguments_: any[]) => any;
125
129
  declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
126
130
  declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
127
131
 
132
+ declare enum StoreHashAlgorithm {
133
+ SHA256 = "sha256",
134
+ SHA1 = "sha1",
135
+ MD5 = "md5",
136
+ djb2Hash = "djb2Hash"
137
+ }
138
+ type StoreHashAlgorithmFunction = ((key: string, storeHashSize: number) => number);
128
139
  /**
129
140
  * @typedef {Object} CacheableMemoryOptions
130
141
  * @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
131
142
  * 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
132
143
  * undefined then it will not have a time-to-live.
133
144
  * @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
134
- * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
145
+ * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
135
146
  * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
147
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
136
148
  */
137
149
  type CacheableMemoryOptions = {
138
150
  ttl?: number | string;
139
151
  useClone?: boolean;
140
152
  lruSize?: number;
141
153
  checkInterval?: number;
154
+ storeHashSize?: number;
155
+ storeHashAlgorithm?: StoreHashAlgorithm | ((key: string, storeHashSize: number) => number);
142
156
  };
143
157
  type SetOptions = {
144
158
  ttl?: number | string;
@@ -146,17 +160,9 @@ type SetOptions = {
146
160
  };
147
161
  declare class CacheableMemory extends Hookified {
148
162
  private _lru;
149
- private readonly _hashCache;
150
- private readonly _hash0;
151
- private readonly _hash1;
152
- private readonly _hash2;
153
- private readonly _hash3;
154
- private readonly _hash4;
155
- private readonly _hash5;
156
- private readonly _hash6;
157
- private readonly _hash7;
158
- private readonly _hash8;
159
- private readonly _hash9;
163
+ private _storeHashSize;
164
+ private _storeHashAlgorithm;
165
+ private _store;
160
166
  private _ttl;
161
167
  private _useClone;
162
168
  private _lruSize;
@@ -189,12 +195,12 @@ declare class CacheableMemory extends Hookified {
189
195
  set useClone(value: boolean);
190
196
  /**
191
197
  * Gets the size of the LRU cache
192
- * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
198
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
193
199
  */
194
200
  get lruSize(): number;
195
201
  /**
196
202
  * Sets the size of the LRU cache
197
- * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
203
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
198
204
  */
199
205
  set lruSize(value: number);
200
206
  /**
@@ -212,6 +218,26 @@ declare class CacheableMemory extends Hookified {
212
218
  * @returns {number} - The size of the cache
213
219
  */
214
220
  get size(): number;
221
+ /**
222
+ * Gets the number of hash stores
223
+ * @returns {number} - The number of hash stores
224
+ */
225
+ get storeHashSize(): number;
226
+ /**
227
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
228
+ * @param {number} value - The number of hash stores
229
+ */
230
+ set storeHashSize(value: number);
231
+ /**
232
+ * Gets the store hash algorithm
233
+ * @returns {StoreHashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
234
+ */
235
+ get storeHashAlgorithm(): StoreHashAlgorithm | StoreHashAlgorithmFunction;
236
+ /**
237
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
238
+ * @param {StoreHashAlgorithm | StoreHashAlgorithmFunction} value - The store hash algorithm
239
+ */
240
+ set storeHashAlgorithm(value: StoreHashAlgorithm | StoreHashAlgorithmFunction);
215
241
  /**
216
242
  * Gets the keys
217
243
  * @returns {IterableIterator<string>} - The keys
@@ -222,6 +248,11 @@ declare class CacheableMemory extends Hookified {
222
248
  * @returns {IterableIterator<CacheableStoreItem>} - The items
223
249
  */
224
250
  get items(): IterableIterator<CacheableStoreItem>;
251
+ /**
252
+ * Gets the store
253
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
254
+ */
255
+ get store(): Array<Map<string, CacheableStoreItem>>;
225
256
  /**
226
257
  * Gets the value of the key
227
258
  * @param {string} key - The key to get the value
@@ -310,17 +341,12 @@ declare class CacheableMemory extends Hookified {
310
341
  */
311
342
  getStore(key: string): Map<string, CacheableStoreItem>;
312
343
  /**
313
- * Get the store based on the hash (internal use)
314
- * @param {number} hash
315
- * @returns {Map<string, CacheableStoreItem>}
344
+ * Hash the key for which store to go to (internal use)
345
+ * @param {string} key - The key to hash
346
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
347
+ * @returns {number} - The hashed key as a number
316
348
  */
317
- getStoreFromHash(hash: number): Map<string, CacheableStoreItem>;
318
- /**
319
- * Hash the key (internal use)
320
- * @param key
321
- * @returns {number} from 0 to 9
322
- */
323
- hashKey(key: string): number;
349
+ getKeyStoreHash(key: string): number;
324
350
  /**
325
351
  * Clone the value. This is for internal use
326
352
  * @param {any} value - The value to clone
@@ -340,7 +366,7 @@ declare class CacheableMemory extends Hookified {
340
366
  */
341
367
  lruMoveToFront(key: string): void;
342
368
  /**
343
- * Resize the LRU cache. This is for internal use
369
+ * Resize the LRU cache. This is for internal use.
344
370
  * @returns {void}
345
371
  */
346
372
  lruResize(): void;
@@ -359,13 +385,6 @@ declare class CacheableMemory extends Hookified {
359
385
  * @returns {void}
360
386
  */
361
387
  stopIntervalCheck(): void;
362
- /**
363
- * Hash the object. This is for internal use
364
- * @param {any} object - The object to hash
365
- * @param {string} [algorithm='sha256'] - The algorithm to hash
366
- * @returns {string} - The hashed string
367
- */
368
- hash(object: any, algorithm?: string): string;
369
388
  /**
370
389
  * Wrap the function for caching
371
390
  * @param {Function} function_ - The function to wrap
@@ -374,8 +393,8 @@ declare class CacheableMemory extends Hookified {
374
393
  */
375
394
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
376
395
  private isPrimitive;
377
- private concatStores;
378
396
  private setTtl;
397
+ private hasExpired;
379
398
  }
380
399
 
381
400
  type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
@@ -704,6 +723,16 @@ declare class Cacheable extends Hookified {
704
723
  * @returns {Function} The wrapped function
705
724
  */
706
725
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
726
+ /**
727
+ * Retrieves the value associated with the given key from the cache. If the key is not found,
728
+ * invokes the provided function to calculate the value, stores it in the cache, and then returns it.
729
+ *
730
+ * @param {string} key - The key to retrieve or set in the cache.
731
+ * @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
732
+ * @param {WrapFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
733
+ * @return {Promise<T | undefined>} - A promise that resolves to the cached or newly computed value, or undefined if an error occurs and caching is not configured for errors.
734
+ */
735
+ getOrSet<T>(key: string, function_: () => Promise<T>, options?: GetOrSetFunctionOptions): Promise<T | undefined>;
707
736
  /**
708
737
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
709
738
  * @param {any} object the object to hash
package/dist/index.d.ts CHANGED
@@ -109,6 +109,10 @@ type CacheableStoreItem = {
109
109
  expires?: number;
110
110
  };
111
111
 
112
+ type GetOrSetFunctionOptions = {
113
+ ttl?: number | string;
114
+ cacheErrors?: boolean;
115
+ };
112
116
  type WrapFunctionOptions = {
113
117
  ttl?: number | string;
114
118
  keyPrefix?: string;
@@ -125,20 +129,30 @@ type AnyFunction = (...arguments_: any[]) => any;
125
129
  declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
126
130
  declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
127
131
 
132
+ declare enum StoreHashAlgorithm {
133
+ SHA256 = "sha256",
134
+ SHA1 = "sha1",
135
+ MD5 = "md5",
136
+ djb2Hash = "djb2Hash"
137
+ }
138
+ type StoreHashAlgorithmFunction = ((key: string, storeHashSize: number) => number);
128
139
  /**
129
140
  * @typedef {Object} CacheableMemoryOptions
130
141
  * @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
131
142
  * 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
132
143
  * undefined then it will not have a time-to-live.
133
144
  * @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
134
- * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
145
+ * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
135
146
  * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
147
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
136
148
  */
137
149
  type CacheableMemoryOptions = {
138
150
  ttl?: number | string;
139
151
  useClone?: boolean;
140
152
  lruSize?: number;
141
153
  checkInterval?: number;
154
+ storeHashSize?: number;
155
+ storeHashAlgorithm?: StoreHashAlgorithm | ((key: string, storeHashSize: number) => number);
142
156
  };
143
157
  type SetOptions = {
144
158
  ttl?: number | string;
@@ -146,17 +160,9 @@ type SetOptions = {
146
160
  };
147
161
  declare class CacheableMemory extends Hookified {
148
162
  private _lru;
149
- private readonly _hashCache;
150
- private readonly _hash0;
151
- private readonly _hash1;
152
- private readonly _hash2;
153
- private readonly _hash3;
154
- private readonly _hash4;
155
- private readonly _hash5;
156
- private readonly _hash6;
157
- private readonly _hash7;
158
- private readonly _hash8;
159
- private readonly _hash9;
163
+ private _storeHashSize;
164
+ private _storeHashAlgorithm;
165
+ private _store;
160
166
  private _ttl;
161
167
  private _useClone;
162
168
  private _lruSize;
@@ -189,12 +195,12 @@ declare class CacheableMemory extends Hookified {
189
195
  set useClone(value: boolean);
190
196
  /**
191
197
  * Gets the size of the LRU cache
192
- * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
198
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
193
199
  */
194
200
  get lruSize(): number;
195
201
  /**
196
202
  * Sets the size of the LRU cache
197
- * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
203
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
198
204
  */
199
205
  set lruSize(value: number);
200
206
  /**
@@ -212,6 +218,26 @@ declare class CacheableMemory extends Hookified {
212
218
  * @returns {number} - The size of the cache
213
219
  */
214
220
  get size(): number;
221
+ /**
222
+ * Gets the number of hash stores
223
+ * @returns {number} - The number of hash stores
224
+ */
225
+ get storeHashSize(): number;
226
+ /**
227
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
228
+ * @param {number} value - The number of hash stores
229
+ */
230
+ set storeHashSize(value: number);
231
+ /**
232
+ * Gets the store hash algorithm
233
+ * @returns {StoreHashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
234
+ */
235
+ get storeHashAlgorithm(): StoreHashAlgorithm | StoreHashAlgorithmFunction;
236
+ /**
237
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
238
+ * @param {StoreHashAlgorithm | StoreHashAlgorithmFunction} value - The store hash algorithm
239
+ */
240
+ set storeHashAlgorithm(value: StoreHashAlgorithm | StoreHashAlgorithmFunction);
215
241
  /**
216
242
  * Gets the keys
217
243
  * @returns {IterableIterator<string>} - The keys
@@ -222,6 +248,11 @@ declare class CacheableMemory extends Hookified {
222
248
  * @returns {IterableIterator<CacheableStoreItem>} - The items
223
249
  */
224
250
  get items(): IterableIterator<CacheableStoreItem>;
251
+ /**
252
+ * Gets the store
253
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
254
+ */
255
+ get store(): Array<Map<string, CacheableStoreItem>>;
225
256
  /**
226
257
  * Gets the value of the key
227
258
  * @param {string} key - The key to get the value
@@ -310,17 +341,12 @@ declare class CacheableMemory extends Hookified {
310
341
  */
311
342
  getStore(key: string): Map<string, CacheableStoreItem>;
312
343
  /**
313
- * Get the store based on the hash (internal use)
314
- * @param {number} hash
315
- * @returns {Map<string, CacheableStoreItem>}
344
+ * Hash the key for which store to go to (internal use)
345
+ * @param {string} key - The key to hash
346
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
347
+ * @returns {number} - The hashed key as a number
316
348
  */
317
- getStoreFromHash(hash: number): Map<string, CacheableStoreItem>;
318
- /**
319
- * Hash the key (internal use)
320
- * @param key
321
- * @returns {number} from 0 to 9
322
- */
323
- hashKey(key: string): number;
349
+ getKeyStoreHash(key: string): number;
324
350
  /**
325
351
  * Clone the value. This is for internal use
326
352
  * @param {any} value - The value to clone
@@ -340,7 +366,7 @@ declare class CacheableMemory extends Hookified {
340
366
  */
341
367
  lruMoveToFront(key: string): void;
342
368
  /**
343
- * Resize the LRU cache. This is for internal use
369
+ * Resize the LRU cache. This is for internal use.
344
370
  * @returns {void}
345
371
  */
346
372
  lruResize(): void;
@@ -359,13 +385,6 @@ declare class CacheableMemory extends Hookified {
359
385
  * @returns {void}
360
386
  */
361
387
  stopIntervalCheck(): void;
362
- /**
363
- * Hash the object. This is for internal use
364
- * @param {any} object - The object to hash
365
- * @param {string} [algorithm='sha256'] - The algorithm to hash
366
- * @returns {string} - The hashed string
367
- */
368
- hash(object: any, algorithm?: string): string;
369
388
  /**
370
389
  * Wrap the function for caching
371
390
  * @param {Function} function_ - The function to wrap
@@ -374,8 +393,8 @@ declare class CacheableMemory extends Hookified {
374
393
  */
375
394
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
376
395
  private isPrimitive;
377
- private concatStores;
378
396
  private setTtl;
397
+ private hasExpired;
379
398
  }
380
399
 
381
400
  type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
@@ -704,6 +723,16 @@ declare class Cacheable extends Hookified {
704
723
  * @returns {Function} The wrapped function
705
724
  */
706
725
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
726
+ /**
727
+ * Retrieves the value associated with the given key from the cache. If the key is not found,
728
+ * invokes the provided function to calculate the value, stores it in the cache, and then returns it.
729
+ *
730
+ * @param {string} key - The key to retrieve or set in the cache.
731
+ * @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
732
+ * @param {WrapFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
733
+ * @return {Promise<T | undefined>} - A promise that resolves to the cached or newly computed value, or undefined if an error occurs and caching is not configured for errors.
734
+ */
735
+ getOrSet<T>(key: string, function_: () => Promise<T>, options?: GetOrSetFunctionOptions): Promise<T | undefined>;
707
736
  /**
708
737
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
709
738
  * @param {any} object the object to hash