import { Collection } from '../utils/collection.ts' import type { ICacheAdapter } from './adapter.ts' /** Default Cache Adapter for in-memory caching. */ export class DefaultCacheAdapter implements ICacheAdapter { data: { [name: string]: Collection } = {} async get(cacheName: string, key: string): Promise { const cache = this.data[cacheName] if (cache === undefined) return return cache.get(key) } async set( cacheName: string, key: string, value: any, expire?: number ): Promise { let cache = this.data[cacheName] if (cache === undefined) { this.data[cacheName] = new Collection() cache = this.data[cacheName] } cache.set(key, value) if (expire !== undefined) setTimeout(() => { cache.delete(key) }, expire) } async delete(cacheName: string, key: string): Promise { const cache = this.data[cacheName] if (cache === undefined) return false return cache.delete(key) } async array(cacheName: string): Promise { const cache = this.data[cacheName] if (cache === undefined) return return cache.array() } async deleteCache(cacheName: string): Promise { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete return delete this.data[cacheName] } }