-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
175 lines (156 loc) · 3.7 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package tcache
// tcache is just another ttl lru cache with two ttl, if get a key of:
// LifeTime (soft ttl): return value and trigger update-key operation
// MaxLifeTime (hard ttl): do update-key operation first and then return updated value
import (
"container/list"
"sync"
"sync/atomic"
"time"
)
type UpdateKeyFunc func(key string) (value interface{}, err error)
// 1. Keep entries available, trigger a update-key operation instead of purging entries out-of-date.
// 2. concurrent access safe
// 3. LRU
type Cache struct {
// LifeTime is lifespan for all entries
LifeTime uint64
// if exceed MaxLifeTime will force update
MaxLifeTime uint64
MaxEntries int
// update-key operation will be trigger when value is nil or value is out-of-date
updateKeyFunc UpdateKeyFunc
cache map[string]*list.Element
ll *list.List
sync.RWMutex
}
const (
ready int32 = iota
updating
)
type entry struct {
Key string
Born int64 // Unix time
State int32
Value interface{}
}
// NewCache creates a new Cache.
// auto flush cache to file if flushFile not "" and flushInterval > 0
// for flush cache value must jsonable
func NewCache(lifeTime, maxlifeTime uint64, maxEntries int, updateKeyFunc UpdateKeyFunc) *Cache {
c := &Cache{
LifeTime: lifeTime,
MaxLifeTime: maxlifeTime,
MaxEntries: maxEntries,
ll: list.New(),
updateKeyFunc: updateKeyFunc,
cache: make(map[string]*list.Element),
}
if c.MaxLifeTime < 100*lifeTime {
c.MaxLifeTime = 100 * lifeTime
}
return c
}
// Add adds a value to the cache. Update born and state if exsit
func (c *Cache) Add(key string, value interface{}) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
c.cache = make(map[string]*list.Element)
c.ll = list.New()
}
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
ee.Value = &entry{
Key: key,
Value: value,
Born: time.Now().Unix(),
State: ready,
}
return
}
ele := c.ll.PushFront(&entry{
Key: key,
Value: value,
Born: time.Now().Unix(),
State: ready,
})
c.cache[key] = ele
if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
c.removeOldest()
}
}
// Get looks up a key's value from the cache.
// if entry is nil will block util get value
// if entry is out of date will return old value and trigger an upate-key operation if needed
func (c *Cache) Get(key string) (interface{}, error) {
c.RLock()
if c.cache == nil {
c.cache = make(map[string]*list.Element)
c.ll = list.New()
}
now := time.Now().Unix()
ee, hit := c.cache[key]
var ele *entry
if hit {
ele = ee.Value.(*entry)
}
c.RUnlock()
// hit
if hit && uint64(now-ele.Born) < c.MaxLifeTime {
// out of date and not updating
if uint64(now-ele.Born) > c.LifeTime && atomic.CompareAndSwapInt32(&ele.State, ready, updating) {
go func() {
value, err := c.updateKeyFunc(key)
if err == nil {
c.Add(key, value)
} else {
atomic.SwapInt32(&ele.State, ready)
}
}()
}
return ele.Value, nil
} else { // not hit
value, err := c.updateKeyFunc(key)
if err == nil {
c.Add(key, value)
return value, nil
}
return nil, err
}
}
// Remove removes the provided key from the cache.
func (c *Cache) Remove(key string) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
return
}
if ele, hit := c.cache[key]; hit {
c.removeElement(ele)
}
}
// Len returns the number of items in the cache.
func (c *Cache) Len() int {
c.RLock()
defer c.RUnlock()
if c.cache == nil {
return 0
}
return len(c.cache)
}
func (c *Cache) removeOldest() {
if c.cache == nil {
return
}
ele := c.ll.Back()
if ele != nil {
c.removeElement(ele)
}
}
// Remove specific element
func (c *Cache) removeElement(e *list.Element) {
c.ll.Remove(e)
kv := e.Value.(*entry)
delete(c.cache, kv.Key)
}