mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
fix onEvicted
This commit is contained in:
parent
eb4e231434
commit
d053edfd74
2 changed files with 8 additions and 6 deletions
|
|
@ -60,12 +60,12 @@ func (c *BasicLRU[K, V]) Add(key K, value V) (evicted bool) {
|
|||
var elem *listElem[K]
|
||||
if c.Len() >= c.cap {
|
||||
elem = c.list.removeLast()
|
||||
delete(c.items, elem.v)
|
||||
evicted = true
|
||||
if c.onEvicted != nil {
|
||||
v := c.items[elem.v]
|
||||
c.onEvicted(elem.v, v.value)
|
||||
}
|
||||
delete(c.items, elem.v)
|
||||
evicted = true
|
||||
} else {
|
||||
elem = new(listElem[K])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,12 +62,14 @@ func New(datadir string) (*EraDatabase, error) {
|
|||
}
|
||||
db := &EraDatabase{datadir: datadir, cache: lru.NewCache[uint64, *era.Era](openFileLimit)}
|
||||
db.cache.OnEvicted(func(key uint64, value *era.Era) {
|
||||
if value == nil {
|
||||
log.Warn("Era1 cache evicted nil value", "epoch", key)
|
||||
return
|
||||
}
|
||||
// Close the era1 file when it is evicted from the cache
|
||||
// to avoid leaks.
|
||||
if value != nil {
|
||||
if err := value.Close(); err != nil {
|
||||
log.Warn("Error closing era1 file", "epoch", key, "err", err)
|
||||
}
|
||||
if err := value.Close(); err != nil {
|
||||
log.Warn("Error closing era1 file", "epoch", key, "err", err)
|
||||
}
|
||||
})
|
||||
log.Info("Opened erastore", "datadir", datadir)
|
||||
|
|
|
|||
Loading…
Reference in a new issue