What is Least Frequently Used (cache replace policy)?
Least Frequently Used, commonly referred to as LFU, is a type of cache algorithm that manages computer memory. It does so by keeping track of the number of times a block is referenced in the memory and purging the one with the least referencing frequency, once the cache is full and needs more memory.
Implementation
The easiest method to employ an LFU algorithm is by assigning a counter to each block that’s loaded into the cache, and increment it by one each time it’s reaccessed. Once the cache is full and more memory needs access, remove the one with the lowest counter value.
You have to implement two functions, put(key, value) and get(key).
The function put(key, value) takes key and value as parameters and assigns the value against the provided key. If the key already exists, it updates the value against that key.
The function get(key) takes key as a parameter and fetches the value that is stored against that key, subsequently incrementing the counter by one as well.
Free Resources