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
+60 -1
View File
@@ -31,7 +31,10 @@ In a group, use /dl with a link, or reply /dl to a message that has a link.
/dl link - download a video
/audio link - download the audio as MP3
/thumb link - get the thumbnail
/subs link - get the subtitles
/info link - show formats and sizes without downloading
/fresh link - download again instead of using a saved copy
/history - your past downloads, with one tap to get them again
/queue - your running downloads
/cancel - cancel your downloads
@@ -58,6 +61,7 @@ thumb = "Get the thumbnail"
info = "Show formats and sizes"
queue = "Your downloads in progress"
cancel = "Cancel your downloads"
history = "Your past downloads"
account = "Your pass, balance and usage"
buy = "Get a pass or credits"
help = "How to use the bot"
@@ -107,6 +111,8 @@ open_wallet = "Open in wallet"
check_payment = "Check payment"
this_file = "This file only"
buy_for_me = "Buy for me"
subs_auto = "{{.Lang}} (auto)"
resend = "{{.N}}. {{.Title}}"
[status]
queued = "In the queue. Position {{.Position}}, about {{.Wait}}."
@@ -115,6 +121,7 @@ downloading_bytes = "Downloading… {{.Done}}"
downloading_progress = "Downloading…\n{{.Bar}} {{.Percent}}\n{{.Done}} of {{.Total}}"
processing = "Processing the file…"
uploading = "Sending it to Telegram…"
preparing = "Getting it ready…"
done = "Done. Enjoy."
cancelled = "Cancelled."
failed = "That did not work. {{.Reason}}"
@@ -140,6 +147,7 @@ too_large = "This file is larger than your limit of {{.MaxSize}}. Try a lower qu
too_long = "This is longer than your limit of {{.MaxDuration}}."
live = "Live streams cannot be downloaded."
queue_full = "The queue is full right now. Please try again in a few minutes."
blocked = "This content is not available through this bot."
daily_volume = "You reached your daily volume of {{.MaxVolume}}. It resets at midnight UTC."
[limit.daily_downloads]
@@ -189,7 +197,8 @@ cancelled = "The download was cancelled."
worker_lost = "The download was interrupted. Please try again."
internal = "Something went wrong on my side. Please try again."
unknown = "The download failed. Please try again later."
playlist = "That link is a playlist. Send the link of a single video for now."
playlist = "That link is a playlist. Send it to me as a normal message to pick a format for the whole list."
playlist_nested = "This item is a list itself, so I skipped it."
send_failed = "I could not send the file to Telegram. Please try again."
# --- Money -------------------------------------------------------------------
@@ -310,3 +319,53 @@ skip_not_needed = "That download is not waiting in the queue anymore, so there i
[over]
options = "You can lift the limit:"
started = "Your limits allow it now. The download has started."
# --- Playlists, subtitles, history ------------------------------------------
[playlist]
empty = "I could not find any items in that playlist."
item_failed = "<b>{{.Title}}</b>\nSkipped. {{.Reason}}"
[playlist.card]
one = """
<b>{{.Title}}</b>
Playlist with {{.Count}} item. I will fetch {{.Take}} of them, one after another.
Pick a format for all of them:"""
other = """
<b>{{.Title}}</b>
Playlist with {{.Count}} items. I will fetch {{.Take}} of them, one after another.
Pick a format for all of them:"""
[playlist.queued]
one = "<b>{{.Title}}</b>\n{{.Count}} item is lined up. It starts as soon as you have a free slot. /cancel stops it."
other = "<b>{{.Title}}</b>\n{{.Count}} items are lined up. They start one after another as you have free slots. /cancel stops them."
[subs]
pick = "Pick a subtitle language:"
none = "This video has no subtitles."
[history]
empty = "You have no past downloads yet."
header = "<b>Your past downloads</b>\nTap one to get it again."
line = "{{.N}}. {{.Title}} · {{.Label}}"
# --- Owner commands ---------------------------------------------------------
[owner]
purge_usage = "Send a link, or a key as <code>extractor:video_id</code>.\nExample: <code>/purge https://example.com/video copyright notice</code>"
cache = """
<b>Cache</b>
Entries: {{.Entries}} ({{.Stored}} with a stored copy)
Stored: {{.Size}} of {{.Budget}}
Hits, all time: {{.Hits}}
Taken down: {{.Blocked}}
<b>Since the last start</b>
Hit rate: {{.Rate}} of {{.Since}} requests
Download volume saved: {{.Saved}}"""
[owner.purged]
one = "Taken down: <code>{{.Key}}</code>\n{{.Count}} cached file was removed. The key is blocked."
other = "Taken down: <code>{{.Key}}</code>\n{{.Count}} cached files were removed. The key is blocked."