@@ -51,7 +51,8 @@ type backend interface {
51
51
}
52
52
53
53
type CacheOptions struct {
54
- Log * logrus.Entry
54
+ Log * logrus.Entry
55
+ BackendName string
55
56
}
56
57
57
58
func WithLog (log * logrus.Entry ) CacheOption {
@@ -60,6 +61,12 @@ func WithLog(log *logrus.Entry) CacheOption {
60
61
}
61
62
}
62
63
64
+ func WithBackendName (name string ) CacheOption {
65
+ return func (o * CacheOptions ) {
66
+ o .BackendName = name
67
+ }
68
+ }
69
+
63
70
type CacheOption func (* CacheOptions )
64
71
65
72
// New creates a new Cache. It chooses a cache implementation based
@@ -73,7 +80,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
73
80
for _ , opt := range cacheOpts {
74
81
opt (opts )
75
82
}
76
- cacheBackend , err := getDefaultBackend (cacheDir , opts .Log )
83
+ cacheBackend , err := getBackend (cacheDir , opts . BackendName , opts .Log )
77
84
if err != nil {
78
85
return nil , err
79
86
}
@@ -84,7 +91,7 @@ func New(cacheDir string, cacheOpts ...CacheOption) (Cache, error) {
84
91
return & cache {backend : cacheBackend , log : opts .Log }, nil
85
92
}
86
93
87
- func getDefaultBackend (cacheDir string , log * logrus.Entry ) (backend , error ) {
94
+ func getBackend (cacheDir string , backendName string , log * logrus.Entry ) (backend , error ) {
88
95
entries , err := os .ReadDir (cacheDir )
89
96
if err != nil && ! errors .Is (err , os .ErrNotExist ) {
90
97
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) {
96
103
}
97
104
98
105
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 )
101
117
}
102
118
103
119
for _ , backend := range backends {
104
- if backend .IsCachePresent () {
120
+ if ( backendName == "" || backend . Name () == backendName ) && backend .IsCachePresent () {
105
121
log .WithField ("backend" , backend .Name ()).Info ("found existing cache contents" )
106
122
return backend , nil
107
123
}
0 commit comments