Skip to content

Commit 1f9e725

Browse files
authoredSep 2, 2024··
pkg/cache: configurable backend preference (#1424)
Signed-off-by: Joe Lanford <[email protected]>
1 parent 32a7b63 commit 1f9e725

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed
 

‎pkg/cache/cache.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ type backend interface {
5151
}
5252

5353
type CacheOptions struct {
54-
Log *logrus.Entry
54+
Log *logrus.Entry
55+
Format string
5556
}
5657

5758
func WithLog(log *logrus.Entry) CacheOption {
@@ -60,6 +61,12 @@ func WithLog(log *logrus.Entry) CacheOption {
6061
}
6162
}
6263

64+
func WithFormat(format string) CacheOption {
65+
return func(o *CacheOptions) {
66+
o.Format = format
67+
}
68+
}
69+
6370
type CacheOption func(*CacheOptions)
6471

6572
// New creates a new Cache. It chooses a cache implementation based
@@ -73,7 +80,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
7380
for _, opt := range cacheOpts {
7481
opt(opts)
7582
}
76-
cacheBackend, err := getDefaultBackend(cacheDir, opts.Log)
83+
cacheBackend, err := getBackend(cacheDir, opts.Format, opts.Log)
7784
if err != nil {
7885
return nil, err
7986
}
@@ -84,7 +91,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
8491
return &cache{backend: cacheBackend, log: opts.Log}, nil
8592
}
8693

87-
func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) {
94+
func getBackend(cacheDir string, backendName string, log *logrus.Entry) (backend, error) {
8895
entries, err := os.ReadDir(cacheDir)
8996
if err != nil && !errors.Is(err, os.ErrNotExist) {
9097
return nil, fmt.Errorf("detect cache format: read cache directory: %v", err)
@@ -96,12 +103,21 @@ func getDefaultBackend(cacheDir string, log *logrus.Entry) (backend, error) {
96103
}
97104

98105
if len(entries) == 0 {
99-
log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend")
100-
return backends[0], nil
106+
if backendName == "" {
107+
log.WithField("backend", backends[0].Name()).Info("cache directory is empty, using preferred backend")
108+
return backends[0], nil
109+
}
110+
for _, b := range backends {
111+
if b.Name() == backendName {
112+
log.WithField("backend", backendName).Info("using preferred backend")
113+
return b, nil
114+
}
115+
}
116+
return nil, fmt.Errorf("preferred backend %q not found", backendName)
101117
}
102118

103119
for _, backend := range backends {
104-
if backend.IsCachePresent() {
120+
if (backendName == "" || backend.Name() == backendName) && backend.IsCachePresent() {
105121
log.WithField("backend", backend.Name()).Info("found existing cache contents")
106122
return backend, nil
107123
}

‎pkg/cache/cache_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ func TestCache_ListPackages(t *testing.T) {
222222
func genTestCaches(t *testing.T, fbcFS fs.FS) map[string]Cache {
223223
t.Helper()
224224

225-
caches := map[string]Cache{
226-
"json": &cache{backend: newJSONBackend(t.TempDir()), log: log.Null()},
227-
"pogreb.v1": &cache{backend: newPogrebV1Backend(t.TempDir()), log: log.Null()},
225+
caches := make(map[string]Cache)
226+
for _, format := range []string{FormatJSON, FormatPogrebV1} {
227+
c, err := New(t.TempDir(), WithFormat(format), WithLog(log.Null()))
228+
require.NoError(t, err)
229+
caches[format] = c
228230
}
229231

230232
for _, c := range caches {

‎pkg/cache/json.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ type jsonBackend struct {
4040
bundles bundleKeys
4141
}
4242

43+
const FormatJSON = "json"
44+
4345
func (q *jsonBackend) Name() string {
44-
return "json"
46+
return FormatJSON
4547
}
4648

4749
func (q *jsonBackend) IsCachePresent() bool {

‎pkg/cache/pogrebv1.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ func newPogrebV1Backend(baseDir string) *pogrebV1Backend {
3131
}
3232

3333
const (
34+
FormatPogrebV1 = "pogreb.v1"
35+
3436
pogrebV1CacheModeDir = 0770
3537
pogrebV1CacheModeFile = 0660
3638

37-
pograbV1CacheDir = "pogreb.v1"
39+
pograbV1CacheDir = FormatPogrebV1
3840
pogrebDigestFile = pograbV1CacheDir + "/digest"
3941
pogrebDbDir = pograbV1CacheDir + "/db"
4042
)
@@ -46,7 +48,7 @@ type pogrebV1Backend struct {
4648
}
4749

4850
func (q *pogrebV1Backend) Name() string {
49-
return pograbV1CacheDir
51+
return FormatPogrebV1
5052
}
5153

5254
func (q *pogrebV1Backend) IsCachePresent() bool {

0 commit comments

Comments
 (0)
Please sign in to comment.