Introduction
Caches are everywhere — CPUs, browsers, databases — and least-recently-used eviction is the classic policy. The trick to making every operation constant time is combining two containers: a linked list in recency order and a hash map from key to list node. This project packages that as a reusable class template and measures how capacity affects hit rate.
Problem statement
simulate command that replays the same key sequence through caches of several capacities.Objectives
- Write a class template with a defaulted hash parameter
- Combine std::list and std::unordered_map for O(1) operations
- Use std::list::splice to move nodes without invalidating iterators
- Return optional results and accept callbacks with std::function
Features
- O(1) get, put, erase and eviction
- Hits, misses, evictions and hit-rate statistics
- Eviction callback (used to log evictions)
- Simulation comparing capacities on one access pattern
Concepts demonstrated
- Class templates
- Iterator stability
- Hash maps
- Caching policies
- std::optional
- Callbacks
System architecture
LruCache stores std::list<std::pair<K, V>> order_ (front = most recent) and std::unordered_map<K, list::iterator> index_. get finds the iterator, splices that node to the front and returns a copy of the value. put updates in place or emplaces at the front, first evicting order_.back() if full. Because list iterators remain valid when other nodes move, the index never needs fixing up. The demo uses LruCache<std::string, std::string>; the simulator instantiates LruCache<int, long long> in front of a deliberately 'slow' function and counts how often it had to be called.
Folder structure
Complete source code
Makefile
What each file does
- src/lru_cache.h
- The complete
LruCache<Key, Value, Hash>class template. - src/main.cpp
- Interactive commands and the capacity simulation using a second instantiation of the template.
- Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
put home /index.html put about /about.html put blog /blog.html get home put shop /shop.html get about peek blog get blog put home /index-v2.html put faq /faq.html del blog del shop resize 1 resize 0 put stats simulate 1 2 3 4 : 1 2 3 1 2 4 1 2 5 1 2 3 4 5 quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - After
get home, addingshopevictsabout, nothome. peek blogdoesn't change the order;get blogmoves it to the front.- Resizing to 1 evicts from the LRU end; resizing to 0 is rejected.
- In the simulation, capacities 1 and 2 never hit while 3 reaches 42.9%.
Common errors
Crash after erasing from the list
Remove the key from the index before (or right after) erasing its list node, so you never dereference a dangling iterator.
Template errors mentioning std::hash
Your key type has no std::hash specialisation; pass a custom Hash as the third template argument.
Possible improvements
- Add time-to-live expiry per entry
- Make it thread-safe with std::mutex
- Implement LFU (least frequently used) and compare
Additional challenges
- 1Prove each operation is O(1) average.
- 2Add
get_or_load(key, loader)that calls the loader on a miss. - 3Implement a 2Q or ARC policy and compare hit rates.
Built and tested it yourself? Mark it to update your dashboard.