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:
2026-11-23 12:49:47 +00:00
parent 99c885cd88
commit d6edd40d79
19 changed files with 1059 additions and 39 deletions
+8
View File
@@ -133,11 +133,15 @@ type MySQLConfig struct {
ParseTime bool `mapstructure:"parse_time" json:"parse_time"`
}
// DefaultIdleTTL is how long a cached item can go unrequested before cleanup removes it.
const DefaultIdleTTL = 7 * 24 * time.Hour
// CacheConfig contains cache management configuration
type CacheConfig struct {
TTLOverrides map[string]time.Duration `mapstructure:"ttl_overrides" json:"ttl_overrides"`
DefaultTTL time.Duration `mapstructure:"default_ttl" json:"default_ttl"`
CleanupInterval time.Duration `mapstructure:"cleanup_interval" json:"cleanup_interval"`
IdleTTL time.Duration `mapstructure:"idle_ttl" json:"idle_ttl"` // 0 disables idle eviction
MaxSizeBytes int64 `mapstructure:"max_size_bytes" json:"max_size_bytes"`
PerProjectQuota int64 `mapstructure:"per_project_quota" json:"per_project_quota"`
}
@@ -353,6 +357,7 @@ func Default() *Config {
Cache: CacheConfig{
DefaultTTL: 7 * 24 * time.Hour,
CleanupInterval: 1 * time.Hour,
IdleTTL: DefaultIdleTTL,
MaxSizeBytes: 500 * 1024 * 1024 * 1024, // 500GB
PerProjectQuota: 50 * 1024 * 1024 * 1024, // 50GB
TTLOverrides: map[string]time.Duration{
@@ -505,6 +510,9 @@ func (c *Config) Validate() error {
if c.Cache.DefaultTTL < 0 {
return fmt.Errorf("cache.default_ttl cannot be negative")
}
if c.Cache.IdleTTL < 0 {
return fmt.Errorf("cache.idle_ttl cannot be negative")
}
if c.Cache.MaxSizeBytes < 0 {
return fmt.Errorf("cache.max_size_bytes cannot be negative")
}
+51
View File
@@ -0,0 +1,51 @@
package config
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func loadCacheYAML(t *testing.T, cacheYAML string) (*Config, error) {
t.Helper()
path := filepath.Join(t.TempDir(), "config.yaml")
require.NoError(t, os.WriteFile(path, []byte(cacheYAML), 0600))
return Load(path)
}
func TestIdleTTLDefaultsToAWeek(t *testing.T) {
require.Equal(t, 168*time.Hour, Default().Cache.IdleTTL)
cfg, err := loadCacheYAML(t, "cache:\n default_ttl: 24h\n")
require.NoError(t, err)
require.Equal(t, 168*time.Hour, cfg.Cache.IdleTTL, "an unset key keeps the default")
}
func TestIdleTTLParse(t *testing.T) {
tests := []struct {
name string
yaml string
want time.Duration
wantErr bool
}{
{name: "duration string", yaml: "cache:\n idle_ttl: 72h\n", want: 72 * time.Hour},
{name: "zero disables", yaml: "cache:\n idle_ttl: 0\n", want: 0},
{name: "quoted zero disables", yaml: "cache:\n idle_ttl: \"0\"\n", want: 0},
{name: "quoted duration", yaml: "cache:\n idle_ttl: \"168h\"\n", want: 168 * time.Hour},
{name: "negative rejected", yaml: "cache:\n idle_ttl: -1h\n", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := loadCacheYAML(t, tt.yaml)
if tt.wantErr {
require.ErrorContains(t, err, "cache.idle_ttl")
return
}
require.NoError(t, err)
require.Equal(t, tt.want, cfg.Cache.IdleTTL)
})
}
}