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
+8
View File
@@ -14,6 +14,7 @@ import (
"github.com/mymmrac/telego"
"github.com/noderunners/dlbot/internal/billing"
"github.com/noderunners/dlbot/internal/cache"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/i18n"
@@ -33,6 +34,7 @@ type Deps struct {
Billing *billing.Billing
// Cashu redeems ecash tokens. Nil turns that part of /redeem off.
Cashu billing.Cashu
Cache *cache.Cache
Storage storage.Storage
I18n *i18n.Bundle
Log *slog.Logger
@@ -50,6 +52,7 @@ type Bot struct {
svc *service.Service
bill *billing.Billing
cashu billing.Cashu
cache *cache.Cache
storage storage.Storage
tr *i18n.Bundle
log *slog.Logger
@@ -60,6 +63,7 @@ type Bot struct {
events chan sched.Event
payEvents chan billing.Event
uploads chan struct{} // limits concurrent uploads to Telegram
pump chan struct{} // wakes the playlist pump
rate *rateLimiter
bg sync.WaitGroup
baseCtx context.Context
@@ -67,6 +71,7 @@ type Bot struct {
statusMu sync.Mutex
statuses map[string]*statusState // by delivery ID
sending map[string]bool // jobs being delivered right now
pumping map[int64]bool // users whose next playlist item is being started
// Tunables, shortened in tests.
reconcileEvery time.Duration
@@ -97,6 +102,7 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
svc: d.Service,
bill: d.Billing,
cashu: d.Cashu,
cache: d.Cache,
storage: d.Storage,
tr: d.I18n,
log: d.Log,
@@ -106,10 +112,12 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
events: make(chan sched.Event, 1024),
payEvents: make(chan billing.Event, 256),
uploads: make(chan struct{}, 4),
pump: make(chan struct{}, 1),
rate: newRateLimiter(),
baseCtx: ctx,
statuses: map[string]*statusState{},
sending: map[string]bool{},
pumping: map[int64]bool{},
reconcileEvery: 5 * time.Second,
statusMinGap: 3 * time.Second,
invoiceGrace: 5 * time.Second,