NewPersistentCache creates the persistent cache in the provided storage.
(ctx context.Context, description string, cacheStorage Storage, storageProtection cacheprot.StorageProtection, sweep SweepSettings, mr *metrics.Registry, timeNow func() time.Time)
| 430 | |
| 431 | // NewPersistentCache creates the persistent cache in the provided storage. |
| 432 | func NewPersistentCache(ctx context.Context, description string, cacheStorage Storage, storageProtection cacheprot.StorageProtection, sweep SweepSettings, mr *metrics.Registry, timeNow func() time.Time) (*PersistentCache, error) { |
| 433 | if cacheStorage == nil { |
| 434 | return nil, nil |
| 435 | } |
| 436 | |
| 437 | sweep = sweep.applyDefaults() |
| 438 | |
| 439 | if storageProtection == nil { |
| 440 | storageProtection = cacheprot.NoProtection() |
| 441 | } |
| 442 | |
| 443 | c := &PersistentCache{ |
| 444 | cacheStorage: cacheStorage, |
| 445 | sweep: sweep, |
| 446 | description: description, |
| 447 | storageProtection: storageProtection, |
| 448 | metricsStruct: initMetricsStruct(mr, description), |
| 449 | listCache: newContentMetadataHeap(), |
| 450 | timeNow: timeNow, |
| 451 | lastCacheWarning: time.Time{}, |
| 452 | } |
| 453 | |
| 454 | if c.timeNow == nil { |
| 455 | c.timeNow = clock.Now |
| 456 | } |
| 457 | |
| 458 | // verify that cache storage is functional by listing from it |
| 459 | if _, err := c.cacheStorage.GetMetadata(ctx, "test-blob"); err != nil && !errors.Is(err, blob.ErrBlobNotFound) { |
| 460 | return nil, errors.Wrapf(err, "unable to open %v", c.description) |
| 461 | } |
| 462 | |
| 463 | releasable.Created("persistent-cache", c) |
| 464 | |
| 465 | if err := c.initialScan(ctx); err != nil { |
| 466 | return nil, errors.Wrapf(err, "error during initial scan of %s", c.description) |
| 467 | } |
| 468 | |
| 469 | return c, nil |
| 470 | } |