dashboard / erock/pico / feat(pgs): lru cache for object info and special files #59 rss

accepted · opened on 2025-04-06T03:01:44Z by erock
Help
checkout latest patchset:
ssh pr.pico.sh print pr-59 | 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 59
add review to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add --review 59
accept PR:
ssh pr.pico.sh pr accept 59
close PR:
ssh pr.pico.sh pr close 59

Logs

erock created pr with ps-119 on 2025-04-06T03:01:44Z
erock added ps-120 on 2025-04-06T03:03:49Z
erock added ps-122 on 2025-04-06T19:08:31Z
erock added ps-123 on 2025-04-06T19:41:38Z
erock changed status on 2025-04-06T22:13:51Z {"status":"accepted"}

Patchsets

ps-119 by erock on 2025-04-06T03:01:44Z
Range Diff ↕ rd-120
2: caace51 ! 1: 26daea4 feat(pgs): lru cache for object info and special files
1: 2cf56f0 ! 2: b004b64 chore(pgs): use http cache clear event to rm lru cache for special files
ps-120 by erock on 2025-04-06T03:03:49Z
Range Diff ↕ rd-122
1: 26daea4 = 1: 26daea4 feat(pgs): lru cache for object info and special files
2: b004b64 = 2: b004b64 chore(pgs): use http cache clear event to rm lru cache for special files
-: ------- > 3: 59f5618 refactor(pgs): store lru cache on web router
ps-122 by erock on 2025-04-06T19:08:31Z
Range Diff ↕ rd-123
1: 26daea4 = 1: 26daea4 feat(pgs): lru cache for object info and special files
2: b004b64 = 2: b004b64 chore(pgs): use http cache clear event to rm lru cache for special files
3: 59f5618 = 3: 59f5618 refactor(pgs): store lru cache on web router
-: ------- > 4: ee12290 refactor(pgs): update minio lru and remove object info cache
ps-123 by erock on 2025-04-06T19:41:38Z

Range-diff rd-122

title
feat(pgs): lru cache for object info and special files
description
Patch equal
old #1
26daea4
new #1
26daea4
title
chore(pgs): use http cache clear event to rm lru cache for special files
description
Patch equal
old #2
b004b64
new #2
b004b64
title
refactor(pgs): store lru cache on web router
description
Patch added
old #0
(none)
new #3
59f5618
Back to top
1: 26daea4 = 1: 26daea4 feat(pgs): lru cache for object info and special files
2: b004b64 = 2: b004b64 chore(pgs): use http cache clear event to rm lru cache for special files
-: ------- > 3: 59f5618 refactor(pgs): store lru cache on web router

old


                    

new

old:pkg/apps/pgs/web.go new:pkg/apps/pgs/web.go
 	"github.com/darkweak/souin/plugins/souin/storages"
 	"github.com/darkweak/storages/core"
 	"github.com/gorilla/feeds"
+	"github.com/hashicorp/golang-lru/v2/expirable"
+	"github.com/picosh/pico/pkg/cache"
 	"github.com/picosh/pico/pkg/db"
 	sst "github.com/picosh/pico/pkg/pobj/storage"
 	"github.com/picosh/pico/pkg/shared"
 type HasPerm = func(proj *db.Project) bool
 
 type WebRouter struct {
-	Cfg        *PgsConfig
-	RootRouter *http.ServeMux
-	UserRouter *http.ServeMux
+	Cfg            *PgsConfig
+	RootRouter     *http.ServeMux
+	UserRouter     *http.ServeMux
+	RedirectsCache *expirable.LRU[string, []*RedirectRule]
+	HeadersCache   *expirable.LRU[string, []*HeaderRule]
 }
 
 func NewWebRouter(cfg *PgsConfig) *WebRouter {
 	router := &WebRouter{
-		Cfg: cfg,
+		Cfg:            cfg,
+		RedirectsCache: expirable.NewLRU[string, []*RedirectRule](2048, nil, cache.CacheTimeout),
+		HeadersCache:   expirable.NewLRU[string, []*HeaderRule](2048, nil, cache.CacheTimeout),
 	}
 	router.initRouters()
 	return router
 	go func() {
 		for key := range web.Cfg.CacheClearingQueue {
 			rKey := filepath.Join(key, "_redirects")
-			redirectsCache.Remove(rKey)
+			web.RedirectsCache.Remove(rKey)
 			hKey := filepath.Join(key, "_headers")
-			headersCache.Remove(hKey)
+			web.HeadersCache.Remove(hKey)
 		}
 	}()
 

old


                    

new

old:pkg/apps/pgs/web_asset_handler.go new:pkg/apps/pgs/web_asset_handler.go
 	"net/http/httputil"
 	_ "net/http/pprof"
 
-	"github.com/hashicorp/golang-lru/v2/expirable"
-	"github.com/picosh/pico/pkg/cache"
 	sst "github.com/picosh/pico/pkg/pobj/storage"
 	"github.com/picosh/pico/pkg/shared/storage"
 )
 
-var (
-	redirectsCache = expirable.NewLRU[string, []*RedirectRule](2048, nil, cache.CacheTimeout)
-	headersCache   = expirable.NewLRU[string, []*HeaderRule](2048, nil, cache.CacheTimeout)
-)
-
 type ApiAssetHandler struct {
 	*WebRouter
 	Logger *slog.Logger
 	var redirects []*RedirectRule
 
 	redirectsCacheKey := filepath.Join(getSurrogateKey(h.UserID, h.ProjectDir), "_redirects")
-	if cachedRedirects, found := redirectsCache.Get(redirectsCacheKey); found {
+	if cachedRedirects, found := h.RedirectsCache.Get(redirectsCacheKey); found {
+		fmt.Println(cachedRedirects)
 		redirects = cachedRedirects
 	} else {
 		redirectFp, redirectInfo, err := h.Cfg.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_redirects"))
 			}
 		}
 
-		redirectsCache.Add(redirectsCacheKey, redirects)
+		h.RedirectsCache.Add(redirectsCacheKey, redirects)
 	}
 
 	routes := calcRoutes(h.ProjectDir, h.Filepath, redirects)
 	var headers []*HeaderRule
 
 	headersCacheKey := filepath.Join(getSurrogateKey(h.UserID, h.ProjectDir), "_headers")
-	if cachedHeaders, found := headersCache.Get(headersCacheKey); found {
+	if cachedHeaders, found := h.HeadersCache.Get(headersCacheKey); found {
 		headers = cachedHeaders
 	} else {
 		headersFp, headersInfo, err := h.Cfg.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_headers"))
 			}
 		}
 
-		headersCache.Add(headersCacheKey, headers)
+		h.HeadersCache.Add(headersCacheKey, headers)
 	}
 
 	userHeaders := []*HeaderLine{}

old


                    

new

old:pkg/apps/pgs/web_test.go new:pkg/apps/pgs/web_test.go
 
 			ct := responseRecorder.Header().Get("content-type")
 			if ct != tc.contentType {
-				t.Errorf("Want status '%s', got '%s'", tc.contentType, ct)
+				t.Errorf("Want content type '%s', got '%s'", tc.contentType, ct)
 			}
 
 			body := strings.TrimSpace(responseRecorder.Body.String())