-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
dnscache.go
66 lines (52 loc) · 1.47 KB
/
dnscache.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
package wildcarder
import (
"sync"
)
// DNSCache represents a cache of DNS queries and answers.
type DNSCache struct {
mu sync.Mutex
cache map[QuestionHash][]AnswerHash
}
// NewDNSCache creates an empty cache.
func NewDNSCache() *DNSCache {
cache := DNSCache{}
cache.cache = make(map[QuestionHash][]AnswerHash)
return &cache
}
// Add adds an answer to the DNS cache. The answer will be appended to the list of
// existing answers for a question if they already exist.
func (c *DNSCache) Add(question string, answers []DNSAnswer) {
c.mu.Lock()
defer c.mu.Unlock()
questionHash := HashQuestion(question)
if _, ok := c.cache[questionHash]; !ok {
c.cache[questionHash] = []AnswerHash{}
}
for _, answer := range answers {
answerHash := HashAnswer(answer)
found := false
for _, answer := range c.cache[questionHash] {
if answer == answerHash {
found = true
break
}
}
if !found {
c.cache[questionHash] = append(c.cache[questionHash], answerHash)
}
}
}
// Find returns the answers for a given DNS query from the cache.
// The list of answers returned can be empty if the question is in the cache but
// no results were found, or nil if the question is not in the cache.
func (c *DNSCache) Find(question string) []AnswerHash {
c.mu.Lock()
defer c.mu.Unlock()
questionHash := HashQuestion(question)
if questionMap, ok := c.cache[questionHash]; ok {
answers := []AnswerHash{}
answers = append(answers, questionMap...)
return answers
}
return nil
}