MCPcopy
hub / github.com/kopia/kopia / Put

Method Put

internal/cache/persistent_lru_cache.go:165–207  ·  view source on GitHub ↗

Put adds the provided key-value pair to the cache.

(ctx context.Context, key string, data gather.Bytes)

Source from the content-addressed store, hash-verified

163
164// Put adds the provided key-value pair to the cache.
165func (c *PersistentCache) Put(ctx context.Context, key string, data gather.Bytes) {
166 if c == nil {
167 return
168 }
169
170 c.listCacheMutex.Lock()
171 defer c.listCacheMutex.Unlock()
172
173 // make sure the cache has enough room for the new item including any protection overhead.
174 l := data.Length() + c.storageProtection.OverheadBytes()
175 c.pendingWriteBytes += int64(l)
176 c.sweepLocked(ctx)
177
178 // LOCK RELEASED for expensive operations
179 c.listCacheMutex.Unlock()
180
181 var protected gather.WriteBuffer
182 defer protected.Close()
183
184 c.storageProtection.Protect(key, data, &protected)
185
186 if protected.Length() != l {
187 log(ctx).Panicf("protection overhead mismatch, assumed %v got %v", l, protected.Length())
188 }
189
190 var mtime time.Time
191
192 if err := c.cacheStorage.PutBlob(ctx, blob.ID(key), protected.Bytes(), blob.PutOptions{GetModTime: &mtime}); err != nil {
193 c.reportStoreError()
194
195 log(ctx).Errorf("unable to add %v to %v: %v", key, c.description, err)
196 }
197
198 c.listCacheMutex.Lock()
199 // LOCK RE-ACQUIRED
200
201 c.pendingWriteBytes -= int64(protected.Length())
202 c.listCache.AddOrUpdate(blob.Metadata{
203 BlobID: blob.ID(key),
204 Length: int64(protected.Bytes().Length()),
205 Timestamp: mtime,
206 })
207}
208
209// Close closes the instance of persistent cache possibly waiting for at least one sweep to complete.
210func (c *PersistentCache) Close(_ context.Context) {

Calls 14

sweepLockedMethod · 0.95
CloseMethod · 0.95
LengthMethod · 0.95
BytesMethod · 0.95
IDTypeAlias · 0.92
reportStoreErrorMethod · 0.80
ErrorfMethod · 0.80
AddOrUpdateMethod · 0.80
LockMethod · 0.65
UnlockMethod · 0.65
LengthMethod · 0.65
OverheadBytesMethod · 0.65