mirror of
https://github.com/restic/restic.git
synced 2025-10-10 08:33:01 +00:00
Move Blobcache into dedicated internal package
This commit is contained in:
87
internal/bloblru/cache.go
Normal file
87
internal/bloblru/cache.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package bloblru
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
|
||||
"github.com/hashicorp/golang-lru/simplelru"
|
||||
)
|
||||
|
||||
// Crude estimate of the overhead per blob: a SHA-256, a linked list node
|
||||
// and some pointers. See comment in Cache.add.
|
||||
const overhead = len(restic.ID{}) + 64
|
||||
|
||||
// A Cache is a fixed-size LRU cache of blob contents.
|
||||
// It is safe for concurrent access.
|
||||
type Cache struct {
|
||||
mu sync.Mutex
|
||||
c *simplelru.LRU
|
||||
|
||||
free, size int // Current and max capacity, in bytes.
|
||||
}
|
||||
|
||||
// Construct a blob cache that stores at most size bytes worth of blobs.
|
||||
func New(size int) *Cache {
|
||||
c := &Cache{
|
||||
free: size,
|
||||
size: size,
|
||||
}
|
||||
|
||||
// NewLRU wants us to specify some max. number of entries, else it errors.
|
||||
// The actual maximum will be smaller than size/overhead, because we
|
||||
// evict entries (RemoveOldest in add) to maintain our size bound.
|
||||
maxEntries := size / overhead
|
||||
lru, err := simplelru.NewLRU(maxEntries, c.evict)
|
||||
if err != nil {
|
||||
panic(err) // Can only be maxEntries <= 0.
|
||||
}
|
||||
c.c = lru
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Cache) Add(id restic.ID, blob []byte) {
|
||||
debug.Log("bloblru.Cache: add %v", id)
|
||||
|
||||
size := len(blob) + overhead
|
||||
if size > c.size {
|
||||
return
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
var key interface{} = id
|
||||
|
||||
if c.c.Contains(key) { // Doesn't update the recency list.
|
||||
return
|
||||
}
|
||||
|
||||
// This loop takes at most min(maxEntries, maxchunksize/overhead)
|
||||
// iterations.
|
||||
for size > c.free {
|
||||
c.c.RemoveOldest()
|
||||
}
|
||||
|
||||
c.c.Add(key, blob)
|
||||
c.free -= size
|
||||
}
|
||||
|
||||
func (c *Cache) Get(id restic.ID) ([]byte, bool) {
|
||||
c.mu.Lock()
|
||||
value, ok := c.c.Get(id)
|
||||
c.mu.Unlock()
|
||||
|
||||
debug.Log("bloblru.Cache: get %v, hit %v", id, ok)
|
||||
|
||||
blob, ok := value.([]byte)
|
||||
return blob, ok
|
||||
}
|
||||
|
||||
func (c *Cache) evict(key, value interface{}) {
|
||||
blob := value.([]byte)
|
||||
debug.Log("bloblru.Cache: evict %v, %d bytes", key, len(blob))
|
||||
c.free += len(blob) + overhead
|
||||
}
|
Reference in New Issue
Block a user