MCPcopy
hub / github.com/restic/restic / Add

Method Add

internal/bloblru/cache.go:49–78  ·  view source on GitHub ↗

Add adds key id with value blob to c. It may return an evicted buffer for reuse.

(id restic.ID, blob []byte)

Source from the content-addressed store, hash-verified

47// Add adds key id with value blob to c.
48// It may return an evicted buffer for reuse.
49func (c *Cache) Add(id restic.ID, blob []byte) (old []byte) {
50 debug.Log("bloblru.Cache: add %v", id)
51
52 size := cap(blob) + overhead
53 if size > c.size {
54 return
55 }
56
57 c.mu.Lock()
58 defer c.mu.Unlock()
59
60 if c.c.Contains(id) { // Doesn't update the recency list.
61 return
62 }
63
64 // This loop takes at most min(maxEntries, maxchunksize/overhead)
65 // iterations.
66 for size > c.free {
67 _, b, _ := c.c.RemoveOldest()
68 if cap(b) > cap(old) {
69 // We can only return one buffer, so pick the largest.
70 old = b
71 }
72 }
73
74 c.c.Add(id, blob)
75 c.free -= size
76
77 return old
78}
79
80func (c *Cache) Get(id restic.ID) ([]byte, bool) {
81 c.mu.Lock()

Callers 10

GetOrComputeMethod · 0.95
TestErrorBackendFunction · 0.45
IsOldFunction · 0.45
runFunction · 0.45
newBackendFunction · 0.45
TestFreezeFunction · 0.45
getWarmupStatusMethod · 0.45
TestRetryAtLeastOnceFunction · 0.45
TestCacheFunction · 0.45
BenchmarkAddFunction · 0.45

Calls 3

LogFunction · 0.92
LockMethod · 0.45
UnlockMethod · 0.45

Tested by 5

TestErrorBackendFunction · 0.36
TestFreezeFunction · 0.36
TestRetryAtLeastOnceFunction · 0.36
TestCacheFunction · 0.36
BenchmarkAddFunction · 0.36