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.
This commit is contained in:
Bjørn Erik Pedersen 2024-04-29 21:44:42 +02:00
parent 3d40aba512
commit 0d1f08a6cd
No known key found for this signature in database

View file

@ -74,7 +74,6 @@ func New(opts Options) *Cache {
evictedIdentities.Push(id) evictedIdentities.Push(id)
return false return false
}) })
resource.MarkStale(v)
} }
c := &Cache{ c := &Cache{
@ -458,11 +457,8 @@ func (p *Partition[K, V]) clearMatching(predicate func(k, v any) bool) {
func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) { func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
opts := p.getOptions() opts := p.getOptions()
if opts.ClearWhen == ClearNever {
return
}
if opts.ClearWhen == ClearOnRebuild { if opts.ClearWhen == ClearOnRebuild && len(changeset) == 0 {
// Clear all. // Clear all.
p.Clear() p.Clear()
return return
@ -502,7 +498,14 @@ func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
// Second pass needs to be done in a separate loop to catch any // Second pass needs to be done in a separate loop to catch any
// elements marked as stale in the other partitions. // elements marked as stale in the other partitions.
p.c.DeleteFunc(func(key K, v V) bool { p.c.DeleteFunc(func(key K, v V) bool {
if shouldDelete(key, v) { match := shouldDelete(key, v)
clear := match || opts.ClearWhen == ClearOnRebuild
if match {
resource.MarkStale(v)
}
if clear {
p.trace.Log( p.trace.Log(
logg.StringFunc( logg.StringFunc(
func() string { func() string {
@ -510,9 +513,9 @@ func (p *Partition[K, V]) clearOnRebuild(changeset ...identity.Identity) {
}, },
), ),
) )
return true
} }
return false
return clear
}) })
} }
@ -586,7 +589,6 @@ type PartitionManager interface {
const ( const (
ClearOnRebuild ClearWhen = iota + 1 ClearOnRebuild ClearWhen = iota + 1
ClearOnChange ClearOnChange
ClearNever
) )
type ClearWhen int type ClearWhen int