|
| 1 | +import { |
| 2 | + Keyv, type KeyvOptions, type KeyvStoreAdapter, type StoredData, |
| 3 | +} from 'keyv'; |
| 4 | +import {CacheableMemory, type CacheableMemoryOptions} from './memory.js'; |
| 5 | + |
| 6 | +export type KeyvCacheableMemoryOptions = CacheableMemoryOptions & { |
| 7 | + namespace?: string; |
| 8 | +}; |
| 9 | + |
| 10 | +export class KeyvCacheableMemory implements KeyvStoreAdapter { |
| 11 | + opts: CacheableMemoryOptions = { |
| 12 | + ttl: 0, |
| 13 | + useClone: true, |
| 14 | + lruSize: 0, |
| 15 | + checkInterval: 0, |
| 16 | + }; |
| 17 | + |
| 18 | + private readonly _defaultCache = new CacheableMemory(); |
| 19 | + private readonly _nCache = new Map<string, CacheableMemory>(); |
| 20 | + private _namespace?: string; |
| 21 | + |
| 22 | + constructor(options?: KeyvCacheableMemoryOptions) { |
| 23 | + if (options) { |
| 24 | + this.opts = options; |
| 25 | + this._defaultCache = new CacheableMemory(options); |
| 26 | + |
| 27 | + if (options.namespace) { |
| 28 | + this._namespace = options.namespace; |
| 29 | + this._nCache.set(this._namespace, new CacheableMemory(options)); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + get namespace(): string | undefined { |
| 35 | + return this._namespace; |
| 36 | + } |
| 37 | + |
| 38 | + set namespace(value: string | undefined) { |
| 39 | + this._namespace = value; |
| 40 | + } |
| 41 | + |
| 42 | + public get store(): CacheableMemory { |
| 43 | + return this.getStore(this._namespace); |
| 44 | + } |
| 45 | + |
| 46 | + async get<Value>(key: string): Promise<StoredData<Value> | undefined> { |
| 47 | + const result = this.getStore(this._namespace).get<Value>(key); |
| 48 | + if (result) { |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + |
| 55 | + async getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>> { |
| 56 | + const result = this.getStore(this._namespace).getMany<Value>(keys); |
| 57 | + |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + async set(key: string, value: any, ttl?: number): Promise<void> { |
| 62 | + this.getStore(this._namespace).set(key, value, ttl); |
| 63 | + } |
| 64 | + |
| 65 | + async setMany(values: Array<{key: string; value: any; ttl?: number}>): Promise<void> { |
| 66 | + this.getStore(this._namespace).setMany(values); |
| 67 | + } |
| 68 | + |
| 69 | + async delete(key: string): Promise<boolean> { |
| 70 | + this.getStore(this._namespace).delete(key); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + async deleteMany?(key: string[]): Promise<boolean> { |
| 75 | + this.getStore(this._namespace).deleteMany(key); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + async clear(): Promise<void> { |
| 80 | + this.getStore(this._namespace).clear(); |
| 81 | + } |
| 82 | + |
| 83 | + async has?(key: string): Promise<boolean> { |
| 84 | + return this.getStore(this._namespace).has(key); |
| 85 | + } |
| 86 | + |
| 87 | + on(event: string, listener: (...arguments_: any[]) => void): this { |
| 88 | + this.getStore(this._namespace).on(event, listener); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public getStore(namespace?: string): CacheableMemory { |
| 93 | + if (!namespace) { |
| 94 | + return this._defaultCache; |
| 95 | + } |
| 96 | + |
| 97 | + if (!this._nCache.has(namespace)) { |
| 98 | + this._nCache.set(namespace, new CacheableMemory(this.opts)); |
| 99 | + } |
| 100 | + |
| 101 | + return this._nCache.get(namespace)!; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Creates a new Keyv instance with a new KeyvCacheableMemory store. This also removes the serialize/deserialize methods from the Keyv instance for optimization. |
| 107 | + * @param options |
| 108 | + * @returns |
| 109 | + */ |
| 110 | +export function createKeyv(options?: KeyvCacheableMemoryOptions): Keyv { |
| 111 | + const store = new KeyvCacheableMemory(options); |
| 112 | + const namespace = options?.namespace; |
| 113 | + |
| 114 | + let ttl; |
| 115 | + if (options?.ttl && Number.isInteger(options.ttl)) { |
| 116 | + ttl = options?.ttl as number; |
| 117 | + } |
| 118 | + |
| 119 | + const keyv = new Keyv({store, namespace, ttl}); |
| 120 | + // Remove seriazlize/deserialize |
| 121 | + keyv.serialize = undefined; |
| 122 | + keyv.deserialize = undefined; |
| 123 | + return keyv; |
| 124 | +} |
0 commit comments