Created
December 4, 2025 15:21
-
-
Save amsheehan/b022121f258bee72be25193f5aa458e1 to your computer and use it in GitHub Desktop.
LRU Cache in Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LRUCache<K, V> { | |
| private cache: Map<K, V>; | |
| private capacity: number; | |
| constructor(capacity: number) { | |
| this.capacity = capacity; | |
| this.cache = new Map(); | |
| } | |
| /** | |
| * Get a value from the cache | |
| * Marks the key as recently used by moving it to the end | |
| */ | |
| get(key: K): V | undefined { | |
| if (!this.cache.has(key)) { | |
| return undefined; | |
| } | |
| const value = this.cache.get(key)!; | |
| this.cache.delete(key); | |
| this.cache.set(key, value); | |
| return value; | |
| } | |
| /** | |
| * Put a value in the cache | |
| * If at capacity, evicts the least recently used item | |
| */ | |
| put(key: K, value: V): void { | |
| if (this.cache.has(key)) { | |
| this.cache.delete(key); | |
| } | |
| else if (this.cache.size >= this.capacity) { | |
| const firstKey = this.cache.keys().next().value; | |
| this.cache.delete(firstKey); | |
| } | |
| this.cache.set(key, value); | |
| } | |
| /** | |
| * Check if key exists in cache | |
| */ | |
| has(key: K): boolean { | |
| return this.cache.has(key); | |
| } | |
| /** | |
| * Get current size | |
| */ | |
| size(): number { | |
| return this.cache.size; | |
| } | |
| /** | |
| * Display current cache state (in order from least to most recent) | |
| */ | |
| display(): void { | |
| const items = Array.from(this.cache.entries()) | |
| .map(([k, v]) => `${k}: ${v}`) | |
| .join(', '); | |
| } | |
| } | |
| // DEMO // | |
| // | |
| // Create cache with capacity of 3 | |
| const cache = new LRUCache<string, string>(3); | |
| console.log('1. Adding items to cache (capacity: 3)'); | |
| cache.put('user:1', 'Alice'); | |
| cache.display(); // [user:1: Alice] | |
| cache.put('user:2', 'Bob'); | |
| cache.display(); // [user:1: Alice, user:2: Bob] | |
| cache.put('user:3', 'Charlie'); | |
| cache.display(); // [user:1: Alice, user:2: Bob, user:3: Charlie] | |
| console.log('\n2. Cache is full. Adding new item will evict LRU (user:1)'); | |
| cache.put('user:4', 'David'); | |
| cache.display(); // [user:2: Bob, user:3: Charlie, user:4: David] | |
| console.log('\n3. Accessing user:2 makes it most recently used'); | |
| console.log(`Get user:2 = ${cache.get('user:2')}`); | |
| cache.display(); // [user:3: Charlie, user:4: David, user:2: Bob] | |
| console.log('\n4. Adding user:5 will now evict user:3 (new LRU)'); | |
| cache.put('user:5', 'Eve'); | |
| cache.display(); // [user:4: David, user:2: Bob, user:5: Eve] | |
| console.log('\n5. Accessing user:4 and then adding user:6'); | |
| console.log(`Get user:4 = ${cache.get('user:4')}`); | |
| cache.display(); // [user:2: Bob, user:5: Eve, user:4: David] | |
| cache.put('user:6', 'Frank'); | |
| cache.display(); // [user:5: Eve, user:4: David, user:6: Frank] | |
| console.log('\n6. Updating existing key (user:5) moves it to most recent'); | |
| cache.put('user:5', 'Eve Updated'); | |
| cache.display(); // [user:4: David, user:6: Frank, user:5: Eve Updated] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment