From adba77862622fd4aa57a796eb082752589e2087c Mon Sep 17 00:00:00 2001 From: Raisa Kabir Date: Mon, 5 Jan 2026 22:24:04 +0600 Subject: [PATCH] Refactor: Update the cache getter function (#7800) Rename the cache getter function to reflect the true functionality of retrieving with TTL consideration. Refs: https://github.com/coredns/coredns/issues/6505 Signed-off-by: Raisa Kabir --- plugin/cache/cache_test.go | 4 ++-- plugin/cache/handler.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/cache/cache_test.go b/plugin/cache/cache_test.go index ed1d38c41..25b301b98 100644 --- a/plugin/cache/cache_test.go +++ b/plugin/cache/cache_test.go @@ -295,7 +295,7 @@ func TestCacheInsertion(t *testing.T) { } // Attempt to retrieve cache entry - i := c.getIgnoreTTL(time.Now().UTC(), state, "dns://:53") + i := c.getIfNotStale(time.Now().UTC(), state, "dns://:53") found := i != nil if !tc.shouldCache && found { @@ -879,7 +879,7 @@ func TestCacheSeparation(t *testing.T) { m = cacheMsg(m, tc.query) state = request.Request{W: &test.ResponseWriter{}, Req: m} - item := c.getIgnoreTTL(time.Now().UTC(), state, "dns://:53") + item := c.getIfNotStale(time.Now().UTC(), state, "dns://:53") found := item != nil if !tc.expectCached && found { diff --git a/plugin/cache/handler.go b/plugin/cache/handler.go index c45801ad6..b6815ee0e 100644 --- a/plugin/cache/handler.go +++ b/plugin/cache/handler.go @@ -34,7 +34,7 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) // in which upstream doesn't support DNSSEC, the two cache items will effectively be the same. Regardless, any // DNSSEC RRs in the response are written to cache with the response. - i := c.getIgnoreTTL(now, state, server) + i := c.getIfNotStale(now, state, server) if i == nil { crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server, do: do, ad: ad, cd: cd, nexcept: c.nexcept, pexcept: c.pexcept, wildcardFunc: wildcardFunc(ctx)} @@ -121,8 +121,8 @@ func (c *Cache) shouldPrefetch(i *item, now time.Time) bool { // Name implements the Handler interface. func (c *Cache) Name() string { return "cache" } -// getIgnoreTTL unconditionally returns an item if it exists in the cache. -func (c *Cache) getIgnoreTTL(now time.Time, state request.Request, server string) *item { +// getIfNotStale returns an item if it exists in the cache and has not expired. +func (c *Cache) getIfNotStale(now time.Time, state request.Request, server string) *item { k := hash(state.Name(), state.QType(), state.Do(), state.Req.CheckingDisabled) cacheRequests.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc() @@ -146,6 +146,7 @@ func (c *Cache) getIgnoreTTL(now time.Time, state request.Request, server string return nil } +// exists unconditionally returns an item if it exists in the cache. func (c *Cache) exists(state request.Request) *item { k := hash(state.Name(), state.QType(), state.Do(), state.Req.CheckingDisabled) if i, ok := c.ncache.Get(k); ok {