A simple LRU cache implementation that uses a doubly linked list to track the order of items in an hash map.
When instatiating the cache, you can optionally specify the type of the key and value, as well as the maximum size of the cache. If no maximum size is specified, the cache will default to a size of 100.
Optional
config: LRUCacheOptionsThe configuration options for the cache
A simple LRU cache implementation that uses a doubly linked list to track the order of items in an hash map.
Illustration of the design:
Items are added to the cache using the
add()
method. When an item is added, it's marked as the most recently used item. If the cache is full, the oldest item is removed from the cache.Each item also tracks the item that was added before it, and the item that was added after it. This allows us to quickly remove the oldest item from the cache without having to iterate through the entire cache.
Note: This implementation is loosely based on the implementation found in the lru_map package which is licensed under the MIT license and recommends users to copy the code into their own projects.