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
+19 -2
View File
@@ -3,8 +3,8 @@
A Telegram bot that downloads video and audio from links, in private chats
and in groups. Written in Go on top of yt-dlp.
> Status: Phase 2 of 8 (core bot and payments). The cache, remote workers,
> group settings, the Mini App and more languages follow. See
> Status: Phase 3 of 8 (core bot, payments, cache). Remote workers, group
> settings, the Mini App and more languages follow. See
> [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) for the decisions made so far.
## What works today
@@ -24,6 +24,11 @@ and in groups. Written in Go on top of yt-dlp.
- Money is safe: append-only ledger, idempotent settlement, reconciliation
at startup, automatic refunds for failed downloads.
- Gift codes, referral rewards and tips.
- A smart cache: repeat downloads are instant, two people asking for the
same video share one download, lower qualities are made from a stored
copy. A cache hit is checked against exactly the same limits as a fresh
download.
- Playlists (each item its own job), subtitles, download history.
- Friendly, translated error messages. Raw tool output stays in the logs.
## Quick start
@@ -62,7 +67,10 @@ curl localhost:8080/healthz
| `/dl <link>` | Download a video. In groups also works as a reply to a link. |
| `/audio <link>` | Download the audio as MP3 |
| `/thumb <link>` | Send the thumbnail |
| `/subs <link>` | Pick a subtitle language and get the file |
| `/info <link>` | Show formats and sizes without downloading |
| `/fresh <link>` | Download again instead of using a saved copy |
| `/history` | Your past downloads, one tap to get them again |
| `/queue` | Your downloads in progress |
| `/cancel` | Cancel your downloads |
| `/account` | Your pass, balance and usage today (private chat) |
@@ -73,6 +81,13 @@ curl localhost:8080/healthz
| `/referral` | Your invite link and how many joined (private chat) |
| `/donate` | Send a tip (private chat) |
Owner commands (private chat, only for the IDs in `OWNER_IDS`):
| Command | What it does |
| --- | --- |
| `/purge <link>` | Take a video down: delete its files, block it for good |
| `/cache` | Cache size, hit rate and volume saved |
### Turning on payments
Payments are off until LNbits is configured. You need an LNbits wallet and
@@ -137,6 +152,8 @@ internal/bot Telegram handlers, cards, status, delivery
internal/service download rules shared by all front ends
internal/entitle who pays and which limits apply (pure function)
internal/billing wallets, ledger, passes, invoices, LNbits client
internal/cache file_id and stored-copy cache
internal/ffmpeg transcoding from a stored copy
internal/sched the job queue
internal/worker the download worker
internal/ytdlp yt-dlp and ffprobe wrapper