refactor(cache): modernize with generics (#7842)

This commit is contained in:
vflaux
2026-02-04 02:23:53 +01:00
committed by GitHub
parent 923a8b5d2b
commit 30c20b52ff
13 changed files with 70 additions and 68 deletions

View File

@@ -24,12 +24,12 @@ type Cache struct {
zonesMetricLabel string
viewMetricLabel string
ncache *cache.Cache
ncache *cache.Cache[*item]
ncap int
nttl time.Duration
minnttl time.Duration
pcache *cache.Cache
pcache *cache.Cache[*item]
pcap int
pttl time.Duration
minpttl time.Duration
@@ -61,11 +61,11 @@ func New() *Cache {
return &Cache{
Zones: []string{"."},
pcap: defaultCap,
pcache: cache.New(defaultCap),
pcache: cache.New[*item](defaultCap),
pttl: maxTTL,
minpttl: minTTL,
ncap: defaultCap,
ncache: cache.New(defaultCap),
ncache: cache.New[*item](defaultCap),
nttl: maxNTTL,
minnttl: minNTTL,
failttl: minNTTL,

View File

@@ -712,8 +712,8 @@ func TestCacheWildcardMetadata(t *testing.T) {
}
_, k := key(qname, w.Msg, response.NoError, state.Do(), state.Req.CheckingDisabled)
i, _ := c.pcache.Get(k)
if i.(*item).wildcard != wildcard {
t.Errorf("expected wildcard response to enter cache with cache item's wildcard = %q, got %q", wildcard, i.(*item).wildcard)
if i.wildcard != wildcard {
t.Errorf("expected wildcard response to enter cache with cache item's wildcard = %q, got %q", wildcard, i.wildcard)
}
// 2. Test retrieving the cached item from cache and writing its wildcard value to metadata
@@ -728,7 +728,7 @@ func TestCacheWildcardMetadata(t *testing.T) {
t.Fatal("expected metadata func for wildcard response retrieved from cache, got nil")
}
if f() != wildcard {
t.Errorf("after retrieving wildcard item from cache, expected \"zone/wildcard\" metadata value to be %q, got %q", wildcard, i.(*item).wildcard)
t.Errorf("after retrieving wildcard item from cache, expected \"zone/wildcard\" metadata value to be %q, got %q", wildcard, i.wildcard)
}
}

View File

@@ -127,19 +127,17 @@ func (c *Cache) getIfNotStale(now time.Time, state request.Request, server strin
cacheRequests.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc()
if i, ok := c.ncache.Get(k); ok {
itm := i.(*item)
ttl := itm.ttl(now)
if itm.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
ttl := i.ttl(now)
if i.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
cacheHits.WithLabelValues(server, Denial, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i.(*item)
return i
}
}
if i, ok := c.pcache.Get(k); ok {
itm := i.(*item)
ttl := itm.ttl(now)
if itm.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
ttl := i.ttl(now)
if i.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
cacheHits.WithLabelValues(server, Success, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i.(*item)
return i
}
}
cacheMisses.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc()
@@ -150,10 +148,10 @@ func (c *Cache) getIfNotStale(now time.Time, state request.Request, server strin
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 {
return i.(*item)
return i
}
if i, ok := c.pcache.Get(k); ok {
return i.(*item)
return i
}
return nil
}

View File

@@ -253,8 +253,8 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
ca.Zones = origins
ca.zonesMetricLabel = strings.Join(origins, ",")
ca.pcache = cache.New(ca.pcap)
ca.ncache = cache.New(ca.ncap)
ca.pcache = cache.New[*item](ca.pcap)
ca.ncache = cache.New[*item](ca.ncap)
}
return ca, nil