-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.go
177 lines (159 loc) · 3.36 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
176
177
package cache2go
import (
"container/list"
"sync"
"time"
)
// Cache is an LRU cache.
type Cache struct {
sync.RWMutex
// MaxEntries is the maximum number of cache entries before
// an item is evicted. Zero means no limit.
maxEntries int
lruIndex *list.List
ttlIndex []*list.Element
cache map[string]*list.Element
expiration time.Duration
}
type entry struct {
key string
value interface{}
timestamp time.Time
}
// New creates a new Cache.
// If maxEntries is zero, the cache has no limit and it's assumed
// that eviction is done by the caller.
func New(maxEntries int, expire time.Duration) *Cache {
c := &Cache{
maxEntries: maxEntries,
expiration: expire,
lruIndex: list.New(),
cache: make(map[string]*list.Element),
}
if c.expiration > 0 {
c.ttlIndex = make([]*list.Element, 0)
go c.cleanExpired()
}
return c
}
// cleans expired entries performing minimal checks
func (c *Cache) cleanExpired() {
for {
c.RLock()
if len(c.ttlIndex) == 0 {
c.RUnlock()
time.Sleep(c.expiration)
continue
}
e := c.ttlIndex[0]
en := e.Value.(*entry)
exp := en.timestamp.Add(c.expiration)
c.RUnlock()
if time.Now().After(exp) {
c.Lock()
c.removeElement(e)
c.Unlock()
} else {
time.Sleep(time.Now().Sub(exp))
}
}
}
// Add adds a value to the cache
func (c *Cache) Set(key string, value interface{}) {
c.Lock()
if c.cache == nil {
c.cache = make(map[string]*list.Element)
c.lruIndex = list.New()
if c.expiration > 0 {
c.ttlIndex = make([]*list.Element, 0)
}
}
if e, ok := c.cache[key]; ok {
c.lruIndex.MoveToFront(e)
en := e.Value.(*entry)
en.value = value
en.timestamp = time.Now()
c.Unlock()
return
}
e := c.lruIndex.PushFront(&entry{key: key, value: value, timestamp: time.Now()})
if c.expiration > 0 {
c.ttlIndex = append(c.ttlIndex, e)
}
c.cache[key] = e
if c.maxEntries != 0 && c.lruIndex.Len() > c.maxEntries {
c.removeOldest()
}
c.Unlock()
}
// Get looks up a key's value from the cache.
func (c *Cache) Get(key string) (value interface{}, ok bool) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
return
}
if e, hit := c.cache[key]; hit {
c.lruIndex.MoveToFront(e)
return e.Value.(*entry).value, true
}
return
}
// Remove removes the provided key from the cache.
func (c *Cache) Delete(key string) {
c.Lock()
defer c.Unlock()
if c.cache == nil {
return
}
if e, hit := c.cache[key]; hit {
c.removeElement(e)
}
}
// RemoveOldest removes the oldest item from the cache.
func (c *Cache) removeOldest() {
if c.cache == nil {
return
}
e := c.lruIndex.Back()
if e != nil {
c.removeElement(e)
}
}
func (c *Cache) removeElement(e *list.Element) {
c.lruIndex.Remove(e)
if c.expiration > 0 {
for i, se := range c.ttlIndex {
if se == e {
//delete
copy(c.ttlIndex[i:], c.ttlIndex[i+1:])
c.ttlIndex[len(c.ttlIndex)-1] = nil
c.ttlIndex = c.ttlIndex[:len(c.ttlIndex)-1]
break
}
}
}
if e.Value != nil {
kv := e.Value.(*entry)
delete(c.cache, kv.key)
}
}
// 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 c.lruIndex.Len()
}
// empties the whole cache
func (c *Cache) Flush() {
c.Lock()
defer c.Unlock()
c.lruIndex = list.New()
if c.expiration > 0 {
c.ttlIndex = make([]*list.Element, 0)
}
c.cache = make(map[string]*list.Element)
}