@@ -50,6 +50,11 @@ export type CacheableOptions = {
5050 * The namespace for the cacheable instance. It can be a string or a function that returns a string.
5151 */
5252 namespace ?: string | ( ( ) => string ) ;
53+ /**
54+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
55+ * If it is not set then it will be a random string that is generated
56+ */
57+ cacheId ?: string ;
5358} ;
5459
5560export class Cacheable extends Hookified {
@@ -59,6 +64,7 @@ export class Cacheable extends Hookified {
5964 private _ttl ?: number | string ;
6065 private readonly _stats = new CacheableStats ( { enabled : false } ) ;
6166 private _namespace ?: string | ( ( ) => string ) ;
67+ private _cacheId : string = Math . random ( ) . toString ( 36 ) . slice ( 2 ) ;
6268
6369 /**
6470 * Creates a new cacheable instance
@@ -87,6 +93,10 @@ export class Cacheable extends Hookified {
8793 this . setTtl ( options . ttl ) ;
8894 }
8995
96+ if ( options ?. cacheId ) {
97+ this . _cacheId = options . cacheId ;
98+ }
99+
90100 if ( options ?. namespace ) {
91101 this . _namespace = options . namespace ;
92102 this . _primary . namespace = this . getNameSpace ( ) ;
@@ -225,6 +235,24 @@ export class Cacheable extends Hookified {
225235 this . setTtl ( ttl ) ;
226236 }
227237
238+ /**
239+ * The cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
240+ * If it is not set then it will be a random string that is generated
241+ * @returns {string } The cacheId for the cacheable instance
242+ */
243+ public get cacheId ( ) : string {
244+ return this . _cacheId ;
245+ }
246+
247+ /**
248+ * Sets the cacheId for the cacheable instance. This is primarily used for the wrap function to not have conflicts.
249+ * If it is not set then it will be a random string that is generated
250+ * @param {string } cacheId The cacheId for the cacheable instance
251+ */
252+ public set cacheId ( cacheId : string ) {
253+ this . _cacheId = cacheId ;
254+ }
255+
228256 /**
229257 * Sets the primary store for the cacheable instance
230258 * @param {Keyv | KeyvStoreAdapter } primary The primary store for the cacheable instance
@@ -603,6 +631,7 @@ export class Cacheable extends Hookified {
603631 ttl : options ?. ttl ?? this . _ttl ,
604632 keyPrefix : options ?. keyPrefix ,
605633 cache : this ,
634+ cacheId : this . _cacheId ,
606635 } ;
607636
608637 return wrap < T > ( function_ , wrapOptions ) ;
0 commit comments