aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/cache/cache.go
diff options
context:
space:
mode:
authorGravatar Frank Riley <fhriley@gmail.com> 2021-03-21 08:58:16 -0700
committerGravatar GitHub <noreply@github.com> 2021-03-21 16:58:16 +0100
commit5b9b079dabc7f71463cea3f0c6a92f338935039d (patch)
treef592af3c022675642e62e959b42803635799da26 /plugin/pkg/cache/cache.go
parented3f287fe8114255739b2c1757a7621ba913244c (diff)
downloadcoredns-5b9b079dabc7f71463cea3f0c6a92f338935039d.tar.gz
coredns-5b9b079dabc7f71463cea3f0c6a92f338935039d.tar.zst
coredns-5b9b079dabc7f71463cea3f0c6a92f338935039d.zip
Add cache eviction metrics to the cache plugin (#4411)
Signed-off-by: Frank Riley <fhriley@gmail.com>
Diffstat (limited to 'plugin/pkg/cache/cache.go')
-rw-r--r--plugin/pkg/cache/cache.go11
1 files changed, 8 insertions, 3 deletions
diff --git a/plugin/pkg/cache/cache.go b/plugin/pkg/cache/cache.go
index 3a2c8ff7f..19a4e7a80 100644
--- a/plugin/pkg/cache/cache.go
+++ b/plugin/pkg/cache/cache.go
@@ -45,9 +45,10 @@ func New(size int) *Cache {
}
// Add adds a new element to the cache. If the element already exists it is overwritten.
-func (c *Cache) Add(key uint64, el interface{}) {
+// Returns true if an existing element was evicted to make room for this element.
+func (c *Cache) Add(key uint64, el interface{}) bool {
shard := key & (shardSize - 1)
- c.shards[shard].Add(key, el)
+ return c.shards[shard].Add(key, el)
}
// Get looks up element index under key.
@@ -75,18 +76,22 @@ func (c *Cache) Len() int {
func newShard(size int) *shard { return &shard{items: make(map[uint64]interface{}), size: size} }
// Add adds element indexed by key into the cache. Any existing element is overwritten
-func (s *shard) Add(key uint64, el interface{}) {
+// Returns true if an existing element was evicted to make room for this element.
+func (s *shard) Add(key uint64, el interface{}) bool {
+ eviction := false
s.Lock()
if len(s.items) >= s.size {
if _, ok := s.items[key]; !ok {
for k := range s.items {
delete(s.items, k)
+ eviction = true
break
}
}
}
s.items[key] = el
s.Unlock()
+ return eviction
}
// Remove removes the element indexed by key from the cache.