aboutsummaryrefslogtreecommitdiff
path: root/middleware/cache/handler.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-10-02 08:31:44 +0100
committerGravatar GitHub <noreply@github.com> 2016-10-02 08:31:44 +0100
commite54c232c8c97fb163647c697e921e6f69846e304 (patch)
treeb75fff81276e58a2ec4417c6c6742a22d6156f27 /middleware/cache/handler.go
parent9b6b8d276269cb1a36b7f78da4caa51106dff0ed (diff)
downloadcoredns-e54c232c8c97fb163647c697e921e6f69846e304.tar.gz
coredns-e54c232c8c97fb163647c697e921e6f69846e304.tar.zst
coredns-e54c232c8c97fb163647c697e921e6f69846e304.zip
middleware/cache: split cache in positive and negative and use lru (#298)
Make the cache memory bounded, by using a LRU cache. Also split the cache in a positive and negative one - each with its own controls. Extend the cache stanza to allow for this: cache { positive limit [ttl] negative limit [ttl] } is now possible. This also add a cache_test.go in the toplevel test/ directory that exercises the caching path. Fixes #260
Diffstat (limited to 'middleware/cache/handler.go')
-rw-r--r--middleware/cache/handler.go30
1 files changed, 17 insertions, 13 deletions
diff --git a/middleware/cache/handler.go b/middleware/cache/handler.go
index 045c8ab1d..e307b0b79 100644
--- a/middleware/cache/handler.go
+++ b/middleware/cache/handler.go
@@ -1,6 +1,8 @@
package cache
import (
+ "time"
+
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/request"
@@ -10,7 +12,7 @@ import (
)
// ServeDNS implements the middleware.Handler interface.
-func (c Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
+func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
qname := state.Name()
@@ -20,34 +22,36 @@ func (c Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
return c.Next.ServeDNS(ctx, w, r)
}
- do := state.Do() // might need more from OPT record?
+ do := state.Do() // might need more from OPT record? Like the actual bufsize?
+
+ if i, ok, expired := c.get(qname, qtype, do); ok && !expired {
- if i, ok := c.get(qname, qtype, do); ok {
resp := i.toMsg(r)
state.SizeAndDo(resp)
w.WriteMsg(resp)
cacheHitCount.WithLabelValues(zone).Inc()
+
return dns.RcodeSuccess, nil
}
+
cacheMissCount.WithLabelValues(zone).Inc()
- crr := NewCachingResponseWriter(w, c.cache, c.cap)
+ crr := &ResponseWriter{w, c}
return c.Next.ServeDNS(ctx, crr, r)
}
-func (c Cache) get(qname string, qtype uint16, do bool) (*item, bool) {
- nxdomain := nameErrorKey(qname, do)
- if i, ok := c.cache.Get(nxdomain); ok {
- return i.(*item), true
+func (c *Cache) get(qname string, qtype uint16, do bool) (*item, bool, bool) {
+ k := rawKey(qname, qtype, do)
+
+ if i, ok := c.ncache.Get(k); ok {
+ return i.(*item), ok, i.(*item).expired(time.Now())
}
- // TODO(miek): delegation was added double check
- successOrNoData := successKey(qname, qtype, do)
- if i, ok := c.cache.Get(successOrNoData); ok {
- return i.(*item), true
+ if i, ok := c.pcache.Get(k); ok {
+ return i.(*item), ok, i.(*item).expired(time.Now())
}
- return nil, false
+ return nil, false, false
}
var (