import { Adapter } from "./Adapter"; export class Cache { private adapter: Adapter; public static caseSensitive: boolean = false; constructor(adapter: Adapter) { this.adapter = adapter; } private constructKey(key: any): string { if (Array.isArray(key)) { key = key.join(':'); } return key; } public static setCaseSensitivity(value: boolean): boolean { return this.caseSensitive = value; } public async load(key: string | string[], ttl: number = 60 * 60 * 24, hash: string = ''): Promise { key = this.constructKey(key); key = Cache.caseSensitive ? key : key.toLowerCase(); hash = Cache.caseSensitive ? hash : hash.toLowerCase(); return await this.adapter.load(key, ttl, hash); } public async save(key: string | string[], data: any, hash: string = ''): Promise { key = this.constructKey(key); key = Cache.caseSensitive ? key : key.toLowerCase(); hash = Cache.caseSensitive ? hash : hash.toLowerCase(); return this.adapter.save(key, data, hash); } public async list(key: string | string[]): Promise { key = this.constructKey(key); key = Cache.caseSensitive ? key : key.toLowerCase(); return this.adapter.list(key); } public async purge(key: string | string[], hash: string = ''): Promise { key = this.constructKey(key); key = Cache.caseSensitive ? key : key.toLowerCase(); hash = Cache.caseSensitive ? hash : hash.toLowerCase(); return this.adapter.purge(key, hash); } public async flush(): Promise { return this.adapter.flush(); } public async ping(): Promise { return this.adapter.ping(); } public async getSize(): Promise { return this.adapter.getSize(); } public async delWithStart(keys: string | string[]): Promise { const pattern = this.constructKey(keys); return this.adapter.delWithStart(pattern); } }