NewStorageOrNil returns cache.Storage backed by the provided directory.
(ctx context.Context, cacheDir string, maxBytes int64, subdir string)
| 38 | |
| 39 | // NewStorageOrNil returns cache.Storage backed by the provided directory. |
| 40 | func NewStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdir string) (Storage, error) { |
| 41 | if maxBytes <= 0 || cacheDir == "" { |
| 42 | return nil, nil |
| 43 | } |
| 44 | |
| 45 | if !ospath.IsAbs(cacheDir) { |
| 46 | return nil, errors.Errorf("cache dir %q was not absolute", cacheDir) |
| 47 | } |
| 48 | |
| 49 | contentCacheDir := filepath.Join(cacheDir, subdir) |
| 50 | |
| 51 | if _, err := os.Stat(contentCacheDir); os.IsNotExist(err) { |
| 52 | if mkdirerr := mkdirAll(contentCacheDir, DirMode); mkdirerr != nil { |
| 53 | return nil, errors.Wrap(mkdirerr, "error creating cache directory") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fs, err := filesystem.New(context.WithoutCancel(ctx), &filesystem.Options{ |
| 58 | Path: contentCacheDir, |
| 59 | Options: sharded.Options{ |
| 60 | DirectoryShards: []int{2}, |
| 61 | }, |
| 62 | }, false) |
| 63 | |
| 64 | return filesystemImplWrapper{fs.(Storage)}, errors.Wrap(err, "error initializing filesystem cache") //nolint:forcetypeassert |
| 65 | } |