I have some C++ code where I need to implement cache replacement using LRU technique.
So far I know two methods to implement LRU cache replacement:
- Using timeStamp for each time the cached data is accessed and finally comparing the timeStamps at time of replacement.
- Using a stack of cached items and moving them to the top if they are accessed recently, so finally the bottom will contain the LRU Candidate.
So, which of these is better to be used in production code?
Are their any other better methods?
Here is a very simple implementation of LRU cache
https://github.com/lamerman/cpp-lru-cache .
It's easy to use and understand how it works. The total size of code is about 50 lines.
Recently I implemented a LRU cache using a linked list spread over a hash map.
It has the advantage of being O(1) for all important operations.
The insertion algorithm:
In our production environment we use a C++ double linked list which is similar to the Linux kernel linked list. The beauty of it is that you can add an object to as many linked lists as you want and list operation is fast and simple.
For simplicity, maybe you should consider using Boost's MultiIndex map. If we separate the key from the data, we support multiple sets of keys on the same data.
From [ http://old.nabble.com/realization-of-Last-Recently-Used-cache-with-boost%3A%3Amulti_index-td22326432.html ]:
"...use two indexes: 1) hashed for searching value by key 2) sequential for tracking last recently used items (get function put item as last item in sequesnce. If we need to remove some items from cache, we may delete they from begin of sequence)."
Note that the "project" operator "allows the programmer to move between different indices of the same multi_index_container" efficiently.
This article describes implementation using a pair of STL containers (a key-value map plus a list for the key access history), or a single
boost::bimap
.