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
+39
View File
@@ -48,6 +48,20 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
b.cmdDownload(ctx, m, act, media.MP3)
case "thumb":
b.cmdDownload(ctx, m, act, media.Thumb)
case "fresh":
b.cmdLink(ctx, m, act, linkRequest{fresh: true})
case "subs":
b.cmdLink(ctx, m, act, linkRequest{subs: true})
case "history":
b.cmdHistory(ctx, m, act)
case "purge":
if b.isOwner(m) {
b.cmdPurge(ctx, m, act)
}
case "cache":
if b.isOwner(m) {
b.cmdCache(ctx, m, act)
}
case "info":
b.cmdInfo(ctx, m, act)
case "queue":
@@ -202,6 +216,23 @@ func (b *Bot) cmdDownload(ctx context.Context, m *telego.Message, act actor, pre
})
}
// cmdLink handles the commands that take a link and differ only in how
// the link flow runs: /fresh and /subs.
func (b *Bot) cmdLink(ctx context.Context, m *telego.Message, act actor, lr linkRequest) {
urls := messageLinks(m)
source := int64(m.MessageID)
if len(urls) == 0 && m.ReplyToMessage != nil {
urls = messageLinks(m.ReplyToMessage)
source = int64(m.ReplyToMessage.MessageID)
}
if len(urls) == 0 {
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "dl.usage", nil), nil)
return
}
lr.chatID, lr.chatType, lr.sourceID, lr.act, lr.url = m.Chat.ID, m.Chat.Type, source, act, urls[0]
b.startLink(ctx, lr)
}
func (b *Bot) cmdInfo(ctx context.Context, m *telego.Message, act actor) {
urls := messageLinks(m)
if len(urls) == 0 && m.ReplyToMessage != nil {
@@ -277,6 +308,10 @@ func (b *Bot) cmdCancel(ctx context.Context, m *telego.Message, act actor) {
n++
}
}
// Playlist items that have not started yet are cancelled too.
if waiting, err := b.store.CancelWaitingRequests(ctx, act.user.ID); err == nil {
n += int(waiting)
}
key := "cancel.done"
if n == 0 {
key = "cancel.nothing"
@@ -329,6 +364,10 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
b.cbDownload(ctx, q, act, id, arg)
case cbCancelCard:
b.cbCancelCard(ctx, q, act, id)
case cbPlaylist:
b.cbPlaylistStart(ctx, q, act, id, arg)
case cbResend:
b.cbResendFile(ctx, q, act, id)
case cbSkipQueue:
b.cbSkip(ctx, q, act, id)
case cbBuy: