Skip to content

Commit 660bbbc

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 660bbbc

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Diff for: cache/dynacache/dynacache.go

+9-9
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,22 @@ 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) {
477+
match := shouldDelete(key, v)
478+
clear := match || opts.ClearWhen == ClearOnRebuild
479+
480+
if clear {
481+
resource.MarkStale(v)
482+
482483
p.trace.Log(
483484
logg.StringFunc(
484485
func() string {
485486
return fmt.Sprintf("first pass: clearing cache key %v", key)
486487
},
487488
),
488489
)
489-
return true
490490
}
491-
return false
491+
492+
return clear
492493
})
493494
}
494495

@@ -562,7 +563,6 @@ type PartitionManager interface {
562563
const (
563564
ClearOnRebuild ClearWhen = iota + 1
564565
ClearOnChange
565-
ClearNever
566566
)
567567

568568
type ClearWhen int

0 commit comments

Comments
 (0)