MCPcopy
hub / github.com/kopia/kopia / GetOrLoad

Method GetOrLoad

internal/cache/persistent_lru_cache.go:60–91  ·  view source on GitHub ↗

GetOrLoad is utility function gets the provided item from the cache or invokes the provided fetch function. The function also appends and verifies HMAC checksums using provided secret on all cached items to ensure data integrity.

(ctx context.Context, key string, fetch func(output *gather.WriteBuffer) error, output *gather.WriteBuffer)

Source from the content-addressed store, hash-verified

58// GetOrLoad is utility function gets the provided item from the cache or invokes the provided fetch function.
59// The function also appends and verifies HMAC checksums using provided secret on all cached items to ensure data integrity.
60func (c *PersistentCache) GetOrLoad(ctx context.Context, key string, fetch func(output *gather.WriteBuffer) error, output *gather.WriteBuffer) error {
61 if c == nil {
62 // special case - also works on non-initialized cache pointer.
63 return fetch(output)
64 }
65
66 if c.getFull(ctx, key, output) {
67 return nil
68 }
69
70 output.Reset()
71
72 c.exclusiveLock(key)
73 defer c.exclusiveUnlock(key)
74
75 // check again while holding the mutex
76 if c.getFull(ctx, key, output) {
77 return nil
78 }
79
80 if err := fetch(output); err != nil {
81 c.reportMissError()
82
83 return err
84 }
85
86 c.reportMissBytes(int64(output.Length()))
87
88 c.Put(ctx, key, output.Bytes())
89
90 return nil
91}
92
93// getFull fetches the contents of a full blob. Returns false if not found.
94func (c *PersistentCache) getFull(ctx context.Context, key string, output *gather.WriteBuffer) bool {

Callers 4

TestPersistentLRUCacheFunction · 0.95
GetContentMethod · 0.80
GetEncryptedBlobMethod · 0.80

Calls 9

getFullMethod · 0.95
exclusiveLockMethod · 0.95
exclusiveUnlockMethod · 0.95
PutMethod · 0.95
reportMissErrorMethod · 0.80
reportMissBytesMethod · 0.80
ResetMethod · 0.65
LengthMethod · 0.65
BytesMethod · 0.45

Tested by 2

TestPersistentLRUCacheFunction · 0.76