This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
Task #2 this week has us build an LRU cache, which is a cache of fixed capacity, whereby when it is full but a new item needs to be added, the item that was least-recently accessed is evicted from the cache.
For example, if a cache of capacity = 3 contains [a b c]
(where a
was accessed most recently), elements are evicted off the right side of the list. Adding element d
, the list would contain [d a b]
, for example.
Of course, [d a b]
are just the keys of the items in the cache. We’ll take an arbitrary scalar (which could itself be a reference) as a value.