forked from dgryski/go-tinylfu
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lru.go
55 lines (43 loc) · 1.11 KB
/
lru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package tinylfu
import "container/list"
// Cache is an LRU cache. It is not safe for concurrent access.
type lruCache struct {
data map[string]*list.Element
cap int
ll *list.List
}
func newLRU(cap int, data map[string]*list.Element) *lruCache {
return &lruCache{
data: data,
cap: cap,
ll: list.New(),
}
}
// Get returns a value from the cache
func (lru *lruCache) get(v *list.Element) {
lru.ll.MoveToFront(v)
}
// Set sets a value in the cache
func (lru *lruCache) add(newItem *Item) (_ *Item, evicted bool) {
if lru.ll.Len() < lru.cap {
lru.data[newItem.Key] = lru.ll.PushFront(newItem)
return nil, false
}
// reuse the tail item
val := lru.ll.Back()
item := val.Value.(*Item)
delete(lru.data, item.Key)
oldItem := *item
*item = *newItem
lru.data[item.Key] = val
lru.ll.MoveToFront(val)
return &oldItem, true
}
// Len returns the total number of items in the cache
func (lru *lruCache) Len() int {
return len(lru.data)
}
// Remove removes an item from the cache, returning the item and a boolean indicating if it was found
func (lru *lruCache) Remove(v *list.Element) {
lru.ll.Remove(v)
}