Least Recently Used cache using C
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Least Recently Used (LRU) cache is a type of data structure that is particularly effective for managing data that may not fit entirely into memory. Implemented as part of various caching strategies, the LRU policy evicts the least recently accessed items to make space for new data. In this article, we'll explore how to implement an LRU cache in C++, along with technical explanations and examples to illustrate its functionality.
Why Use an LRU Cache?
An LRU cache can be invaluable when you're working with databases, operating systems, or any application where memory resources are constrained. The main idea is simple: track the recent usage patterns of data to make intelligent decisions about which data to keep and which to evict.
Data Structure
The optimal structure for an LRU cache combines a hash map for fast access and a doubly-linked list for quick updates to the cache order. Here's a breakdown of the components:
- Hash Map: Provides average time complexity for lookups, storing cache entries with keys and pointers to the positions in the doubly-linked list.
- Doubly-Linked List: Facilitates efficient inserts and deletes with time complexity, maintaining the order of usage from most to least recent.
C++ Implementation
Let's dive into a C++ implementation of an LRU cache that uses these data structures. We'll first define the core elements necessary to execute the LRU policy.
Example Code
Explanation
- Constructor: Initializes the cache with a specified capacity.
- Get Method:
- Checks if the item exists in the cache.
- Moves accessed key to the head of the list, indicating recent use.
- Put Method:
- Updates the cache if the item already exists.
- If the cache is full, removes the least recently used item.
- Inserts the new item at the head of the list.
Performance Considerations
An LRU cache implemented as above provides excellent time complexity characteristics:
- Both
getandputoperations work in time. - The eviction of the least-recently used item is efficiently handled by maintaining the order in a doubly-linked list.
Key Points Summary
| Feature | Implementation Method | Time Complexity |
| Data Lookup | Hash Map | |
| Data Insertion | Doubly-Linked List with move to head | |
| Eviction Mechanism | Remove from back of Doubly-Linked List and update Hash Map | |
| Order Maintenance | Doubly-Linked List |
Conclusion
The LRU cache is a fundamental concept in many applications where efficient memory management is required. By combining a hash map and a doubly-linked list, the LRU cache offers simplicity with excellent performance. This C++ implementation provides a practical example of how to build an efficient LRU caching mechanism capable of handling dynamic access patterns.
Related reading
- Legal Hierarchical Quorums in Zookeeper
- Limit Kafka batches size when using Spark Streaming
- Limit on the number of topics in Kafka
- Linking containers between task definitions in AWS ECS?
- LeetCode Contains Duplicate III
- Leetcode House robber
- Left Rotation on an Array
- Library for working with potentially infinite graphs defined by neighbor-list functions

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.