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 <raisa.kabir2010@gmail.com>
This commit is contained in:
Raisa Kabir
2026-01-05 22:24:04 +06:00
committed by GitHub
parent 6dca5b26d1
commit adba778626
2 changed files with 6 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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 {