From bedab1c16534e34bad512f6c9b4e177bfec91f06 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Sat, 26 Oct 2024 13:47:10 -0700 Subject: [PATCH] adding in as private _namespace --- packages/cacheable/src/keyv-memory.ts | 48 +++++++++++++++------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/cacheable/src/keyv-memory.ts b/packages/cacheable/src/keyv-memory.ts index 6bc87628..471ed6e6 100644 --- a/packages/cacheable/src/keyv-memory.ts +++ b/packages/cacheable/src/keyv-memory.ts @@ -6,10 +6,6 @@ export type KeyvCacheableMemoryOptions = CacheableMemoryOptions & { }; export class KeyvCacheableMemory implements KeyvStoreAdapter { - - private _defaultCache = new CacheableMemory(); - private _nCache = new Map(); - opts: CacheableMemoryOptions = { ttl: 0, useClone: true, @@ -17,26 +13,36 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter { checkInterval: 0, }; - namespace?: string; + private readonly _defaultCache = new CacheableMemory(); + private readonly _nCache = new Map(); + private _namespace?: string; constructor(options?: KeyvCacheableMemoryOptions) { if (options) { this.opts = options; this._defaultCache = new CacheableMemory(options); - if(options.namespace) { - this.namespace = options.namespace; - this._nCache.set(this.namespace, new CacheableMemory(options)); + if (options.namespace) { + this._namespace = options.namespace; + this._nCache.set(this._namespace, new CacheableMemory(options)); } } } - public get store() : CacheableMemory { - return this.getStore(this.namespace); + get namespace(): string | undefined { + return this._namespace; + } + + set namespace(value: string | undefined) { + this._namespace = value; + } + + public get store(): CacheableMemory { + return this.getStore(this._namespace); } async get(key: string): Promise | undefined> { - const result = this.getStore(this.namespace).get(key); + const result = this.getStore(this._namespace).get(key); if (result) { return result; } @@ -45,35 +51,35 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter { } async getMany(keys: string[]): Promise>> { - const result = this.getStore(this.namespace).getMany(keys); + const result = this.getStore(this._namespace).getMany(keys); return result; } async set(key: string, value: any, ttl?: number): Promise { - this.getStore(this.namespace).set(key, value, ttl); + this.getStore(this._namespace).set(key, value, ttl); } async setMany(values: Array<{key: string; value: any; ttl?: number}>): Promise { - this.getStore(this.namespace).setMany(values); + this.getStore(this._namespace).setMany(values); } async delete(key: string): Promise { - this.getStore(this.namespace).delete(key); + this.getStore(this._namespace).delete(key); return true; } async deleteMany?(key: string[]): Promise { - this.getStore(this.namespace).deleteMany(key); + this.getStore(this._namespace).deleteMany(key); return true; } async clear(): Promise { - this.getStore(this.namespace).clear(); + this.getStore(this._namespace).clear(); } async has?(key: string): Promise { - return this.getStore(this.namespace).has(key); + return this.getStore(this._namespace).has(key); } on(event: string, listener: (...arguments_: any[]) => void): this { @@ -81,14 +87,14 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter { } public getStore(namespace?: string): CacheableMemory { - if(!namespace) { + if (!namespace) { return this._defaultCache; } - + if (!this._nCache.has(namespace)) { this._nCache.set(namespace, new CacheableMemory(this.opts)); } - return this._nCache.get(namespace) as CacheableMemory; + return this._nCache.get(namespace)!; } }