mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(cache): evict items idle past cache.idle_ttl
Every cached item got a 7-day expiry at store time. A popular tarball left the cache seven days after it was cached, however often clients requested it. Artifacts now get no age-based expiry. Cleanup removes any item whose last_accessed is older than cache.idle_ttl (default 168h, 0 disables). It reads the items in bounded batches of 500 through the indexed last_accessed column and deletes them through deletePackage. Metadata entries keep the freshness expiry from default_ttl. Startup and each cleanup run clear the expiry that earlier versions set on artifact rows. Cleanup records the idle and ttl eviction reasons and logs the count and bytes freed. The stats API reports idle_ttl in seconds. Package deletes are soft deletes and no history table has a foreign key to packages, so download history stays intact.
This commit is contained in:
@@ -233,6 +233,7 @@ func (a *App) initializeComponents() error {
|
||||
a.cache, err = cache.New(a.storage, a.metadata, a.scanManager, a.analyticsEngine, cache.Config{
|
||||
DefaultTTL: a.config.Cache.DefaultTTL,
|
||||
CleanupInterval: cleanupInterval,
|
||||
IdleTTL: a.config.Cache.IdleTTL,
|
||||
MaxPackageSize: a.config.Security.Scanners.Static.MaxPackageSize,
|
||||
FailClosed: a.config.Security.FailClosed,
|
||||
})
|
||||
|
||||
@@ -407,6 +407,7 @@ func (a *App) handleStats(c fiber.Ctx) error {
|
||||
"total_downloads": cacheStats.TotalDownloads,
|
||||
"total_size": cacheStats.TotalSize,
|
||||
"max_cache_size": a.config.Cache.MaxSizeBytes,
|
||||
"idle_ttl": int64(a.config.Cache.IdleTTL / time.Second), // seconds, 0 = idle eviction off
|
||||
"cache_hits": cacheStats.TotalDownloads,
|
||||
"cache_misses": 0, // TODO: Track cache misses
|
||||
"cache_evictions": 0, // TODO: Track evictions
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
||||
)
|
||||
|
||||
func TestStatsReportsIdleTTLSeconds(t *testing.T) {
|
||||
for name, tt := range map[string]struct {
|
||||
idle time.Duration
|
||||
want float64
|
||||
}{
|
||||
"a week": {idle: 168 * time.Hour, want: 604800},
|
||||
"disabled": {idle: 0, want: 0},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
a := newTestApp(t, func(c *config.Config) { c.Cache.IdleTTL = tt.idle })
|
||||
|
||||
code, _, body := doRequest(t, a, "/api/stats", nil)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("status %d, body %q", code, body)
|
||||
}
|
||||
var resp struct {
|
||||
Stats map[string]any `json:"stats"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &resp); err != nil {
|
||||
t.Fatalf("decode %q: %v", body, err)
|
||||
}
|
||||
got, ok := resp.Stats["idle_ttl"].(float64)
|
||||
if !ok || got != tt.want {
|
||||
t.Errorf("stats.idle_ttl = %v, want %v (body %s)", resp.Stats["idle_ttl"], tt.want, body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user