This repository was archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheviction.go
210 lines (189 loc) · 5.68 KB
/
eviction.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
199
200
201
202
203
204
205
206
207
208
209
210
package recache
import "time"
var (
// Schedule and debounce eventual cache eviction of record.
// Buffered to reduce lock contention on scheduling and deadlocks
evictAfter = make(chan evictionReq, 1<<10)
)
type evictionReq struct {
loc intercacheRecordLocation
timer time.Duration
}
func init() {
go func() {
pending := make(map[intercacheRecordLocation]time.Time)
scan := time.Tick(time.Second)
for {
select {
case req := <-evictAfter:
existing, ok := pending[req.loc]
deadline := time.Now().Add(req.timer)
if !ok || deadline.Before(existing) {
pending[req.loc] = deadline
}
case <-scan:
now := time.Now()
for loc, deadline := range pending {
if deadline.Before(now) {
delete(pending, loc)
evict(loc, 0)
}
}
}
}
}()
}
// Evict record from cache after t
func evict(loc intercacheRecordLocation, t time.Duration) {
getCache(loc.cache).evict(loc.recordLocation, t)
}
// Evict record from cache after t
func (c *Cache) evict(loc recordLocation, t time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.evictWithLock(loc, t)
}
// Evict record from cache after t. Requires lock on c.mu.
func (c *Cache) evictWithLock(loc recordLocation, t time.Duration) {
rec, ok := c.record(loc)
if !ok {
return
}
if t != 0 {
evictAfter <- evictionReq{
loc: intercacheRecordLocation{
cache: c.id,
recordLocation: loc,
},
timer: t,
}
return
}
delete(c.frontends[loc.frontend], loc.key)
c.lruList.Remove(rec.node)
c.memoryUsed -= rec.memoryUsed
for _, ch := range rec.includedIn {
if ch.cache == c.id {
// Hot path to reduce lock contention
c.evictWithLock(ch.recordLocation, 0)
} else {
// Separate goroutine to prevent lock intersection
go evict(ch, 0)
}
}
}
// Evict all keys of specific frontend after t
func (c *Cache) evictFrontend(frontend int, t time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.evictFrontendWithLock(frontend, t)
}
// Evict all keys of specific frontend after t. Requires lock on c.mu.
func (c *Cache) evictFrontendWithLock(frontend int, t time.Duration) {
for _, k := range c.keys(frontend) {
c.evictWithLock(recordLocation{frontend, k}, t)
}
}
// Evict keys from frontend using matcher function fn after t.
//
// fn muts return true, if a key must be evicted.
func (c *Cache) evictByFunc(
frontend int,
t time.Duration,
fn func(Key) (bool, error),
) (err error) {
c.mu.Lock()
defer c.mu.Unlock()
var (
b = c.frontends[frontend]
evict bool
)
for _, k := range c.keys(frontend) {
// Check, if key not already evicted by recursive eviction, to reduce
// potentially expensive matcher function calls
if _, ok := b[k]; !ok {
continue
}
evict, err = fn(k)
if err != nil {
return
}
if evict {
c.evictWithLock(recordLocation{frontend, k}, t)
}
}
return
}
// Evict all records from cache after t amount of time, if the matched are still
// in the cache by then.
//
// If t = 0, any matched record(s) are evicted immediately.
//
// t can be used to decrease record turnover on often evicted records, thereby
// decreasing fresh data fetches and improving performance.
//
// Any subsequent scheduled eviction calls on matching records with a greater t
// value than is currently left from a previous scheduled eviction on the
// record will have no effect.
//
// A scheduled eviction with a smaller timer than currently left on the record
// will replace the existing timer.
func (c *Cache) EvictAll(t time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
for i := range c.frontends {
c.evictFrontendWithLock(i, t)
}
}
// Evict a record by key after t amount of time, if the matched are still in
// the cache by then.
//
// If t = 0, any matched record(s) are evicted immediately.
//
// t can be used to decrease record turnover on often evicted records, thereby
// decreasing fresh data fetches and improving performance.
//
// Any subsequent scheduled eviction calls on matching records with a greater t
// value than is currently left from a previous scheduled eviction on the
// record will have no effect.
//
// A scheduled eviction with a smaller timer than currently left on the record
// will replace the existing timer.
func (f *Frontend) Evict(t time.Duration, k Key) {
f.cache.evict(recordLocation{f.id, k}, t)
}
// Evict all records from frontend after t amount of time, if the matched are
// still in the cache by then.
//
// If t = 0, any matched record(s) are evicted immediately.
//
// t can be used to decrease record turnover on often evicted records, thereby
// decreasing fresh data fetches and improving performance.
//
// Any subsequent scheduled eviction calls on matching records with a greater t
// value than is currently left from a previous scheduled eviction on the
// record will have no effect.
//
// A scheduled eviction with a smaller timer than currently left on the record
// will replace the existing timer.
func (f *Frontend) EvictAll(t time.Duration) {
f.cache.evictFrontend(f.id, t)
}
// Evict records from frontend using matcher function fn after t amount of time,
// if the matched are still in the cache by then.
//
// If t = 0, any matched record(s) are evicted immediately.
//
// t can be used to decrease record turnover on often evicted records, thereby
// decreasing fresh data fetches and improving performance.
//
// Any subsequent scheduled eviction calls on matching records with a greater t
// value than is currently left from a previous scheduled eviction on the
// record will have no effect.
//
// A scheduled eviction with a smaller timer than currently left on the record
// will replace the existing timer.
func (f *Frontend) EvictByFunc(t time.Duration, fn func(Key) (bool, error),
) error {
return f.cache.evictByFunc(f.id, t, fn)
}