Skip to content

Commit a774be2

Browse files
committed
Warm up monthly samples caches on startup
1 parent d4ab018 commit a774be2

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

internal/database/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func (c *MonthlySamplesCache) load(ctx context.Context) (map[int]int, error) {
8585
return cache, nil
8686
}
8787

88+
func (c *MonthlySamplesCache) Warmup(ctx context.Context) error {
89+
_, err := c.load(ctx)
90+
return err
91+
}
92+
8893
func startOfNextMonth() time.Time {
8994
now := time.Now()
9095
return time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, now.Location())

internal/packages/repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func NewSQLiteRepository(db *sql.DB) *SQLiteRepository {
3232
}
3333
}
3434

35+
func (r *SQLiteRepository) WarmupCache(ctx context.Context) error {
36+
return r.monthlyMaxCache.Warmup(ctx)
37+
}
38+
3539
// monthRange returns the SQL WHERE fragment and bound args for a month range.
3640
// When startMonth is 0, no lower bound is applied (all history up to endMonth).
3741
func monthRange(startMonth, endMonth int) (clause string, args []any) {

internal/popularity/repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func (r *Repository[T, L]) FindSeries(ctx context.Context, identifier string, st
206206
return &list, nil
207207
}
208208

209+
func (r *Repository[T, L]) WarmupCache(ctx context.Context) error {
210+
return r.samplesCache.Warmup(ctx)
211+
}
212+
209213
func (r *Repository[T, L]) queryPattern(query string) string {
210214
if r.cfg.QueryContains {
211215
return "%" + query + "%"

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"log/slog"
56
"net/http"
67
"os"
@@ -85,6 +86,16 @@ func run(cfg config.Config) error {
8586
return err
8687
}
8788

89+
// Warm up caches
90+
ctx := context.Background()
91+
for _, repo := range []interface{ WarmupCache(context.Context) error }{
92+
packagesRepo, countriesRepo, mirrorsRepo, systemArchRepo, osRepo,
93+
} {
94+
if err := repo.WarmupCache(ctx); err != nil {
95+
slog.Warn("failed to warm up cache", "error", err)
96+
}
97+
}
98+
8899
// Setup HTTP routes
89100
mux := http.NewServeMux()
90101

0 commit comments

Comments
 (0)