plugin/cache: Fix: metric `cache miss total`  shall include ttl case
In the case of ttl <= 0, `cacheMisses` should increase its counter.
@@ -27,8 +27,8 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
server := metrics.WithServer(ctx)
- i, ttl := c.get(now, state, server)
- if i != nil && ttl > 0 {
+ i, found := c.get(now, state, server)
+ if i != nil && found {
resp := i.toMsg(r, now)
state.SizeAndDo(resp)
@@ -36,6 +36,7 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
w.WriteMsg(resp)
if c.prefetch > 0 {
+ ttl := i.ttl(now)
i.Freq.Update(c.duration, now)
threshold := int(math.Ceil(float64(c.percentage) / 100 * float64(i.origTTL)))
@@ -64,20 +65,20 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// Name implements the Handler interface.
func (c *Cache) Name() string { return "cache" }
-func (c *Cache) get(now time.Time, state request.Request, server string) (*item, int) {
+func (c *Cache) get(now time.Time, state request.Request, server string) (*item, bool) {
k := hash(state.Name(), state.QType(), state.Do())
- if i, ok := c.ncache.Get(k); ok {
+ if i, ok := c.ncache.Get(k); ok && i.(*item).ttl(now) > 0 {
cacheHits.WithLabelValues(server, Denial).Inc()
- return i.(*item), i.(*item).ttl(now)
+ return i.(*item), true
}
- if i, ok := c.pcache.Get(k); ok {
+ if i, ok := c.pcache.Get(k); ok && i.(*item).ttl(now) > 0 {
cacheHits.WithLabelValues(server, Success).Inc()
- return i.(*item), i.(*item).ttl(now)
+ return i.(*item), true
}
cacheMisses.WithLabelValues(server).Inc()
- return nil, 0
+ return nil, false
}
func (c *Cache) exists(state request.Request) *item {
