-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
227 lines (197 loc) · 3.81 KB
/
cache.go
File metadata and controls
227 lines (197 loc) · 3.81 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"runtime"
"sync"
"time"
)
const (
NoExpiration time.Duration = -1
DefaultExpiration time.Duration = 0
)
type Item struct {
Object bool
Expiration int64
LastAccess time.Time
}
func (item Item) Expired() bool {
if item.Expiration == 0 {
return false
}
return time.Now().UnixNano() > item.Expiration
}
type Cache struct {
*cache
}
func NewCache(defaultExpiration, cleanupInterval time.Duration) *Cache {
items := make(map[string]Item, 100)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
type cache struct {
defaultExpiration time.Duration
items map[string]Item
mu sync.RWMutex
onEvicted func(string, bool)
janitor *janitor
}
func newCache(de time.Duration, m map[string]Item) *cache {
if de == 0 {
de = -1
}
c := &cache{
defaultExpiration: de,
items: m,
}
return c
}
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *Cache {
c := newCache(de, m)
C := &Cache{
cache: c,
}
if ci > 0 {
runJanitor(c, ci)
runtime.SetFinalizer(C, stopJanitor)
}
return C
}
func (c *cache) Set(k string, x bool, d time.Duration) {
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = time.Now().Add(d).UnixNano()
}
c.mu.Lock()
c.items[k] = Item{
Object: x,
Expiration: e,
LastAccess: time.Now(),
}
c.mu.Unlock()
}
func (c *cache) Has(k string) bool {
_, has := c.Get(k)
return has
}
func (c *cache) Get(k string) (bool, bool) {
c.mu.Lock()
defer c.mu.Unlock()
item, found := c.items[k]
if !found {
return item.Object, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
return item.Object, false
}
}
item.LastAccess = time.Now()
c.items[k] = item
return item.Object, true
}
func (c *cache) Delete(k string) {
c.mu.Lock()
v, evicted := c.delete(k)
c.mu.Unlock()
if evicted {
c.onEvicted(k, v)
}
}
func (c *cache) delete(k string) (bool, bool) {
if c.onEvicted != nil {
if v, found := c.items[k]; found {
delete(c.items, k)
return v.Object, true
}
}
delete(c.items, k)
return false, false
}
func (c *cache) DeleteExpired() {
now := time.Now().UnixNano()
c.mu.Lock()
for k, v := range c.items {
if v.Expiration > 0 && now > v.Expiration {
c.delete(k)
}
}
c.mu.Unlock()
}
func (c *cache) DeleteLeastRecent(maxSize int) {
if len(c.items) <= maxSize {
return
}
var itemsToDelete []string
var oldestTime time.Time
var oldestKey string
c.mu.RLock()
for k, v := range c.items {
oldestTime = v.LastAccess
oldestKey = k
break
}
for len(c.items)-len(itemsToDelete) > maxSize {
for k, v := range c.items {
if v.LastAccess.Before(oldestTime) && !contains(itemsToDelete, k) {
oldestTime = v.LastAccess
oldestKey = k
}
}
itemsToDelete = append(itemsToDelete, oldestKey)
oldestTime = time.Now()
for k, v := range c.items {
if v.LastAccess.Before(oldestTime) && !contains(itemsToDelete, k) {
oldestTime = v.LastAccess
oldestKey = k
}
}
}
c.mu.RUnlock()
c.mu.Lock()
for _, k := range itemsToDelete {
delete(c.items, k)
}
c.mu.Unlock()
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func (c *cache) Flush() {
c.mu.Lock()
c.items = map[string]Item{}
c.mu.Unlock()
}
type janitor struct {
Interval time.Duration
stop chan bool
}
func (j *janitor) Run(c *cache) {
ticker := time.NewTicker(j.Interval)
for {
select {
case <-ticker.C:
c.DeleteExpired()
c.DeleteLeastRecent(10000)
case <-j.stop:
ticker.Stop()
return
}
}
}
func stopJanitor(c *Cache) {
c.janitor.stop <- true
}
func runJanitor(c *cache, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan bool),
}
c.janitor = j
go j.Run(c)
}