dashboard / erock/pico / chore: lru cache for txt lookup #62 rss

accepted · opened on 2025-04-08T15:27:23Z by erock
Help
checkout latest patchset:
ssh pr.pico.sh print pr-62 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print ps-X | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 62
add review to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add --review 62
accept PR:
ssh pr.pico.sh pr accept 62
close PR:
ssh pr.pico.sh pr close 62

Logs

erock created pr with ps-127 on 2025-04-08T15:27:23Z
erock changed status on 2025-04-08T18:28:54Z {"status":"accepted"}

Patchsets

ps-127 by erock on 2025-04-08T15:27:23Z

Patchset ps-127

chore: lru cache for txt lookup

Eric Bower
2025-04-08T15:27:02Z
Back to top

chore: lru cache for txt lookup

pkg/shared/router.go link
+12 -1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
diff --git a/pkg/shared/router.go b/pkg/shared/router.go
index cf032b0..a226175 100644
--- a/pkg/shared/router.go
+++ b/pkg/shared/router.go
@@ -10,6 +10,8 @@ import (
 	"regexp"
 	"strings"
 
+	"github.com/hashicorp/golang-lru/v2/expirable"
+	"github.com/picosh/pico/pkg/cache"
 	"github.com/picosh/pico/pkg/db"
 	"github.com/picosh/pico/pkg/pssh"
 	"github.com/picosh/pico/pkg/shared/storage"
@@ -211,15 +213,24 @@ func GetSubdomain(r *http.Request) string {
 	return r.Context().Value(CtxSubdomainKey{}).(string)
 }
 
+var txtCache = expirable.NewLRU[string, string](2048, nil, cache.CacheTimeout)
+
 func GetCustomDomain(host string, space string) string {
 	txt := fmt.Sprintf("_%s.%s", space, host)
+	record, found := txtCache.Get(txt)
+	if found {
+		return record
+	}
+
 	records, err := net.LookupTXT(txt)
 	if err != nil {
 		return ""
 	}
 
 	for _, v := range records {
-		return strings.TrimSpace(v)
+		rec := strings.TrimSpace(v)
+		txtCache.Add(txt, rec)
+		return rec
 	}
 
 	return ""