import type { ICache, KeyManifestEntry, MaybePromise, } from './shared' import { CACHE_KEY_PREFIX, } from './shared' export class CacheKeyManifest { private readonly manifestKey: string constructor(private cache: ICache, private clientId: string) { this.manifestKey = this.createManifestKeyFrom(this.clientId) } async add(key: string): Promise { const keys = new Set( (await this.cache.get(this.manifestKey))?.keys || [], ) keys.add(key) await this.cache.set(this.manifestKey, { keys: [...keys], }) } async remove(key: string): Promise { const entry = await this.cache.get(this.manifestKey) if (entry) { const keys = new Set(entry.keys) keys.delete(key) if (keys.size > 0) return await this.cache.set(this.manifestKey, { keys: [...keys] }) return await this.cache.remove(this.manifestKey) } } get(): MaybePromise { return this.cache.get(this.manifestKey) } clear(): MaybePromise { return this.cache.remove(this.manifestKey) } private createManifestKeyFrom(clientId: string): string { return `${CACHE_KEY_PREFIX}::${clientId}` } }