Skip to content

Commit

Permalink
refactor: Improve LRU cache handling and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Dec 11, 2024
1 parent 4df861c commit 188278b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class LRUCache<K, V> {
this.cache.delete(key);
} else if (this.cache.size >= this.maxSize) {
const firstKey = this.cache.keys().next().value;
this.cache.delete(firstKey);
if (firstKey !== undefined) {
this.cache.delete(firstKey);
}
}
this.cache.set(key, value);
}
Expand All @@ -41,11 +43,11 @@ interface CacheMetadata {

const DEFAULT_CACHE_SIZE = 100;
export interface ICache {
getAudioBuffer(context: AudioContext, url: string): Promise<AudioBuffer>;
clearMemoryCache(): void;
getAudioBuffer(context: AudioContext, url: string): Promise<AudioBuffer>;
clearMemoryCache(): void;
}

export class AudioCache implements ICache {
export class AudioCache implements ICache {
private static pendingRequests = new Map<string, Promise<AudioBuffer>>();
private static decodedBuffers = new LRUCache<string, AudioBuffer>(
DEFAULT_CACHE_SIZE
Expand Down

0 comments on commit 188278b

Please sign in to comment.