LRU Cache Visualizer

Set cache capacity, run get and put operations, then inspect the hash map lookup and linked-list recency order from most recently used to least recently used.

Size
3/3
Hits
0
Misses
0
LRU
B

Cache settings

Use a small capacity to make evictions easy to see. The visualizer supports 1 to 10 items.

Manual operation

Operation script

LRU recency list

Sample cache loaded. Run get or put to move keys to the most recently used side.

Active Evicted
A
value: 1
MRU
->
C
value: 3
slot 2
->
B
value: 2
LRU

Hash map lookup

A
1
C
3
B
2

Operation log

Sample cache loaded. Run get or put to move keys to the most recently used side.

What is an LRU Cache Visualizer?

An LRU cache visualizer shows how a least recently used cache keeps fast key lookup while tracking which item was used most recently. When the cache reaches capacity, it evicts the least recently used item.

LRU caches are usually explained with a hash map and a doubly linked list. The hash map gives direct key lookup. The linked list keeps recency order from most recently used to least recently used.

For the full cluster of related tools, browse the Data Structure Visualizers hub.

How to use this LRU cache visualizer

  • Set the cache capacity.
  • Run put(key, value) to add or update an item.
  • Run get(key) to mark an item as recently used.
  • Paste a batch of operations such as put A 1, get A, and put B 2.
  • Watch the hash map, recency list, hits, misses, and eviction output update.

The visual order is most recently used on the left and least recently used on the right.

LRU cache with hash map and linked list

A hash map alone can find keys quickly, but it does not preserve recency order by itself. A linked list can preserve order, but search is slow without a map.

An LRU cache combines both ideas. Compare this page with the Hash Table Visualizer and the Linked List Visualizer to see the two parts separately.

Frequently Asked Questions

What does LRU mean?
LRU means least recently used. When the cache is full, the item that has not been used for the longest time is removed first.
Why does get change the cache order?
A successful get means that key was just used, so it moves to the most recently used position.
What happens when put updates an existing key?
The value is updated and the key moves to the most recently used position.
What is the time complexity of LRU cache get and put?
With a hash map plus linked list, both get and put are usually O(1).

Related tools