mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2024-12-23 11:57:17 +00:00
9afc6816d2
It seems there is no fully typed library implementation of an LRU cache. So I wrote one. Method names are the same as github.com/hashicorp/golang-lru, and the new type can be used as a drop-in replacement. Two reasons to do this: - It's much easier to understand what a cache is for when the types are right there. - Performance: the new implementation is slightly faster and performs zero memory allocations in Add when the cache is at capacity. Overall, memory usage of the cache is much reduced because keys are values are no longer wrapped in interface.
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
// 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"
|
|
)
|
|
|
|
// 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.
|
|
//
|
|
// 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.
|
|
type SizeConstrainedCache[K comparable, V blobType] struct {
|
|
size uint64
|
|
maxSize uint64
|
|
lru BasicLRU[K, V]
|
|
lock sync.Mutex
|
|
}
|
|
|
|
// NewSizeConstrainedCache creates a new size-constrained LRU cache.
|
|
func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V] {
|
|
return &SizeConstrainedCache[K, V]{
|
|
size: 0,
|
|
maxSize: maxSize,
|
|
lru: NewBasicLRU[K, V](math.MaxInt),
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
func (c *SizeConstrainedCache[K, V]) Add(key K, value V) (evicted bool) {
|
|
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
|
|
}
|
|
targetSize -= uint64(len(v))
|
|
}
|
|
c.size = targetSize
|
|
}
|
|
c.lru.Add(key, value)
|
|
return evicted
|
|
}
|
|
|
|
// Get looks up a key's value from the cache.
|
|
func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool) {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
|
|
return c.lru.Get(key)
|
|
}
|