|
| 1 | +// lfu.go |
| 2 | +// description: a type of cache algorithm used to manage memory within a computer. |
| 3 | +// details: |
| 4 | +// The standard characteristics of this method involve the system keeping track of the number of times a block is referenced in memory. |
| 5 | +// When the cache is full and requires more room the system will purge the item with the lowest reference frequency. |
| 6 | +// ref: (https://en.wikipedia.org/wiki/Least_frequently_used) |
| 7 | +// time complexity: O(N) |
| 8 | +// space complexity: O(1) |
| 9 | +// author: [CocaineCong](https://github.com/CocaineCong) |
| 10 | + |
| 11 | +package cache |
| 12 | + |
| 13 | +import ( |
| 14 | + "container/list" |
| 15 | + "math" |
| 16 | +) |
| 17 | + |
| 18 | +// LFU the Least Frequently Used (LFU) page-replacement algorithm |
| 19 | +type LFU struct { |
| 20 | + len int // length |
| 21 | + cap int // capacity |
| 22 | + minFreq int // The element that operates least frequently in LFU |
| 23 | + |
| 24 | + // key: key of element, value: value of element |
| 25 | + itemMap map[string]*list.Element |
| 26 | + |
| 27 | + // key: frequency of possible occurrences of all elements in the itemMap |
| 28 | + // value: elements with the same frequency |
| 29 | + freqMap map[int]*list.List |
| 30 | +} |
| 31 | + |
| 32 | +// NewLFU init the LFU cache with capacity |
| 33 | +func NewLFU(capacity int) LFU { |
| 34 | + return LFU{ |
| 35 | + len: 0, |
| 36 | + cap: capacity, |
| 37 | + minFreq: math.MaxInt, |
| 38 | + itemMap: make(map[string]*list.Element), |
| 39 | + freqMap: make(map[int]*list.List), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// initItem to init item for LFU |
| 44 | +func initItem(k string, v any, f int) item { |
| 45 | + return item{ |
| 46 | + key: k, |
| 47 | + value: v, |
| 48 | + freq: f, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Get the key in cache by LFU |
| 53 | +func (c *LFU) Get(key string) any { |
| 54 | + // if existed, will return value |
| 55 | + if e, ok := c.itemMap[key]; ok { |
| 56 | + // the frequency of e +1 and change freqMap |
| 57 | + c.increaseFreq(e) |
| 58 | + obj := e.Value.(item) |
| 59 | + return obj.value |
| 60 | + } |
| 61 | + |
| 62 | + // if not existed, return nil |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Put the key in LFU cache |
| 67 | +func (c *LFU) Put(key string, value any) { |
| 68 | + if e, ok := c.itemMap[key]; ok { |
| 69 | + // if key existed, update the value |
| 70 | + obj := e.Value.(item) |
| 71 | + obj.value = value |
| 72 | + c.increaseFreq(e) |
| 73 | + } else { |
| 74 | + // if key not existed |
| 75 | + obj := initItem(key, value, 1) |
| 76 | + // if the length of item gets to the top line |
| 77 | + // remove the least frequently operated element |
| 78 | + if c.len == c.cap { |
| 79 | + c.eliminate() |
| 80 | + c.len-- |
| 81 | + } |
| 82 | + // insert in freqMap and itemMap |
| 83 | + c.insertMap(obj) |
| 84 | + // change minFreq to 1 because insert the newest one |
| 85 | + c.minFreq = 1 |
| 86 | + // length++ |
| 87 | + c.len++ |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// increaseFreq increase the frequency if element |
| 92 | +func (c *LFU) increaseFreq(e *list.Element) { |
| 93 | + obj := e.Value.(item) |
| 94 | + // remove from low frequency first |
| 95 | + oldLost := c.freqMap[obj.freq] |
| 96 | + oldLost.Remove(e) |
| 97 | + // change the value of minFreq |
| 98 | + if c.minFreq == obj.freq && oldLost.Len() == 0 { |
| 99 | + // if it is the last node of the minimum frequency that is removed |
| 100 | + c.minFreq++ |
| 101 | + } |
| 102 | + // add to high frequency list |
| 103 | + c.insertMap(obj) |
| 104 | +} |
| 105 | + |
| 106 | +// insertMap insert item in map |
| 107 | +func (c *LFU) insertMap(obj item) { |
| 108 | + // add in freqMap |
| 109 | + l, ok := c.freqMap[obj.freq] |
| 110 | + if !ok { |
| 111 | + l = list.New() |
| 112 | + c.freqMap[obj.freq] = l |
| 113 | + } |
| 114 | + e := l.PushFront(obj) |
| 115 | + // update or add the value of itemMap key to e |
| 116 | + c.itemMap[obj.key] = e |
| 117 | +} |
| 118 | + |
| 119 | +// eliminate clear the least frequently operated element |
| 120 | +func (c *LFU) eliminate() { |
| 121 | + l := c.freqMap[c.minFreq] |
| 122 | + e := l.Back() |
| 123 | + obj := e.Value.(item) |
| 124 | + l.Remove(e) |
| 125 | + |
| 126 | + delete(c.itemMap, obj.key) |
| 127 | +} |
0 commit comments