| 2 | |
| 3 | /** In-memory cache adapter with time-based expiration. Used as the default result cache. */ |
| 4 | export class MemoryCacheAdapter implements CacheAdapter { |
| 5 | readonly #data = new Map<string, { data: any; expiration: number }>(); |
| 6 | readonly #options: { expiration: number }; |
| 7 | |
| 8 | constructor(options: { expiration: number }) { |
| 9 | this.#options = options; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * @inheritDoc |
| 14 | */ |
| 15 | get<T = any>(name: string): T | undefined { |
| 16 | const data = this.#data.get(name); |
| 17 | |
| 18 | if (data) { |
| 19 | if (data.expiration < Date.now()) { |
| 20 | this.#data.delete(name); |
| 21 | } else { |
| 22 | return data.data; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | return undefined; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @inheritDoc |
| 31 | */ |
| 32 | set(name: string, data: any, origin: string, expiration?: number): void { |
| 33 | this.#data.set(name, { data, expiration: Date.now() + (expiration ?? this.#options.expiration) }); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @inheritDoc |
| 38 | */ |
| 39 | remove(name: string): void { |
| 40 | this.#data.delete(name); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | */ |
| 46 | clear(): void { |
| 47 | this.#data.clear(); |
| 48 | } |
| 49 | } |
nothing calls this directly
no outgoing calls
no test coverage detected