Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cacheable - adding namespace support for KeyvCacheableMemory storage … #863

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions packages/cacheable/src/keyv-memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {type KeyvStoreAdapter, type StoredData} from 'keyv';
import {CacheableMemory, type CacheableMemoryOptions} from './memory.js';

export type KeyvCacheableMemoryOptions = CacheableMemoryOptions & {
namespace?: string;
};

export class KeyvCacheableMemory implements KeyvStoreAdapter {
opts: CacheableMemoryOptions = {
ttl: 0,
Expand All @@ -9,17 +13,36 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter {
checkInterval: 0,
};

namespace?: string | undefined;
private readonly _cache = new CacheableMemory();
constructor(options?: CacheableMemoryOptions) {
private readonly _defaultCache = new CacheableMemory();
private readonly _nCache = new Map<string, CacheableMemory>();
private _namespace?: string;

constructor(options?: KeyvCacheableMemoryOptions) {
if (options) {
this.opts = options;
this._cache = new CacheableMemory(options);
this._defaultCache = new CacheableMemory(options);

if (options.namespace) {
this._namespace = options.namespace;
this._nCache.set(this._namespace, new CacheableMemory(options));
}
}
}

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._cache.get<Value>(key);
const result = this.getStore(this._namespace).get<Value>(key);
if (result) {
return result;
}
Expand All @@ -28,38 +51,50 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter {
}

async getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>> {
const result = this._cache.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._cache.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._cache.setMany(values);
this.getStore(this._namespace).setMany(values);
}

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

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

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

async has?(key: string): Promise<boolean> {
return this._cache.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) {
return this._defaultCache;
}

if (!this._nCache.has(namespace)) {
this._nCache.set(namespace, new CacheableMemory(this.opts));
}

return this._nCache.get(namespace)!;
}
}
21 changes: 21 additions & 0 deletions packages/cacheable/test/keyv-memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ describe('Keyv Cacheable Memory', () => {
const keyv = new Keyv({store: keyvCacheableMemory});
expect(keyv).toBeDefined();
});
test('should set namespace for keyv cacheable memory', async () => {
const namespace = 'ns1';
const keyvCacheableMemory = new KeyvCacheableMemory({namespace});
expect(keyvCacheableMemory.namespace).toBe(namespace);
keyvCacheableMemory.namespace = 'ns2';
expect(keyvCacheableMemory.namespace).toBe('ns2');
});
test('should set options for keyv cacheable memory', async () => {
const keyvCacheableMemory = new KeyvCacheableMemory({ttl: 1000, lruSize: 1000});
expect(keyvCacheableMemory).toBeDefined();
Expand Down Expand Up @@ -72,4 +79,18 @@ describe('Keyv Cacheable Memory', () => {
const value = await keyvCacheableMemory.get('key1');
expect(value).toBe('value1');
});
test('should be able to get the store based on namespace', async () => {
const cache = new KeyvCacheableMemory();
await cache.set('key1', 'default');
expect(await cache.get('key1')).toBe('default');
cache.namespace = 'ns1';
expect(await cache.get('key1')).toBe(undefined);
expect(cache.store.get('key1')).toBe(undefined);
await cache.set('key1', 'ns1');
expect(await cache.get('key1')).toBe('ns1');
expect(cache.store.get('key1')).toBe('ns1');
cache.namespace = undefined;
expect(await cache.get('key1')).toBe('default');
expect(cache.store.get('key1')).toBe('default');
});
});
Loading