forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru-cache.go
158 lines (139 loc) · 3.06 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
package rdns
import (
"time"
"github.com/miekg/dns"
)
type lruCache struct {
maxItems int
items map[lruKey]*cacheItem
head, tail *cacheItem
}
type cacheItem struct {
key lruKey
*cacheAnswer
prev, next *cacheItem
}
type lruKey struct {
question dns.Question
net string
}
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
*dns.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)
item := c.touch(key)
if item != nil {
return
}
// Add new item to the top of the linked list
item = &cacheItem{
key: key,
cacheAnswer: 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.cacheAnswer
}
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.cacheAnswer) {
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 lruKeyFromQuery(q *dns.Msg) lruKey {
key := lruKey{question: q.Question[0]}
edns0 := q.IsEdns0()
if edns0 != nil {
// 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
}