2022-11-09 08:06:02 +01:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package lru
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-11-14 15:41:56 +01:00
|
|
|
// blobType is the type constraint for values stored in SizeConstrainedCache.
|
|
|
|
type blobType interface {
|
|
|
|
~[]byte | ~string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SizeConstrainedCache is a cache where capacity is in bytes (instead of item count). When the cache
|
|
|
|
// is at capacity, and a new item is added, older items are evicted until the size
|
|
|
|
// constraint is met.
|
|
|
|
//
|
2022-11-09 08:06:02 +01:00
|
|
|
// OBS: This cache assumes that items are content-addressed: keys are unique per content.
|
|
|
|
// In other words: two Add(..) with the same key K, will always have the same value V.
|
2022-11-14 15:41:56 +01:00
|
|
|
type SizeConstrainedCache[K comparable, V blobType] struct {
|
2022-11-09 08:06:02 +01:00
|
|
|
size uint64
|
|
|
|
maxSize uint64
|
2022-11-14 15:41:56 +01:00
|
|
|
lru BasicLRU[K, V]
|
2022-11-11 19:48:36 +01:00
|
|
|
lock sync.Mutex
|
2022-11-09 08:06:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-14 15:41:56 +01:00
|
|
|
// NewSizeConstrainedCache creates a new size-constrained LRU cache.
|
|
|
|
func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V] {
|
|
|
|
return &SizeConstrainedCache[K, V]{
|
2022-11-09 08:06:02 +01:00
|
|
|
size: 0,
|
2022-11-14 15:41:56 +01:00
|
|
|
maxSize: maxSize,
|
|
|
|
lru: NewBasicLRU[K, V](math.MaxInt),
|
2022-11-09 08:06:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a value to the cache. Returns true if an eviction occurred.
|
|
|
|
// OBS: This cache assumes that items are content-addressed: keys are unique per content.
|
|
|
|
// In other words: two Add(..) with the same key K, will always have the same value V.
|
|
|
|
// OBS: The value is _not_ copied on Add, so the caller must not modify it afterwards.
|
2022-11-14 15:41:56 +01:00
|
|
|
func (c *SizeConstrainedCache[K, V]) Add(key K, value V) (evicted bool) {
|
2022-11-09 08:06:02 +01:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
// Unless it is already present, might need to evict something.
|
|
|
|
// OBS: If it is present, we still call Add internally to bump the recentness.
|
|
|
|
if !c.lru.Contains(key) {
|
|
|
|
targetSize := c.size + uint64(len(value))
|
|
|
|
for targetSize > c.maxSize {
|
|
|
|
evicted = true
|
|
|
|
_, v, ok := c.lru.RemoveOldest()
|
|
|
|
if !ok {
|
|
|
|
// list is now empty. Break
|
|
|
|
break
|
|
|
|
}
|
2022-11-14 15:41:56 +01:00
|
|
|
targetSize -= uint64(len(v))
|
2022-11-09 08:06:02 +01:00
|
|
|
}
|
|
|
|
c.size = targetSize
|
|
|
|
}
|
|
|
|
c.lru.Add(key, value)
|
|
|
|
return evicted
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get looks up a key's value from the cache.
|
2022-11-14 15:41:56 +01:00
|
|
|
func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool) {
|
2022-11-11 19:48:36 +01:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2022-11-09 08:06:02 +01:00
|
|
|
|
2022-11-14 15:41:56 +01:00
|
|
|
return c.lru.Get(key)
|
2022-11-09 08:06:02 +01:00
|
|
|
}
|