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:
@@ -0,0 +1,52 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestListIdlePackages(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _ := newStore(t)
|
||||
now := time.Now()
|
||||
for i, p := range []struct {
|
||||
name string
|
||||
age time.Duration
|
||||
}{{"old-a", 10 * 24 * time.Hour}, {"old-b", 9 * 24 * time.Hour}, {"fresh", time.Hour}} {
|
||||
require.NoError(t, s.SavePackage(ctx, &metadata.Package{
|
||||
Registry: "npm", Name: p.name, Version: "1", LastAccessed: now.Add(-p.age), Size: int64(i),
|
||||
}))
|
||||
}
|
||||
require.NoError(t, s.SaveScanResult(ctx, &metadata.ScanResult{Registry: "npm", PackageName: "old-a", PackageVersion: "1"}))
|
||||
|
||||
got, err := s.ListIdlePackages(ctx, now.Add(-7*24*time.Hour), 1)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 1)
|
||||
require.Equal(t, "old-a", got[0].Name, "oldest first, scan results are not packages")
|
||||
|
||||
got, err = s.ListIdlePackages(ctx, now.Add(-7*24*time.Hour), 10)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 2)
|
||||
}
|
||||
|
||||
func TestListExpiredPackagesAndClearArtifactExpiry(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, _ := newStore(t)
|
||||
past := time.Now().Add(-time.Hour)
|
||||
for _, v := range []string{"list", "1.0.0"} {
|
||||
require.NoError(t, s.SavePackage(ctx, &metadata.Package{Registry: "npm", Name: "x", Version: v, ExpiresAt: &past}))
|
||||
}
|
||||
|
||||
cleared, err := s.ClearArtifactExpiry(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(1), cleared)
|
||||
|
||||
got, err := s.ListExpiredPackages(ctx, time.Now(), 10)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, got, 1)
|
||||
require.Equal(t, "list", got[0].Version)
|
||||
}
|
||||
Reference in New Issue
Block a user