cacheable 1.9.0 → 1.10.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
@@ -109,9 +109,21 @@ type CacheableStoreItem = {
109
109
  expires?: number;
110
110
  };
111
111
 
112
+ type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
113
+ type GetOrSetFunctionOptions = {
114
+ ttl?: number | string;
115
+ cacheErrors?: boolean;
116
+ throwErrors?: boolean;
117
+ };
118
+ type GetOrSetOptions = GetOrSetFunctionOptions & {
119
+ cacheId?: string;
120
+ cache: Cacheable;
121
+ };
122
+ type CreateWrapKey = (function_: AnyFunction, arguments_: any[], options?: WrapFunctionOptions) => string;
112
123
  type WrapFunctionOptions = {
113
124
  ttl?: number | string;
114
125
  keyPrefix?: string;
126
+ createKey?: CreateWrapKey;
115
127
  cacheErrors?: boolean;
116
128
  cacheId?: string;
117
129
  };
@@ -123,22 +135,33 @@ type WrapSyncOptions = WrapFunctionOptions & {
123
135
  };
124
136
  type AnyFunction = (...arguments_: any[]) => any;
125
137
  declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
138
+ declare function getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options: GetOrSetOptions): Promise<T | undefined>;
126
139
  declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
127
140
 
141
+ declare enum StoreHashAlgorithm {
142
+ SHA256 = "sha256",
143
+ SHA1 = "sha1",
144
+ MD5 = "md5",
145
+ djb2Hash = "djb2Hash"
146
+ }
147
+ type StoreHashAlgorithmFunction = ((key: string, storeHashSize: number) => number);
128
148
  /**
129
149
  * @typedef {Object} CacheableMemoryOptions
130
150
  * @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
151
  * 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
152
  * undefined then it will not have a time-to-live.
133
153
  * @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.
154
+ * @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
155
  * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
156
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
136
157
  */
137
158
  type CacheableMemoryOptions = {
138
159
  ttl?: number | string;
139
160
  useClone?: boolean;
140
161
  lruSize?: number;
141
162
  checkInterval?: number;
163
+ storeHashSize?: number;
164
+ storeHashAlgorithm?: StoreHashAlgorithm | ((key: string, storeHashSize: number) => number);
142
165
  };
143
166
  type SetOptions = {
144
167
  ttl?: number | string;
@@ -146,17 +169,9 @@ type SetOptions = {
146
169
  };
147
170
  declare class CacheableMemory extends Hookified {
148
171
  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;
172
+ private _storeHashSize;
173
+ private _storeHashAlgorithm;
174
+ private _store;
160
175
  private _ttl;
161
176
  private _useClone;
162
177
  private _lruSize;
@@ -189,12 +204,12 @@ declare class CacheableMemory extends Hookified {
189
204
  set useClone(value: boolean);
190
205
  /**
191
206
  * 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.
207
+ * @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
208
  */
194
209
  get lruSize(): number;
195
210
  /**
196
211
  * 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.
212
+ * @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
213
  */
199
214
  set lruSize(value: number);
200
215
  /**
@@ -212,6 +227,26 @@ declare class CacheableMemory extends Hookified {
212
227
  * @returns {number} - The size of the cache
213
228
  */
214
229
  get size(): number;
230
+ /**
231
+ * Gets the number of hash stores
232
+ * @returns {number} - The number of hash stores
233
+ */
234
+ get storeHashSize(): number;
235
+ /**
236
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
237
+ * @param {number} value - The number of hash stores
238
+ */
239
+ set storeHashSize(value: number);
240
+ /**
241
+ * Gets the store hash algorithm
242
+ * @returns {StoreHashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
243
+ */
244
+ get storeHashAlgorithm(): StoreHashAlgorithm | StoreHashAlgorithmFunction;
245
+ /**
246
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
247
+ * @param {StoreHashAlgorithm | StoreHashAlgorithmFunction} value - The store hash algorithm
248
+ */
249
+ set storeHashAlgorithm(value: StoreHashAlgorithm | StoreHashAlgorithmFunction);
215
250
  /**
216
251
  * Gets the keys
217
252
  * @returns {IterableIterator<string>} - The keys
@@ -222,6 +257,11 @@ declare class CacheableMemory extends Hookified {
222
257
  * @returns {IterableIterator<CacheableStoreItem>} - The items
223
258
  */
224
259
  get items(): IterableIterator<CacheableStoreItem>;
260
+ /**
261
+ * Gets the store
262
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
263
+ */
264
+ get store(): Array<Map<string, CacheableStoreItem>>;
225
265
  /**
226
266
  * Gets the value of the key
227
267
  * @param {string} key - The key to get the value
@@ -310,17 +350,12 @@ declare class CacheableMemory extends Hookified {
310
350
  */
311
351
  getStore(key: string): Map<string, CacheableStoreItem>;
312
352
  /**
313
- * Get the store based on the hash (internal use)
314
- * @param {number} hash
315
- * @returns {Map<string, CacheableStoreItem>}
316
- */
317
- getStoreFromHash(hash: number): Map<string, CacheableStoreItem>;
318
- /**
319
- * Hash the key (internal use)
320
- * @param key
321
- * @returns {number} from 0 to 9
353
+ * Hash the key for which store to go to (internal use)
354
+ * @param {string} key - The key to hash
355
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
356
+ * @returns {number} - The hashed key as a number
322
357
  */
323
- hashKey(key: string): number;
358
+ getKeyStoreHash(key: string): number;
324
359
  /**
325
360
  * Clone the value. This is for internal use
326
361
  * @param {any} value - The value to clone
@@ -340,7 +375,7 @@ declare class CacheableMemory extends Hookified {
340
375
  */
341
376
  lruMoveToFront(key: string): void;
342
377
  /**
343
- * Resize the LRU cache. This is for internal use
378
+ * Resize the LRU cache. This is for internal use.
344
379
  * @returns {void}
345
380
  */
346
381
  lruResize(): void;
@@ -359,13 +394,6 @@ declare class CacheableMemory extends Hookified {
359
394
  * @returns {void}
360
395
  */
361
396
  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
397
  /**
370
398
  * Wrap the function for caching
371
399
  * @param {Function} function_ - The function to wrap
@@ -374,8 +402,8 @@ declare class CacheableMemory extends Hookified {
374
402
  */
375
403
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
376
404
  private isPrimitive;
377
- private concatStores;
378
405
  private setTtl;
406
+ private hasExpired;
379
407
  }
380
408
 
381
409
  type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
@@ -592,6 +620,7 @@ declare class Cacheable extends Hookified {
592
620
  * @returns {void}
593
621
  */
594
622
  setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
623
+ isKeyvInstance(keyv: any): boolean;
595
624
  getNameSpace(): string | undefined;
596
625
  /**
597
626
  * Retrieves an entry from the cache, with an optional “raw” mode.
@@ -704,6 +733,17 @@ declare class Cacheable extends Hookified {
704
733
  * @returns {Function} The wrapped function
705
734
  */
706
735
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
736
+ /**
737
+ * Retrieves the value associated with the given key from the cache. If the key is not found,
738
+ * invokes the provided function to calculate the value, stores it in the cache, and then returns it.
739
+ *
740
+ * @param {GetOrSetKey} key - The key to retrieve or set in the cache. This can also be a function that returns a string key.
741
+ * If a function is provided, it will be called with the cache options to generate the key.
742
+ * @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
743
+ * @param {GetOrSetFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
744
+ * @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.
745
+ */
746
+ getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options?: GetOrSetFunctionOptions): Promise<T | undefined>;
707
747
  /**
708
748
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
709
749
  * @param {any} object the object to hash
@@ -719,4 +759,4 @@ declare class Cacheable extends Hookified {
719
759
  private setTtl;
720
760
  }
721
761
 
722
- export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, createKeyv, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync };
762
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, createKeyv, getOrSet, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync };
package/dist/index.d.ts CHANGED
@@ -109,9 +109,21 @@ type CacheableStoreItem = {
109
109
  expires?: number;
110
110
  };
111
111
 
112
+ type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
113
+ type GetOrSetFunctionOptions = {
114
+ ttl?: number | string;
115
+ cacheErrors?: boolean;
116
+ throwErrors?: boolean;
117
+ };
118
+ type GetOrSetOptions = GetOrSetFunctionOptions & {
119
+ cacheId?: string;
120
+ cache: Cacheable;
121
+ };
122
+ type CreateWrapKey = (function_: AnyFunction, arguments_: any[], options?: WrapFunctionOptions) => string;
112
123
  type WrapFunctionOptions = {
113
124
  ttl?: number | string;
114
125
  keyPrefix?: string;
126
+ createKey?: CreateWrapKey;
115
127
  cacheErrors?: boolean;
116
128
  cacheId?: string;
117
129
  };
@@ -123,22 +135,33 @@ type WrapSyncOptions = WrapFunctionOptions & {
123
135
  };
124
136
  type AnyFunction = (...arguments_: any[]) => any;
125
137
  declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
138
+ declare function getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options: GetOrSetOptions): Promise<T | undefined>;
126
139
  declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
127
140
 
141
+ declare enum StoreHashAlgorithm {
142
+ SHA256 = "sha256",
143
+ SHA1 = "sha1",
144
+ MD5 = "md5",
145
+ djb2Hash = "djb2Hash"
146
+ }
147
+ type StoreHashAlgorithmFunction = ((key: string, storeHashSize: number) => number);
128
148
  /**
129
149
  * @typedef {Object} CacheableMemoryOptions
130
150
  * @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
151
  * 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
152
  * undefined then it will not have a time-to-live.
133
153
  * @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.
154
+ * @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
155
  * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
156
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
136
157
  */
137
158
  type CacheableMemoryOptions = {
138
159
  ttl?: number | string;
139
160
  useClone?: boolean;
140
161
  lruSize?: number;
141
162
  checkInterval?: number;
163
+ storeHashSize?: number;
164
+ storeHashAlgorithm?: StoreHashAlgorithm | ((key: string, storeHashSize: number) => number);
142
165
  };
143
166
  type SetOptions = {
144
167
  ttl?: number | string;
@@ -146,17 +169,9 @@ type SetOptions = {
146
169
  };
147
170
  declare class CacheableMemory extends Hookified {
148
171
  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;
172
+ private _storeHashSize;
173
+ private _storeHashAlgorithm;
174
+ private _store;
160
175
  private _ttl;
161
176
  private _useClone;
162
177
  private _lruSize;
@@ -189,12 +204,12 @@ declare class CacheableMemory extends Hookified {
189
204
  set useClone(value: boolean);
190
205
  /**
191
206
  * 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.
207
+ * @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
208
  */
194
209
  get lruSize(): number;
195
210
  /**
196
211
  * 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.
212
+ * @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
213
  */
199
214
  set lruSize(value: number);
200
215
  /**
@@ -212,6 +227,26 @@ declare class CacheableMemory extends Hookified {
212
227
  * @returns {number} - The size of the cache
213
228
  */
214
229
  get size(): number;
230
+ /**
231
+ * Gets the number of hash stores
232
+ * @returns {number} - The number of hash stores
233
+ */
234
+ get storeHashSize(): number;
235
+ /**
236
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
237
+ * @param {number} value - The number of hash stores
238
+ */
239
+ set storeHashSize(value: number);
240
+ /**
241
+ * Gets the store hash algorithm
242
+ * @returns {StoreHashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
243
+ */
244
+ get storeHashAlgorithm(): StoreHashAlgorithm | StoreHashAlgorithmFunction;
245
+ /**
246
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
247
+ * @param {StoreHashAlgorithm | StoreHashAlgorithmFunction} value - The store hash algorithm
248
+ */
249
+ set storeHashAlgorithm(value: StoreHashAlgorithm | StoreHashAlgorithmFunction);
215
250
  /**
216
251
  * Gets the keys
217
252
  * @returns {IterableIterator<string>} - The keys
@@ -222,6 +257,11 @@ declare class CacheableMemory extends Hookified {
222
257
  * @returns {IterableIterator<CacheableStoreItem>} - The items
223
258
  */
224
259
  get items(): IterableIterator<CacheableStoreItem>;
260
+ /**
261
+ * Gets the store
262
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
263
+ */
264
+ get store(): Array<Map<string, CacheableStoreItem>>;
225
265
  /**
226
266
  * Gets the value of the key
227
267
  * @param {string} key - The key to get the value
@@ -310,17 +350,12 @@ declare class CacheableMemory extends Hookified {
310
350
  */
311
351
  getStore(key: string): Map<string, CacheableStoreItem>;
312
352
  /**
313
- * Get the store based on the hash (internal use)
314
- * @param {number} hash
315
- * @returns {Map<string, CacheableStoreItem>}
316
- */
317
- getStoreFromHash(hash: number): Map<string, CacheableStoreItem>;
318
- /**
319
- * Hash the key (internal use)
320
- * @param key
321
- * @returns {number} from 0 to 9
353
+ * Hash the key for which store to go to (internal use)
354
+ * @param {string} key - The key to hash
355
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
356
+ * @returns {number} - The hashed key as a number
322
357
  */
323
- hashKey(key: string): number;
358
+ getKeyStoreHash(key: string): number;
324
359
  /**
325
360
  * Clone the value. This is for internal use
326
361
  * @param {any} value - The value to clone
@@ -340,7 +375,7 @@ declare class CacheableMemory extends Hookified {
340
375
  */
341
376
  lruMoveToFront(key: string): void;
342
377
  /**
343
- * Resize the LRU cache. This is for internal use
378
+ * Resize the LRU cache. This is for internal use.
344
379
  * @returns {void}
345
380
  */
346
381
  lruResize(): void;
@@ -359,13 +394,6 @@ declare class CacheableMemory extends Hookified {
359
394
  * @returns {void}
360
395
  */
361
396
  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
397
  /**
370
398
  * Wrap the function for caching
371
399
  * @param {Function} function_ - The function to wrap
@@ -374,8 +402,8 @@ declare class CacheableMemory extends Hookified {
374
402
  */
375
403
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
376
404
  private isPrimitive;
377
- private concatStores;
378
405
  private setTtl;
406
+ private hasExpired;
379
407
  }
380
408
 
381
409
  type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
@@ -592,6 +620,7 @@ declare class Cacheable extends Hookified {
592
620
  * @returns {void}
593
621
  */
594
622
  setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
623
+ isKeyvInstance(keyv: any): boolean;
595
624
  getNameSpace(): string | undefined;
596
625
  /**
597
626
  * Retrieves an entry from the cache, with an optional “raw” mode.
@@ -704,6 +733,17 @@ declare class Cacheable extends Hookified {
704
733
  * @returns {Function} The wrapped function
705
734
  */
706
735
  wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
736
+ /**
737
+ * Retrieves the value associated with the given key from the cache. If the key is not found,
738
+ * invokes the provided function to calculate the value, stores it in the cache, and then returns it.
739
+ *
740
+ * @param {GetOrSetKey} key - The key to retrieve or set in the cache. This can also be a function that returns a string key.
741
+ * If a function is provided, it will be called with the cache options to generate the key.
742
+ * @param {() => Promise<T>} function_ - The asynchronous function that computes the value to be cached if the key does not exist.
743
+ * @param {GetOrSetFunctionOptions} [options] - Optional settings for caching, such as the time to live (TTL) or whether to cache errors.
744
+ * @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.
745
+ */
746
+ getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options?: GetOrSetFunctionOptions): Promise<T | undefined>;
707
747
  /**
708
748
  * Will hash an object using the specified algorithm. The default algorithm is 'sha256'.
709
749
  * @param {any} object the object to hash
@@ -719,4 +759,4 @@ declare class Cacheable extends Hookified {
719
759
  private setTtl;
720
760
  }
721
761
 
722
- export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, createKeyv, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync };
762
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, createKeyv, getOrSet, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync };