Skip to content

Commit 0d1f08a

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 3d40aba commit 0d1f08a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Diff for: cache/dynacache/dynacache.go

+11-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{
@@ -458,11 +457,8 @@ func (p *Partition[K, V]) clearMatching(predicate func(k, v any) bool) {
458457

459458
func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
460459
opts := p.getOptions()
461-
if opts.ClearWhen == ClearNever {
462-
return
463-
}
464460

465-
if opts.ClearWhen == ClearOnRebuild {
461+
if opts.ClearWhen == ClearOnRebuild && len(changeset) == 0 {
466462
// Clear all.
467463
p.Clear()
468464
return
@@ -502,17 +498,24 @@ func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
502498
// Second pass needs to be done in a separate loop to catch any
503499
// elements marked as stale in the other partitions.
504500
p.c.DeleteFunc(func(key K, v V) bool {
505-
if shouldDelete(key, v) {
501+
match := shouldDelete(key, v)
502+
clear := match || opts.ClearWhen == ClearOnRebuild
503+
504+
if match {
505+
resource.MarkStale(v)
506+
}
507+
508+
if clear {
506509
p.trace.Log(
507510
logg.StringFunc(
508511
func() string {
509512
return fmt.Sprintf("first pass: clearing cache key %v", key)
510513
},
511514
),
512515
)
513-
return true
514516
}
515-
return false
517+
518+
return clear
516519
})
517520
}
518521

@@ -586,7 +589,6 @@ type PartitionManager interface {
586589
const (
587590
ClearOnRebuild ClearWhen = iota + 1
588591
ClearOnChange
589-
ClearNever
590592
)
591593

592594
type ClearWhen int

0 commit comments

Comments
 (0)