Put adds the provided key-value pair to the cache.
(ctx context.Context, key string, data gather.Bytes)
| 163 | |
| 164 | // Put adds the provided key-value pair to the cache. |
| 165 | func (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. |
| 210 | func (c *PersistentCache) Close(_ context.Context) { |