Currently Demanded Localities are kep in a smaller and faster memory called as cache in order to reduce expensive main memory accesses.
Locality of reference: if CPU has requested an Address for memory Access then that particular Address or nearby Addresses will be Accessed soon.
Hit ratio = No.of hit / total memory reference
Miss ratio = 1 - Hit ratio
when CPU requested data is not present in cache then it is said to be cache miss, so the requested data is sent from main memory to cpu and along with it, the block (which contains missed data ) is copied from main memory to cache for future references. This reduces repeated expensive memory accesses.
Since cache memory is limited, older data must be removed when capacity is exceeded.
- LRU replaces the block which have not been used for longest period of time.
- LRU is commonly used because this policies gives:
- High hit ratio
- low cache miss penalty.
- efficiently utilize memory
- optimize frequently accessed data retrieval
LRU-based caching is commonly used in:
- Browsers
- Databases
- Backend APIs
- Operating Systems
- Distributed Systems
hence, this Policies is implemented in real systems.
This project combines:
- HashMap
- Doubly Linked List
to achieve O(1) time complexity for both get and put operations.
Used for:
- constant-time lookup
- fast key-based access
Used for:
- maintaining access order
- efficiently moving recently accessed items
- removing least recently used items
Combining both data structures enables efficient cache management.
This project implements an In-Memory LRU Cache System using Java.
The cache:
- stores data in memory
- tracks recently accessed items
- automatically evicts least recently used entries when capacity is exceeded
The implementation simulates real-world cache eviction behavior used in backend systems.
- O(1) get and put operations
- Automatic LRU eviction
- Efficient in-memory caching
- HashMap + Doubly Linked List implementation
- Edge case handling
- Ordered access tracking
- Java
- HashMap
- Doubly Linked List
- Object-Oriented Programming (OOP)
LRUCache.java
Main.java
- Clone the repository
git clone https://github.com/Monishohms/lru-cache-java.git- Compile Java files
javac *.java- Run the project
java Main- Generic Type Support
- Thread-Safe Cache
- TTL Expiration
- Persistent Storage Support
- REST API Integration
- Distributed Cache Support