diff --git a/cache/dynacache/dynacache.go b/cache/dynacache/dynacache.go index 87ef68f5be2..903ec469a0b 100644 --- a/cache/dynacache/dynacache.go +++ b/cache/dynacache/dynacache.go @@ -74,7 +74,6 @@ func New(opts Options) *Cache { evictedIdentities.Push(id) return false }) - resource.MarkStale(v) } c := &Cache{ @@ -434,11 +433,8 @@ func (p *Partition[K, V]) clearMatching(predicate func(k, v any) bool) { func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) { opts := p.getOptions() - if opts.ClearWhen == ClearNever { - return - } - if opts.ClearWhen == ClearOnRebuild { + if opts.ClearWhen == ClearOnRebuild && len(changeset) == 0 { // Clear all. p.Clear() return @@ -478,17 +474,23 @@ func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) { // Second pass needs to be done in a separate loop to catch any // elements marked as stale in the other partitions. p.c.DeleteFunc(func(key K, v V) bool { - if shouldDelete(key, v) { - p.trace.Log( - logg.StringFunc( - func() string { - return fmt.Sprintf("first pass: clearing cache key %v", key) - }, - ), - ) - return true + match := shouldDelete(key, v) + clear := match || opts.ClearWhen == ClearOnRebuild + + if match { + if clear { + p.trace.Log( + logg.StringFunc( + func() string { + return fmt.Sprintf("first pass: clearing cache key %v", key) + }, + ), + ) + } + resource.MarkStale(v) } - return false + + return clear }) } @@ -562,7 +564,6 @@ type PartitionManager interface { const ( ClearOnRebuild ClearWhen = iota + 1 ClearOnChange - ClearNever ) type ClearWhen int