-
Notifications
You must be signed in to change notification settings - Fork 62
/
lru-cache.go
227 lines (203 loc) · 4.47 KB
/
lru-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
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 rdns
import (
"encoding/json"
"io"
"time"
"github.com/miekg/dns"
)
type lruCache struct {
maxItems int
items map[lruKey]*cacheItem
head, tail *cacheItem
}
type cacheItem struct {
Key lruKey
Answer *cacheAnswer
prev, next *cacheItem
}
type lruKey struct {
Question dns.Question
Net string
Do bool
}
type cacheAnswer struct {
Timestamp time.Time // Time the record was cached. Needed to adjust TTL
Expiry time.Time // Time the record expires and should be removed
PrefetchEligible bool // The cache can prefetch this record
Msg *dns.Msg
}
func (c cacheAnswer) MarshalJSON() ([]byte, error) {
msg, err := c.Msg.Pack()
if err != nil {
return nil, err
}
type alias cacheAnswer
record := struct {
alias
Msg []byte
}{
alias: alias(c),
Msg: msg,
}
return json.Marshal(record)
}
func (c *cacheAnswer) UnmarshalJSON(data []byte) error {
type alias cacheAnswer
aux := struct {
*alias
Msg []byte
}{
alias: (*alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
c.Msg = new(dns.Msg)
return c.Msg.Unpack(aux.Msg)
}
func newLRUCache(capacity int) *lruCache {
head := new(cacheItem)
tail := new(cacheItem)
head.next = tail
tail.prev = head
return &lruCache{
maxItems: capacity,
items: make(map[lruKey]*cacheItem),
head: head,
tail: tail,
}
}
func (c *lruCache) add(query *dns.Msg, answer *cacheAnswer) {
key := lruKeyFromQuery(query)
c.addKey(key, answer)
}
func (c *lruCache) addKey(key lruKey, answer *cacheAnswer) {
item := c.touch(key)
if item != nil {
// Update the item, it's already at the top of the list
// so we can just change the value
item.Answer = answer
return
}
// Add new item to the top of the linked list
item = &cacheItem{
Key: key,
Answer: answer,
next: c.head.next,
prev: c.head,
}
c.head.next.prev = item
c.head.next = item
c.items[key] = item
c.resize()
}
// Loads a cache item and puts it to the top of the queue (most recent).
func (c *lruCache) touch(key lruKey) *cacheItem {
item := c.items[key]
if item == nil {
return nil
}
// move the item to the top of the linked list
item.prev.next = item.next
item.next.prev = item.prev
item.next = c.head.next
item.prev = c.head
c.head.next.prev = item
c.head.next = item
return item
}
func (c *lruCache) delete(q *dns.Msg) {
key := lruKeyFromQuery(q)
item := c.items[key]
if item == nil {
return
}
item.prev.next = item.next
item.next.prev = item.prev
delete(c.items, key)
}
func (c *lruCache) get(query *dns.Msg) *cacheAnswer {
key := lruKeyFromQuery(query)
item := c.touch(key)
if item != nil {
return item.Answer
}
return nil
}
// Shrink the cache down to the maximum number of items.
func (c *lruCache) resize() {
if c.maxItems <= 0 { // no size limit
return
}
drop := len(c.items) - c.maxItems
for i := 0; i < drop; i++ {
item := c.tail.prev
item.prev.next = c.tail
c.tail.prev = item.prev
delete(c.items, item.Key)
}
}
// Clear the cache.
func (c *lruCache) reset() {
head := new(cacheItem)
tail := new(cacheItem)
head.next = tail
tail.prev = head
c.head = head
c.tail = tail
c.items = make(map[lruKey]*cacheItem)
}
// Iterate over the cached answers and call the provided function. If it
// returns true, the item is deleted from the cache.
func (c *lruCache) deleteFunc(f func(*cacheAnswer) bool) {
item := c.head.next
for item != c.tail {
if f(item.Answer) {
item.prev.next = item.next
item.next.prev = item.prev
delete(c.items, item.Key)
}
item = item.next
}
}
func (c *lruCache) size() int {
return len(c.items)
}
func (c *lruCache) serialize(w io.Writer) error {
enc := json.NewEncoder(w)
for item := c.tail.prev; item != c.head; item = item.prev {
if err := enc.Encode(item); err != nil {
return err
}
}
return nil
}
func (c *lruCache) deserialize(r io.Reader) error {
dec := json.NewDecoder(r)
for dec.More() {
item := new(cacheItem)
if err := dec.Decode(item); err != nil {
return err
}
// Skip bad (or incompatible) records
if item.Key.Question.Name == "" || item.Answer == nil {
continue
}
c.addKey(item.Key, item.Answer)
}
return nil
}
func lruKeyFromQuery(q *dns.Msg) lruKey {
key := lruKey{Question: q.Question[0]}
edns0 := q.IsEdns0()
if edns0 != nil {
key.Do = edns0.Do()
// See if we have a subnet option
for _, opt := range edns0.Option {
if subnet, ok := opt.(*dns.EDNS0_SUBNET); ok {
key.Net = subnet.Address.String()
}
}
}
return key
}