This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
198 lines (171 loc) · 4.44 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Package weakcache is weak cache implementation using finalizers and reference counting.
package weakcache
import (
"hash/maphash"
"runtime"
"sync"
"time"
)
// Record is a reference-counted cache record.
type Record struct {
Value interface{}
minTTL int64
expires int64
refs uint
lastUnref int64
}
// isExpired reports if the record has expired or
// has been unreferenced for too long.
func (r Record) isExpired(now int64) bool {
// The record has not been referenced for at least r.minTTL duration.
if r.lastUnref > 0 && r.lastUnref+r.minTTL < now {
return true
}
if r.expires > 0 && r.expires < now {
return true
}
return false
}
type recordMap map[uint64]Record
type fetch func() (interface{}, error)
// Cache is a reference-counting cache which lets keys and values
// that have no reference outside of the cache be garbage collected.
type Cache struct {
mu sync.Mutex
gcInterval time.Duration
reachable recordMap
unreachable recordMap
seed maphash.Seed
quit chan struct{}
}
// New creates an empty cache with specified GC interval.
func New(gcInterval time.Duration) *Cache {
c := &Cache{
gcInterval: gcInterval,
reachable: make(recordMap),
unreachable: make(recordMap),
seed: maphash.MakeSeed(),
quit: make(chan struct{}),
}
go c.gcLoop()
return c
}
// Fetch gets or sets a record. It calls fetch as a fallback on cache miss.
// minTTL specifies how long the record will survive without being referenced.
// maxTTL specifies the maximum lifetime of the record.
func (c *Cache) Fetch(key string, minTTL, maxTTL time.Duration, fetch fetch) (*Record, error) {
index := c.index(key)
// Acquire a unique pointer to the record. When the pointer gets garbage collected,
// the reference count for the record will be decremented.
rec, err := c.fetch(index, minTTL, maxTTL, fetch)
if err != nil {
return nil, err
}
runtime.SetFinalizer(rec, func(_ interface{}) {
go c.unref(index)
})
return rec, nil
}
// Len returns the number of cached items.
func (c *Cache) Len() int {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.reachable) + len(c.unreachable)
}
// Close stops the cache GC loop.
func (c *Cache) Close() {
close(c.quit)
}
func (c *Cache) index(key string) uint64 {
var h maphash.Hash
h.SetSeed(c.seed)
h.WriteString(key)
return h.Sum64()
}
func (c *Cache) gcLoop() {
ticker := time.NewTicker(c.gcInterval)
defer ticker.Stop()
for {
select {
case <-c.quit:
return
case now := <-ticker.C:
nowNano := now.UnixNano()
c.mu.Lock()
// Clean up unreachable records,
for index, rec := range c.unreachable {
if rec.isExpired(nowNano) {
delete(c.unreachable, index)
}
}
c.mu.Unlock()
}
}
}
func (c *Cache) fetch(index uint64, minTTL, maxTTL time.Duration, fetch fetch) (*Record, error) {
c.mu.Lock()
defer c.mu.Unlock()
now := time.Now()
get := func() *Record {
if rec, ok := c.unreachable[index]; ok {
// An unreachable record was found, make it reachable later.
delete(c.unreachable, index)
if rec.isExpired(now.UnixNano()) {
return nil
}
return &rec
} else if rec, ok = c.reachable[index]; ok {
if rec.isExpired(now.UnixNano()) {
delete(c.reachable, index)
return nil
}
// A reachable record was found.
return &rec
}
return nil
}
rec := get()
if rec == nil {
// Create a new record.
value, err := fetch()
if err != nil {
return nil, err
}
rec = &Record{
Value: value,
minTTL: int64(minTTL),
}
if maxTTL > 0 {
rec.expires = now.Add(maxTTL).UnixNano()
}
}
rec.refs++
// Store a value in the map. The pointer is returned only to the caller
// so that the caller triggers a finalizer when the pointer is garbage collected.
c.reachable[index] = *rec
return rec, nil
}
// unref is called when a pointer to a cache record gets garbage collected.
func (c *Cache) unref(index uint64) {
c.mu.Lock()
defer c.mu.Unlock()
rec, ok := c.reachable[index]
if !ok {
// The record probably expired during fetch
// while having other live pointers.
return
}
// Decrease reference count for the record.
rec.refs--
if rec.refs > 0 {
// Record has other live pointers.
c.reachable[index] = rec
} else {
// No pointers, move to unreachable map.
delete(c.reachable, index)
// Mark the last unref time so that the record would survive
// being unreachable until at least minTTL duration has passed.
rec.lastUnref = time.Now().UnixNano()
c.unreachable[index] = rec
}
}