How to implement a Least Frequently Used (LFU) cache?

后端 未结 7 1450
孤城傲影
孤城傲影 2021-01-31 04:50

Least Frequently Used (LFU) is a type of cache algorithm used to manage memory within a computer. The standard characteristics of this method involve the system keeping track of

相关标签:
7条回答
  • I think, the LFU data structure must combine priority queue (for maintaining fast access to lfu item) and hash map (for providing fast access to any item by its key); I would suggest the following node definition for each object stored in cache:

    class Node<T> {
       // access key
       private int key;
       // counter of accesses
       private int numAccesses;
       // current position in pq
       private int currentPos;
       // item itself
       private T item;
       //getters, setters, constructors go here
    }
    

    You need key for referring to an item. You need numAccesses as a key for priority queue. You need currentPos to be able to quickly find a pq position of item by key. Now you organize hash map (key(Integer) -> node(Node<T>)) to quickly access items and min heap-based priority queue using number of accesses as priority. Now you can very quickly perform all operations (access, add new item, update number of acceses, remove lfu). You need to write each operation carefully, so that it maintains all the nodes consistent (their number of accesses, their position in pq and there existence in hash map). All operations will work with constant average time complexity which is what you expect from cache.

    0 讨论(0)
提交回复
热议问题