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
+26 -1
View File
@@ -17,8 +17,10 @@ import (
"github.com/noderunners/dlbot/internal/billing"
"github.com/noderunners/dlbot/internal/billing/lnbits"
"github.com/noderunners/dlbot/internal/bot"
"github.com/noderunners/dlbot/internal/cache"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/ffmpeg"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/sched"
"github.com/noderunners/dlbot/internal/service"
@@ -35,6 +37,8 @@ type Options struct {
Log *slog.Logger
// Runner replaces the real yt-dlp, for tests.
Runner ytdlp.Runner
// Transcoder replaces the real ffmpeg, for tests.
Transcoder ffmpeg.Transcoder
// Tune is called with the bot before it starts, for tests.
Tune func(*bot.Bot)
}
@@ -48,6 +52,7 @@ type App struct {
Storage storage.Storage
Sched *sched.Scheduler
Billing *billing.Billing
Cache *cache.Cache
Service *service.Service
Bot *bot.Bot
HTTP *api.Server
@@ -171,11 +176,24 @@ func New(ctx context.Context, opts Options) (*App, error) {
ln = lnbits.New(cfg.LNbits.URL, cfg.LNbits.InvoiceKey)
}
a.Billing = billing.New(a.Store, ln, cfg, log)
a.Service = service.New(cfg, a.Store, a.Sched, a.Billing, a.runner, log)
if a.tgBot, err = tg.NewBot(cfg.Telegram.Token, cfg.Telegram.APIURL); err != nil {
return fail(fmt.Errorf("app: telegram client: %w", err))
}
// file_ids only work for the bot that got them, so the cache is keyed
// by the bot's ID, which is the number in front of the token.
a.Cache = cache.New(cache.Config{
Enabled: cfg.Cache.Enabled,
BudgetBytes: cfg.Cache.BudgetMB << 20,
TTLDefault: cfg.Cache.TTL.D(),
TTLStable: cfg.Cache.TTLStable.D(),
TTLEphemeral: cfg.Cache.TTLEphemeral.D(),
Stable: cfg.Cache.StableSites,
Ephemeral: cfg.Cache.EphemeralSites,
MetaTTL: cfg.Cache.MetaTTL.D(),
Derive: cfg.Cache.Derive,
}, a.tgBot.ID(), a.Store, a.Storage, log)
a.Service = service.New(cfg, a.Store, a.Sched, a.Billing, a.Cache, a.runner, log)
a.Bot, err = bot.New(ctx, bot.Deps{
API: a.tgBot,
Config: cfg,
@@ -183,6 +201,7 @@ func New(ctx context.Context, opts Options) (*App, error) {
Sched: a.Sched,
Service: a.Service,
Billing: a.Billing,
Cache: a.Cache,
Storage: a.Storage,
I18n: tr,
Log: log,
@@ -208,6 +227,11 @@ func New(ctx context.Context, opts Options) (*App, error) {
Heartbeat: cfg.Queue.Heartbeat.D(),
ClaimWait: 5 * time.Second,
}, &sched.Local{Sched: a.Sched, Storage: a.Storage}, a.runner, log)
trans := opts.Transcoder
if trans == nil {
trans = &ffmpeg.Exec{Path: cfg.Worker.FfmpegPath, Timeout: cfg.Worker.DownloadTimeout.D()}
}
a.worker.SetTranscoder(trans)
}
return a, nil
}
@@ -257,6 +281,7 @@ func (a *App) Run(ctx context.Context) error {
})
run(func() { a.Sched.Run(ctx) })
run(func() { a.Billing.Run(ctx) })
run(func() { a.Cache.Run(ctx, a.cfg.Cache.SweepInterval.D()) })
if a.worker != nil {
run(func() { a.worker.Run(ctx) })
}