A least recently used (LRU) cache is a fixed size cache that behaves just like a regular lookup table, but remembers the order in which elements are inserted. Once its (user-defined) capacity is reached, it uses this information to replace the least recently used element with a newly inserted one.
Wouldn't an LRU cache keep track of the last accessed element and evict that?
Thanks everyone for the feedback. I've changed the behavior of the cache to be properly LRU -- lookup methods now move the element to the front. I wasn't trying to click-bait anyone by advertising LRU while it was LRI. I just didn't call my "move to front" method (it's just one line) inside lookup/find to see if people would think that's "OK". I figure it's not, so now it's what's expected.
I thought about this for a while and decided not moving elements to the front in the access methods would make the lookup faster. Also often "LRI" is actually good enough or preferable. I was hoping people would share their thoughts. I could also add a boolean switch to make it optional.
You can't really call it a least-recently used cache if you're basing it on insert order. The semantics are not the same.
Anyway, you don't need to actually move the element to the front. Instead, you can have a separate lightweight index that handles the ordering and bookkeeping so you never have to move your actual data elements around.
I allocate new list nodes (i.e. list::insert) until the cache is full. At that point, I reuse old nodes by assigning their key to a new key (on insertion) and moving them to the front. As such there will only be C allocations ever, where C is the capacity of the cache.
Except that linked lists tend to be allocation-heavy
Really?
Moving a node's position inside the list should be O(1) with no allocation.
Actually, once you've got the node wrapper for your data element, what allocations need to be made? I mean, I'm sure some are made in the std::list implementation... but algorithmically, I don't see that it's necessary, or that it's a property of linked lists themselves.
Redis does something like that but with some added randomization. It also does not move elements to the front of list, but takes some random N samples of keys and removes the least recently accessed key from it. Atleast this is what the algorithm used to be some time back.
LRI can easily fall into pessimal usage patterns. It'll periodically evict the most used items, which kind of defeats the purpose of a cache.
Also it makes your post have a clickbait title as it's neither LRU or feature complete.
LRI is easy to implement. LRU is much harder to do right.
I guess using a linked list would speed up the moving up to front phase (with pre-allocated nodes of course), so you don't have to change your design too much.
I always had the LRU implementation ready, I had no bad in mind. The order is maintained in a linked list of references and I splice nodes, so there's no data duplication and moving to the front is fast.
I guess using a linked list would speed up the moving up to front phase (with pre-allocated nodes of course)
That's where I really wish C++ had intrinsic containers; with an intrinsic intrusive container, you can link/unlink a node without allocating in most cases, making it ideal for composing containers.
As for splicing... that's only half the story. The unordered_map is allocating for each element inserted much like the list is allocating for each element. That's two allocations per element, whereas with intrusive nodes it could be a single one per element... or even a single array of such elements.
Now, in this particular case, this might not be an issue. But in general for composing containers it helps.
With Boost on the table, however, one can jump directly to Boost.MultiIndex for composites :)
Sure, if people find that interesting I can add it. It would be quite straight forward to subclass another implementation from the BaseCache (just like TimedCache).
I just checked the Redis code base and it uses an interesting approach to LFU (uses a logarithmic counter) and it should be better than the LRU approach that was in use earlier.
In general, cache is an associative structure. Keys have associated values, and you can look up values by the keys (more efficiently than producing the value). It is like a dictionary (an unordered map) with a strategy to evict old elements.
A circular buffer is not associative. It is simply a sequence of values.
Values of a cache are accessed in an arbitrary order. The values of a circular buffer are accessed in same order as they were inserted.
A FIFO cache (a LRI cache) could be implemented using a circular buffer, although the linear lookup complexity (in relation to the size of the ring) would be bad.
19
u/finalman Jan 03 '17
Wouldn't an LRU cache keep track of the last accessed element and evict that?