-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathredis.go
107 lines (88 loc) · 2.14 KB
/
redis.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
package redis
import (
"errors"
"strconv"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
"github.com/mediocregopher/radix.v2/pool"
"github.com/miekg/dns"
)
// Redis is plugin that looks up responses in a cache and caches replies.
// It has a success and a denial of existence cache.
type Redis struct {
Next plugin.Handler
Zones []string
pool *pool.Pool
nttl time.Duration
pttl time.Duration
addr string
idle int
// Testing.
now func() time.Time
}
// New returns an new initialized Redis.
func New() *Redis {
return &Redis{
Zones: []string{"."},
addr: "127.0.0.1:6379",
idle: 10,
pool: &pool.Pool{},
pttl: maxTTL,
nttl: maxNTTL,
now: time.Now,
}
}
// Add adds the message m under k in Redis.
func Add(p *pool.Pool, key int, m *dns.Msg, duration time.Duration) error {
// SETEX key duration m
conn, err := p.Get()
if err != nil {
return err
}
defer p.Put(conn)
resp := conn.Cmd("SETEX", strconv.Itoa(key), int(duration.Seconds()), ToString(m))
return resp.Err
}
// Get returns the message under key from Redis.
func Get(p *pool.Pool, key int) (*dns.Msg, error) {
conn, err := p.Get()
if err != nil {
return nil, err
}
defer p.Put(conn)
resp := conn.Cmd("GET", strconv.Itoa(key))
if resp.Err != nil {
return nil, resp.Err
}
ttl := 0 // Item just expired, slap 0 TTL on it.
respTTL := conn.Cmd("TTL", strconv.Itoa(key))
if respTTL.Err == nil {
ttl, err = respTTL.Int()
if err != nil {
ttl = 0
}
}
s, _ := resp.Str()
if s == "" {
return nil, errors.New("not found")
}
m := FromString(s, ttl)
return m, nil
}
func (re *Redis) get(now time.Time, state request.Request, server string) *dns.Msg {
k := hash(state.Name(), state.QType(), state.Do())
m, err := Get(re.pool, k)
if err != nil {
log.Debugf("Failed to get response from Redis cache: %s", err)
cacheMisses.WithLabelValues(server).Inc()
return nil
}
log.Debugf("Returning response from Redis cache: %s for %s", m.Question[0].Name, state.Name())
cacheHits.WithLabelValues(server).Inc()
return m
}
func (re *Redis) connect() (err error) {
re.pool, err = pool.New("tcp", re.addr, re.idle)
return err
}