get(key):
put(key, value):
| Structure | Why We Use It |
|---|---|
unordered_map<Key, Node*> |
To find any key in O(1) time |
| Doubly-linked list | To move nodes to front and remove from back in O(1) |
std::mutex |
To safely allow multiple threads to use the cache |
Think of:
unordered_map as a table of contents — quickly finds where a book is.Now imagine multiple people using the room at the same time — we need to make sure no two people change the list at the same time ⇒ that’s what mutex is for.
get(key)):