Introduction to LRU Cache (original) (raw)

Last Updated : 15 Jun, 2026

Least Recently Used (LRU) is a cache eviction policy that keeps track of how recently data is used. When the cache has no space for new data, it removes the item that has not been accessed for the longest time and stores the new item instead.

least_recently_used

Operations on LRU Cache

These operations help manage data efficiently in the cache by storing, accessing, updating, and removing items based on their recent usage.

Working of LRU Cache

Let's suppose we have an LRU cache of capacity 3, and we would like to perform the following operations:

The above operations are performed one after another as shown in the image below:

Working-of-LRU-Cache-copy-2

**Explanation of Each Operation

Designing a LRU Cache

**Input:[LRUCache(2), put(1,1), put(2,2), get(1), put(3,3), get(2)]
**Output: [1, -1]

**Example: Cache Capacity = 2

Operation What Happens
put(1,1) Add key 1 → Cache: [1]
put(2,2) Add key 2 → Cache: [1, 2]
get(1) Returns 1; key 1 becomes most recently used → Cache: [2, 1]
put(3,3) Cache full; remove key 2 (LRU), add key 3 → Cache: [1, 3]
get(2) Returns -1 (not found)

**Output: [1, -1]
**Reason: Key 2 was the least recently used item, so it was removed when key 3 was inserted.

LRU Cache Implementation Methods

Refer Design LRU Cache for details of all approaches.

Efficient Solution - Using Doubly Linked List and Hashing

The LRU (Least Recently Used) cache is commonly implemented using a combination of a hash map and a doubly linked list to perform insertion, deletion, and access operations efficiently.

Refer LRU cache implementation using Doubly Linked List and Hashing for details

Complexity Analysis of the Efficient Solution

The efficient implementation of an LRU Cache uses a combination of a Hash Map and a Doubly Linked List to perform cache operations in constant time.

Time Complexity

Auxiliary Space

Real-World Application

LRU Cache is commonly used in systems where recently accessed data needs to be retrieved quickly.