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
+38 -1
View File
@@ -14,6 +14,31 @@ import (
// Callers should treat it as a non-fatal capability check.
var ErrNotImplemented = errors.New("metadata: operation not implemented by this backend")
// DefaultBatchSize bounds the rows a list query returns when the caller passes no limit.
const DefaultBatchSize = 500
// MetadataVersions are the cache-key versions of metadata pages (index, list, latest).
var MetadataVersions = []string{"list", "page", "latest", "metadata"}
// MetadataNameSuffixes mark Go module metadata files (.mod, .info).
var MetadataNameSuffixes = []string{".mod", ".info"}
// IsMetadataEntry reports whether a cache entry is a metadata page that must
// refresh, as opposed to an immutable artifact.
func IsMetadataEntry(name, version string) bool {
for _, v := range MetadataVersions {
if version == v {
return true
}
}
for _, suffix := range MetadataNameSuffixes {
if strings.HasSuffix(name, suffix) {
return true
}
}
return false
}
// Store is an alias for MetadataStore for convenience
type Store = MetadataStore
@@ -31,9 +56,21 @@ type MetadataStore interface {
// ListPackages lists packages with optional filtering
ListPackages(ctx context.Context, opts *ListOptions) ([]*Package, error)
// UpdateDownloadCount increments download counter
// UpdateDownloadCount increments download counter and stamps last_accessed
UpdateDownloadCount(ctx context.Context, registry, name, version string) error
// ListIdlePackages returns up to limit packages last accessed before
// idleBefore, oldest first. A limit <= 0 means DefaultBatchSize.
ListIdlePackages(ctx context.Context, idleBefore time.Time, limit int) ([]*Package, error)
// ListExpiredPackages returns up to limit packages whose ExpiresAt is before
// the given time, oldest first. A limit <= 0 means DefaultBatchSize.
ListExpiredPackages(ctx context.Context, expiredBefore time.Time, limit int) ([]*Package, error)
// ClearArtifactExpiry removes ExpiresAt from every non-metadata entry and
// returns the number of rows changed. It is idempotent.
ClearArtifactExpiry(ctx context.Context) (int64, error)
// GetStats returns statistics
GetStats(ctx context.Context, registry string) (*Stats, error)