/
structure_cache_lur.go
69 lines (57 loc) · 1.18 KB
/
structure_cache_lur.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
package cmn
import (
"container/list"
"sync"
)
type LruCache struct {
maxEntries int
cache map[string]*list.Element
lruList *list.List
mutex sync.Mutex
}
type lruCacheEntry struct {
key string
value string
}
// 内存缓存(最近使用优先)
func NewLruCache(maxEntries int) *LruCache {
if maxEntries < 16 {
maxEntries = 16
}
if maxEntries > 100*10000 {
maxEntries = 100 * 10000
}
return &LruCache{
maxEntries: maxEntries,
cache: make(map[string]*list.Element, maxEntries),
lruList: list.New(),
}
}
func (c *LruCache) Get(key string) (string, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
if ele, ok := c.cache[key]; ok {
c.lruList.MoveToFront(ele)
return ele.Value.(*lruCacheEntry).value, true
}
return "", false
}
func (c *LruCache) Add(key string, value string) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.lruList.Len() >= c.maxEntries {
c.removeOldest()
}
e := c.lruList.PushFront(&lruCacheEntry{key, value})
c.cache[key] = e
}
func (c *LruCache) removeOldest() {
if c.lruList.Len() == 0 {
return
}
ele := c.lruList.Back()
if ele != nil {
c.lruList.Remove(ele)
delete(c.cache, ele.Value.(*lruCacheEntry).key)
}
}