LRU (Least Recently Used) Cache data structure
A cache that stores key-value pairs with a fixed capacity. When the cache is full, it evicts the least recently used item.
Time Complexity:
const cache = new LRUCache<number, string>(2);cache.put(1, 'one');cache.put(2, 'two');cache.get(1); // 'one'cache.put(3, 'three'); // evicts key 2cache.get(2); // nullcache.get(3); // 'three' Copy
const cache = new LRUCache<number, string>(2);cache.put(1, 'one');cache.put(2, 'two');cache.get(1); // 'one'cache.put(3, 'three'); // evicts key 2cache.get(2); // nullcache.get(3); // 'three'
Creates a new LRU Cache with the specified capacity
Maximum number of items the cache can hold
Private
Adds a node to the front of the list
Clears all items from the cache
Removes a key from the cache
The key to remove
true if the key was removed, false if it didn't exist
Evicts the least recently used item (tail of the list)
Gets the value associated with the key Marks the key as recently used
The key to look up
The value associated with the key, or null if not found
Checks if a key exists in the cache Does not mark the key as recently used
The key to check
true if the key exists, false otherwise
Returns an array of all keys in the cache, ordered from most to least recently used
Array of keys
Moves a node to the front of the list (most recently used position)
Puts a key-value pair into the cache If the key already exists, updates the value If the cache is at capacity, evicts the least recently used item
The key to store
The value to store
Removes a node from the list
Returns the current size of the cache
The number of items in the cache
Returns an array of all values in the cache, ordered from most to least recently used
Array of values
Generated using TypeDoc
LRU (Least Recently Used) Cache data structure
A cache that stores key-value pairs with a fixed capacity. When the cache is full, it evicts the least recently used item.
Time Complexity:
Example