mirror of
https://github.com/coredns/coredns.git
synced 2026-02-07 16:03:09 -05:00
refactor(cache): modernize with generics (#7842)
This commit is contained in:
8
plugin/cache/cache.go
vendored
8
plugin/cache/cache.go
vendored
@@ -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,
|
||||
|
||||
6
plugin/cache/cache_test.go
vendored
6
plugin/cache/cache_test.go
vendored
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
plugin/cache/handler.go
vendored
18
plugin/cache/handler.go
vendored
@@ -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
|
||||
}
|
||||
|
||||
4
plugin/cache/setup.go
vendored
4
plugin/cache/setup.go
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user