-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
96 lines (83 loc) · 1.81 KB
/
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
package filterql
import (
"container/list"
"sync"
)
type CacheProvider interface {
Load(query string) (BoolAst, bool)
Store(query string, cond BoolAst)
}
// MapCache 简单封装sync.Map实现的Cache,仅用于测试
type mapCache struct {
sync.RWMutex
m map[string]BoolAst
}
func NewMapCache() *mapCache { return &mapCache{m: make(map[string]BoolAst)} }
func (c *mapCache) Load(query string) (rv BoolAst, found bool) {
c.RLock()
defer c.RUnlock()
rv, found = c.m[query]
return
/*
if data, found := c.m.Load(query); !found {
return nil, false
} else if cond, is := data.(BoolAst); !is {
return nil, false
} else {
return cond, true
}
*/
}
func (c *mapCache) Store(query string, cond BoolAst) {
c.Lock()
defer c.Unlock()
c.m[query] = cond
}
// LruCache
type lruItem struct {
query string
cond BoolAst
}
type lruCache struct {
lock sync.Mutex
items *list.List
capacity int
m map[string]*list.Element
}
func NewLRUCache(capacity int) *lruCache {
return &lruCache{
items: list.New(),
capacity: capacity,
m: make(map[string]*list.Element),
}
}
func (c *lruCache) Load(query string) (BoolAst, bool) {
c.lock.Lock()
defer c.lock.Unlock()
if el, found := c.m[query]; !found {
return nil, false
} else if item, is := el.Value.(lruItem); !is {
return nil, false
} else {
c.items.MoveToFront(el)
return item.cond, true
}
}
func (c *lruCache) Store(query string, cond BoolAst) {
c.lock.Lock()
defer c.lock.Unlock()
item := lruItem{query: query, cond: cond}
if el, found := c.m[query]; found {
el.Value = item
c.items.MoveToFront(el)
} else {
newEl := c.items.PushFront(item)
c.m[query] = newEl
if c.items.Len() > c.capacity {
if back := c.items.Back(); back != nil {
c.items.Remove(back)
delete(c.m, back.Value.(lruItem).query)
}
}
}
}