Commit acf6290

Eric Bower  ·  2026-04-22 09:15:16 -0400 EDT
parent 50987d0
chore(pgs): configurable max items for lru
3 files changed,  +16, -2
+1, -0
......@@ -102,6 +102,7 @@ PGS_PROTOCOL=http
102102 PGS_STORAGE_DIR=.storage
103103 PGS_DEBUG=1
104104 PGS_CACHE_TTL=600s
105+PGS_CACHE_MAX_ITEMS=0
105106
106107 PICO_CADDYFILE=./caddy/Caddyfile.pico
107108 PICO_V4=
+8, -0
......@@ -4,6 +4,7 @@ import (
44 "fmt"
55 "log/slog"
66 "path/filepath"
7+ "strconv"
78 "time"
89
910 pgsdb "github.com/picosh/pico/pkg/apps/pgs/db"
......@@ -13,6 +14,7 @@ import (
1314
1415 type PgsConfig struct {
1516 CacheTTL time.Duration
17+ CacheMaxItems int
1618 Domain string
1719 MaxAssetSize int64
1820 MaxSize uint64
......@@ -74,12 +76,18 @@ func NewPgsConfig(logger *slog.Logger, dbpool pgsdb.PgsDB, st storage.StorageSer
7476 if err != nil {
7577 cacheTTL = 600 * time.Second
7678 }
79+ cacheMaxItemsStr := shared.GetEnv("PGS_CACHE_MAX_ITEMS", "")
80+ cacheMaxItems := 0
81+ if cacheMaxItemsStr != "" {
82+ cacheMaxItems, _ = strconv.Atoi(cacheMaxItemsStr)
83+ }
7784
7885 sshHost := shared.GetEnv("PGS_SSH_HOST", "0.0.0.0")
7986 sshPort := shared.GetEnv("PGS_SSH_PORT", "2222")
8087
8188 cfg := PgsConfig{
8289 CacheTTL: cacheTTL,
90+ CacheMaxItems: cacheMaxItems,
8391 Domain: domain,
8492 MaxAssetSize: maxAssetSize,
8593 MaxSize: maxSize,
+7, -2
......@@ -112,7 +112,7 @@ func (p *PromCacheMetrics) AddUpstreamRequest() {
112112 func NewPgsHttpCache(cfg *PgsConfig, upstream http.Handler) *httpcache.HttpCache {
113113 ttl := cfg.CacheTTL
114114 metrics := NewPromCacheMetrics(cfg.Logger, prometheus.DefaultRegisterer)
115- cache := expirable.NewLRU(0, metrics.EvictCacheItem, ttl)
115+ cache := expirable.NewLRU(cfg.CacheMaxItems, metrics.EvictCacheItem, ttl)
116116 httpCache := &httpcache.HttpCache{
117117 Ttl: ttl,
118118 Logger: cfg.Logger,
......@@ -124,7 +124,12 @@ func NewPgsHttpCache(cfg *PgsConfig, upstream http.Handler) *httpcache.HttpCache
124124 },
125125 CacheMetrics: metrics,
126126 }
127- httpCache.Logger.Info("httpcache initiated", "ttl", httpCache.Ttl, "storage", "lru")
127+ httpCache.Logger.Info(
128+ "httpcache initiated",
129+ "storageType", "expirable.LRU",
130+ "ttl", ttl,
131+ "maxItems", cfg.CacheMaxItems,
132+ )
128133 return httpCache
129134 }
130135