From a3c60bcff3a8175e8bd1ebd5ba7c0e36644a0c5a Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Mon, 15 Jan 2024 11:03:39 +0200 Subject: [PATCH 1/2] Match cache restore-keys in creation reverse order --- pkg/artifactcache/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/artifactcache/handler.go b/pkg/artifactcache/handler.go index b6600b6b85a..2faf881db68 100644 --- a/pkg/artifactcache/handler.go +++ b/pkg/artifactcache/handler.go @@ -386,7 +386,7 @@ func (h *Handler) findCache(db *bolthold.Store, keys []string, version string) ( for _, prefix := range keys[1:] { found := false - if err := db.ForEach(bolthold.Where("Key").Ge(prefix).And("Version").Eq(version).SortBy("Key"), func(v *Cache) error { + if err := db.ForEach(bolthold.Where("Key").Ge(prefix).And("Version").Eq(version).SortBy("CreatedAt").Reverse(), func(v *Cache) error { if !strings.HasPrefix(v.Key, prefix) { return stop } From 6f084c70ad67c212b0abe1efba35e6e46f009c2c Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Thu, 18 Jan 2024 14:36:34 +0200 Subject: [PATCH 2/2] Match full prefix when selecting cache --- pkg/artifactcache/handler.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/artifactcache/handler.go b/pkg/artifactcache/handler.go index 2faf881db68..3178260b12d 100644 --- a/pkg/artifactcache/handler.go +++ b/pkg/artifactcache/handler.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "strconv" "strings" "sync/atomic" @@ -386,7 +387,12 @@ func (h *Handler) findCache(db *bolthold.Store, keys []string, version string) ( for _, prefix := range keys[1:] { found := false - if err := db.ForEach(bolthold.Where("Key").Ge(prefix).And("Version").Eq(version).SortBy("CreatedAt").Reverse(), func(v *Cache) error { + prefixPattern := fmt.Sprintf("^%s", regexp.QuoteMeta(prefix)) + re, err := regexp.Compile(prefixPattern) + if err != nil { + continue + } + if err := db.ForEach(bolthold.Where("Key").RegExp(re).And("Version").Eq(version).SortBy("CreatedAt").Reverse(), func(v *Cache) error { if !strings.HasPrefix(v.Key, prefix) { return stop }