| 1 | export class LruStringCache { |
| 2 | private readonly map = new Map<string, string>(); |
| 3 | |
| 4 | constructor(private readonly limit: number) { |
| 5 | if (limit <= 0) { |
| 6 | throw new Error(`LruStringCache: limit must be > 0 (got ${limit})`); |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | get(key: string): string | undefined { |
| 11 | const value = this.map.get(key); |
| 12 | if (value === undefined) return undefined; |
| 13 | this.map.delete(key); |
| 14 | this.map.set(key, value); |
| 15 | return value; |
| 16 | } |
| 17 | |
| 18 | set(key: string, value: string): void { |
| 19 | if (this.map.has(key)) this.map.delete(key); |
| 20 | this.map.set(key, value); |
| 21 | while (this.map.size > this.limit) { |
| 22 | const oldestKey = this.map.keys().next().value; |
| 23 | if (oldestKey === undefined) break; |
| 24 | this.map.delete(oldestKey); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | clear(): void { |
| 29 | this.map.clear(); |
| 30 | } |
| 31 | |
| 32 | get size(): number { |
| 33 | return this.map.size; |
| 34 | } |
| 35 | } |
nothing calls this directly
no outgoing calls
no test coverage detected