This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
cache.go
61 lines (52 loc) · 1.57 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
57
58
59
60
61
package main
import (
"time"
"github.com/miekg/dns"
"github.com/rancher/rancher-dns/cache"
)
func getClientCache(clientUUID string) *cache.Cache {
clientSpecificCachesMutex.RLock()
clientCache, ok := clientSpecificCaches[clientUUID]
clientSpecificCachesMutex.RUnlock()
if !ok {
clientCache = cache.New(int(*cacheCapacity), int(*defaultTtl))
clientSpecificCachesMutex.Lock()
clientSpecificCaches[clientUUID] = clientCache
clientSpecificCachesMutex.Unlock()
}
return clientCache
}
func globalCacheHit(req *dns.Msg) (*dns.Msg, time.Time) {
return globalCache.Hit(req.Question[0], false, false, req.MsgHdr.Id)
}
func clientSpecificCacheHit(clientUUID string, req *dns.Msg) (*dns.Msg, time.Time) {
return getClientCache(clientUUID).Hit(req.Question[0], false, false, req.MsgHdr.Id)
}
func addToCache(req, msg *dns.Msg, clientUUID ...string) {
var currCache *cache.Cache
if len(clientUUID) == 0 {
currCache = globalCache
} else {
currCache = getClientCache(clientUUID[0])
}
ttl := currCache.GetTTL()
if len(msg.Answer) > 0 {
var requestTtl = time.Duration(msg.Answer[0].Header().Ttl) * time.Second
if requestTtl < ttl {
ttl = requestTtl
}
}
key := cache.Key(req.Question[0], false, false)
currCache.InsertMessage(key, msg, ttl)
}
func addToGlobalCache(req, msg *dns.Msg) {
addToCache(req, msg)
}
func addToClientSpecificCache(clientUUID string, req, msg *dns.Msg) {
addToCache(req, msg, clientUUID)
}
func clearClientSpecificCaches() {
clientSpecificCachesMutex.Lock()
clientSpecificCaches = make(map[string]*cache.Cache)
clientSpecificCachesMutex.Unlock()
}