forked from rancher/rancher-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
56 lines (45 loc) · 1.39 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
package main
import (
"github.com/miekg/dns"
"github.com/skynetservices/skydns/cache"
)
func getClientCache(clientIp string) *cache.Cache {
clientSpecificCachesMutex.RLock()
cache, ok := clientSpecificCaches[clientIp]
clientSpecificCachesMutex.RUnlock()
if ok {
return cache
}
return nil
}
func addClientCache(clientIp string) {
if clientCache := getClientCache(clientIp); clientCache != nil {
return
}
clientSpecificCachesMutex.Lock()
clientSpecificCaches[clientIp] = cache.New(int(*cacheCapacity), int(*defaultTtl))
clientSpecificCachesMutex.Unlock()
}
func globalCacheHit(req *dns.Msg) *dns.Msg {
return globalCache.Hit(req.Question[0], false, false, req.MsgHdr.Id)
}
func clientSpecificCacheHit(clientIp string, req *dns.Msg) *dns.Msg {
addClientCache(clientIp)
clientCache := getClientCache(clientIp)
return clientCache.Hit(req.Question[0], false, false, req.MsgHdr.Id)
}
func addToGlobalCache(req, msg *dns.Msg) {
key := cache.Key(req.Question[0], false, false)
globalCache.InsertMessage(key, msg)
}
func addToClientSpecificCache(clientIp string, req, msg *dns.Msg) {
addClientCache(clientIp)
clientCache := getClientCache(clientIp)
key := cache.Key(req.Question[0], false, false)
clientCache.InsertMessage(key, msg)
}
func clearClientSpecificCaches() {
clientSpecificCachesMutex.Lock()
clientSpecificCaches = make(map[string]*cache.Cache)
clientSpecificCachesMutex.Unlock()
}