Skip to content

Commit f34528e

Browse files
committed
cache/dynacache: Don't mark all evicted items as stale
Limit that to the evicted items that matches the given change set. Currently this doesn't make any practical difference, but it would make the stale flag more general useful.
1 parent 1961327 commit f34528e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Diff for: cache/dynacache/dynacache.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func New(opts Options) *Cache {
7474
evictedIdentities.Push(id)
7575
return false
7676
})
77-
resource.MarkStale(v)
7877
}
7978

8079
c := &Cache{
@@ -434,11 +433,8 @@ func (p *Partition[K, V]) clearMatching(predicate func(k, v any) bool) {
434433

435434
func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
436435
opts := p.getOptions()
437-
if opts.ClearWhen == ClearNever {
438-
return
439-
}
440436

441-
if opts.ClearWhen == ClearOnRebuild {
437+
if opts.ClearWhen == ClearOnRebuild && len(changeset) == 0 {
442438
// Clear all.
443439
p.Clear()
444440
return
@@ -478,17 +474,23 @@ func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
478474
// Second pass needs to be done in a separate loop to catch any
479475
// elements marked as stale in the other partitions.
480476
p.c.DeleteFunc(func(key K, v V) bool {
481-
if shouldDelete(key, v) {
482-
p.trace.Log(
483-
logg.StringFunc(
484-
func() string {
485-
return fmt.Sprintf("first pass: clearing cache key %v", key)
486-
},
487-
),
488-
)
489-
return true
477+
match := shouldDelete(key, v)
478+
clear := match || opts.ClearWhen == ClearOnRebuild
479+
480+
if match {
481+
if clear {
482+
p.trace.Log(
483+
logg.StringFunc(
484+
func() string {
485+
return fmt.Sprintf("first pass: clearing cache key %v", key)
486+
},
487+
),
488+
)
489+
}
490+
resource.MarkStale(v)
490491
}
491-
return false
492+
493+
return clear
492494
})
493495
}
494496

@@ -562,7 +564,6 @@ type PartitionManager interface {
562564
const (
563565
ClearOnRebuild ClearWhen = iota + 1
564566
ClearOnChange
565-
ClearNever
566567
)
567568

568569
type ClearWhen int

0 commit comments

Comments
 (0)