66 "os"
77 "regexp"
88 "strings"
9+ "sync"
910 "time"
1011
1112 "github.com/semaphoreci/toolbox/cache-cli/pkg/archive"
@@ -67,6 +68,7 @@ func RunRestore(cmd *cobra.Command, args []string) {
6768}
6869
6970func downloadAndUnpack (storage storage.Storage , archiver archive.Archiver , metricsManager metrics.MetricsManager , keys []string ) {
71+ cachedList := sync .OnceValues (storage .List )
7072 for _ , rawKey := range keys {
7173 key := NormalizeKey (rawKey )
7274 if ok , _ := storage .HasKey (key ); ok {
@@ -75,7 +77,12 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
7577 break
7678 }
7779
78- availableKeys , err := storage .List ()
80+ // If the key has no regex characters, skip the list/regexp match.
81+ if regexp .QuoteMeta (key ) == key {
82+ continue
83+ }
84+
85+ availableKeys , err := cachedList ()
7986 utils .Check (err )
8087
8188 matchingKey := findMatchingKey (availableKeys , key )
@@ -90,13 +97,15 @@ func downloadAndUnpack(storage storage.Storage, archiver archive.Archiver, metri
9097}
9198
9299func findMatchingKey (availableKeys []storage.CacheKey , match string ) string {
100+ pattern , err := regexp .Compile (match )
101+ if err != nil {
102+ return ""
103+ }
93104 for _ , availableKey := range availableKeys {
94- isMatch , _ := regexp .MatchString (match , availableKey .Name )
95- if isMatch {
105+ if pattern .MatchString (availableKey .Name ) {
96106 return availableKey .Name
97107 }
98108 }
99-
100109 return ""
101110}
102111
0 commit comments