Skip to content

Commit

Permalink
adding in as private _namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Oct 26, 2024
1 parent e13113a commit bedab1c
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions packages/cacheable/src/keyv-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,43 @@ export type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
};

export class KeyvCacheableMemory implements KeyvStoreAdapter {

private _defaultCache = new CacheableMemory();
private _nCache = new Map<string, CacheableMemory>();

opts: CacheableMemoryOptions = {
ttl: 0,
useClone: true,
lruSize: 0,
checkInterval: 0,
};

namespace?: string;
private readonly _defaultCache = new CacheableMemory();
private readonly _nCache = new Map<string, CacheableMemory>();
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<Value>(key: string): Promise<StoredData<Value> | undefined> {
const result = this.getStore(this.namespace).get<Value>(key);
const result = this.getStore(this._namespace).get<Value>(key);
if (result) {
return result;
}
Expand All @@ -45,50 +51,50 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter {
}

async getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>> {
const result = this.getStore(this.namespace).getMany<Value>(keys);
const result = this.getStore(this._namespace).getMany<Value>(keys);

return result;
}

async set(key: string, value: any, ttl?: number): Promise<void> {
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<void> {
this.getStore(this.namespace).setMany(values);
this.getStore(this._namespace).setMany(values);
}

async delete(key: string): Promise<boolean> {
this.getStore(this.namespace).delete(key);
this.getStore(this._namespace).delete(key);
return true;
}

async deleteMany?(key: string[]): Promise<boolean> {
this.getStore(this.namespace).deleteMany(key);
this.getStore(this._namespace).deleteMany(key);
return true;
}

async clear(): Promise<void> {
this.getStore(this.namespace).clear();
this.getStore(this._namespace).clear();
}

async has?(key: string): Promise<boolean> {
return this.getStore(this.namespace).has(key);
return this.getStore(this._namespace).has(key);
}

on(event: string, listener: (...arguments_: any[]) => void): this {
return this;
}

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)!;
}
}

0 comments on commit bedab1c

Please sign in to comment.