-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlru_cache.go
136 lines (118 loc) · 2.71 KB
/
lru_cache.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package collections
import (
"github.com/lvyahui8/ellyn/sdk/common/definitions"
"sync"
)
type cacheItem[K comparable, V definitions.Recyclable] struct {
key K
val V
prev *cacheItem[K, V]
next *cacheItem[K, V]
}
type LRUCache[K comparable, V definitions.Recyclable] struct {
lock sync.RWMutex
capacity int
head *cacheItem[K, V]
tail *cacheItem[K, V]
table map[K]*cacheItem[K, V]
}
func NewLRUCache[K comparable, V definitions.Recyclable](capacity int) *LRUCache[K, V] {
return &LRUCache[K, V]{
capacity: capacity,
table: make(map[K]*cacheItem[K, V]),
}
}
func (cache *LRUCache[K, V]) Get(key K) (V, bool) {
cache.lock.Lock()
defer cache.lock.Unlock()
return cache.get(key)
}
func (cache *LRUCache[K, V]) get(key K) (v V, ok bool) {
item := cache.table[key]
if item == nil {
return
}
cache.moveToFirst(item)
return item.val, true
}
func (cache *LRUCache[K, V]) GetWithDefault(key K, createDefault func() V) V {
cache.lock.Lock()
defer cache.lock.Unlock()
val, exist := cache.get(key)
if !exist {
val = createDefault()
cache.set(key, val)
}
return val
}
func (cache *LRUCache[K, V]) Set(key K, value V) {
cache.lock.Lock()
defer cache.lock.Unlock()
cache.set(key, value)
}
func (cache *LRUCache[K, V]) set(key K, value V) {
item := cache.table[key]
if item == nil {
if len(cache.table) >= cache.capacity {
// 空间满了,清理最久未使用的元素
cache.removeItem(cache.tail)
}
item = &cacheItem[K, V]{
val: value,
key: key,
}
cache.table[item.key] = item
}
cache.moveToFirst(item)
}
func (cache *LRUCache[K, V]) Remove(key K) {
cache.lock.Lock()
defer cache.lock.Unlock()
item := cache.table[key]
cache.removeItem(item)
}
func (cache *LRUCache[K, V]) Values() (res []V) {
cache.lock.RLock()
defer cache.lock.RUnlock()
for cur := cache.head; cur != nil; {
res = append(res, cur.val)
cur = cur.next
}
return
}
func (cache *LRUCache[K, V]) moveToFirst(item *cacheItem[K, V]) {
if cache.head != nil && cache.head.key == item.key {
return
}
// 将元素从原链表摘出
cache.removeItemFromLink(item)
// 更新到链表头部
if cache.head == nil {
cache.tail = item
} else {
cache.head.prev = item
}
item.prev = nil
item.next = cache.head
cache.head = item
}
func (cache *LRUCache[K, V]) removeItemFromLink(item *cacheItem[K, V]) {
if item == nil {
return
}
// 将元素从原链表摘出
if cache.tail != nil && cache.tail.key == item.key {
cache.tail = item.prev
}
if item.prev != nil {
item.prev.next = item.next
}
if item.next != nil {
item.next.prev = item.prev
}
}
func (cache *LRUCache[K, V]) removeItem(item *cacheItem[K, V]) {
cache.removeItemFromLink(item)
item.val.Recycle()
delete(cache.table, item.key)
}