Skip to content

Commit acf6290

Browse files
committed
chore(pgs): configurable max items for lru
1 parent 50987d0 commit acf6290

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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=

pkg/apps/pgs/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

pkg/apps/pgs/web.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)