Phase 3: cache

Repeat downloads are instant, and the cache never bypasses a limit.

- internal/cache: canonical key (extractor + video id + profile, URL hash
  fallback), per-bot file_id layer, stored copy layer, metadata layer,
  TTL by source type, LRU-plus-hits eviction that keeps file_ids, orphan
  sweeper, takedown that deletes, invalidates and blocks
- One admission gate: a cache hit passes the same size, duration, quota,
  volume, active job and queue checks as a fresh download, is judged by
  the real cached file, and costs the same. It only skips the worker
- Singleflight: a second requester joins the job that is queued or running
  for the same result. Each keeps their own caps, checked at delivery
- Derive: lower profiles and audio are transcoded from a stored copy with
  ffmpeg; a higher profile that already fits is served as an alias; a
  failed derive falls back to a download
- Self-healing: a rejected file_id falls back to the stored copy, then to
  a fresh download, and the entry is updated either way
- Private or login-gated content and live streams are never cached
- Playlists as per-item jobs started in turn, /subs, /history with resend,
  /fresh, owner /purge and /cache
- Worker removes its temp files right after the upload

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 08:38:38 +00:00
co-authored by Claude Fable 5.2
parent 0d50f453a0
commit 9c9d641eda
39 changed files with 4121 additions and 121 deletions
+48 -2
View File
@@ -44,6 +44,7 @@ type Config struct {
Queue Queue `toml:"queue"`
Limits Limits `toml:"limits"`
LNbits LNbits `toml:"lnbits"`
Cache Cache `toml:"cache"`
Prices Prices `toml:"prices"`
Rate Rate `toml:"rate"`
Log Log `toml:"log"`
@@ -131,6 +132,32 @@ type Tier struct {
// DailyMB is the volume per UTC day in MB, 0 for no cap. It applies to
// paid users too, as a guard against abuse.
DailyMB int64 `toml:"daily_mb"`
// PlaylistItems is how many items of one playlist link are fetched.
PlaylistItems int `toml:"playlist_items"`
}
// Cache holds the smart cache settings.
type Cache struct {
Enabled bool `toml:"enabled" env:"CACHE_ENABLED"`
// BudgetMB caps the total size of stored copies. 0 means no cap.
BudgetMB int64 `toml:"budget_mb" env:"CACHE_BUDGET_MB"`
// TTLs by source class: how long a result is served before it is
// fetched again.
TTL Duration `toml:"ttl" env:"CACHE_TTL"`
TTLStable Duration `toml:"ttl_stable" env:"CACHE_TTL_STABLE"`
TTLEphemeral Duration `toml:"ttl_ephemeral" env:"CACHE_TTL_EPHEMERAL"`
// StableSites and EphemeralSites are yt-dlp extractor name prefixes.
StableSites []string `toml:"stable_sites" env:"CACHE_STABLE_SITES"`
EphemeralSites []string `toml:"ephemeral_sites" env:"CACHE_EPHEMERAL_SITES"`
// MetaTTL is how long link metadata is remembered, so cards for known
// links appear at once.
MetaTTL Duration `toml:"meta_ttl" env:"CACHE_META_TTL"`
// Derive makes lower profiles and audio from a stored higher profile
// with ffmpeg instead of downloading again.
Derive bool `toml:"derive" env:"CACHE_DERIVE"`
// SweepInterval is how often expiry, the size budget and orphan files
// are checked.
SweepInterval Duration `toml:"sweep_interval" env:"CACHE_SWEEP_INTERVAL"`
}
// Limits holds the per-tier limits. Free applies to everyone without a
@@ -227,9 +254,21 @@ func Default() Config {
},
Limits: Limits{
Free: Tier{MaxSizeMB: 50, MaxDuration: Duration(10 * time.Minute), ActiveJobs: 1,
DailyDownloads: 5, DailyMB: 250},
DailyDownloads: 5, DailyMB: 250, PlaylistItems: 3},
Paid: Tier{MaxSizeMB: 2000, MaxDuration: Duration(3 * time.Hour), ActiveJobs: 3,
DailyDownloads: 100, DailyMB: 20000},
DailyDownloads: 100, DailyMB: 20000, PlaylistItems: 25},
},
Cache: Cache{
Enabled: true,
BudgetMB: 20000,
TTL: Duration(7 * 24 * time.Hour),
TTLStable: Duration(30 * 24 * time.Hour),
TTLEphemeral: Duration(6 * time.Hour),
StableSites: []string{"youtube", "vimeo", "archive.org", "soundcloud", "dailymotion", "peertube", "bandcamp"},
EphemeralSites: []string{"twitter", "instagram", "tiktok", "facebook", "reddit", "twitch", "generic"},
MetaTTL: Duration(30 * time.Minute),
Derive: true,
SweepInterval: Duration(10 * time.Minute),
},
LNbits: LNbits{
PollInterval: Duration(5 * time.Second),
@@ -387,6 +426,13 @@ func applyTierEnv(prefix string, t *Tier, lookup func(string) (string, bool)) er
}
t.DailyDownloads = n
}
if v, ok := lookup(prefix + "_PLAYLIST_ITEMS"); ok && v != "" {
n, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("config: %s_PLAYLIST_ITEMS: %w", prefix, err)
}
t.PlaylistItems = n
}
if v, ok := lookup(prefix + "_DAILY_MB"); ok && v != "" {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {