Files
readest/apps/readest-app/src/utils/lru.ts
T
Huang Xin 00003a9415 Preloading audio for Edge TTS and other fixes (#138)
* Blacklist some low-quality web tts voices

* Dropdown layout tweaks

* Handle no audio data received in Edge TTS

* Preloading audio for Edge TTS
2025-01-10 14:11:09 +01:00

55 lines
1.1 KiB
TypeScript

export class LRUCache<K, V> {
private capacity: number;
private map: Map<K, V>;
constructor(capacity: number) {
if (capacity <= 0) {
throw new Error('LRUCache capacity must be greater than 0');
}
this.capacity = capacity;
this.map = new Map();
}
get(key: K): V | undefined {
if (!this.map.has(key)) {
return undefined;
}
const value = this.map.get(key)!;
this.map.delete(key);
this.map.set(key, value);
return value;
}
set(key: K, value: V): void {
if (this.map.has(key)) {
this.map.delete(key);
} else if (this.map.size === this.capacity) {
const oldestKey = this.map.keys().next().value;
if (oldestKey) {
this.map.delete(oldestKey);
}
}
this.map.set(key, value);
}
has(key: K): boolean {
return this.map.has(key);
}
delete(key: K): boolean {
return this.map.delete(key);
}
clear(): void {
this.map.clear();
}
size(): number {
return this.map.size;
}
entries(): Array<[K, V]> {
return Array.from(this.map).reverse();
}
}