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
View File
@@ -37,6 +37,8 @@ phase; sections for later phases say so.
| `internal/entitle` | Pure entitlement resolver: what pays, which limits apply. |
| `internal/billing` | Wallets, ledger, passes, invoices, settlement, gift codes. |
| `internal/billing/lnbits` | LNbits HTTP client. |
| `internal/cache` | file_id and stored-copy cache, TTLs, eviction, takedown. |
| `internal/ffmpeg` | Transcoding lower profiles from a stored copy. |
| `internal/links` | URL extraction, validation, redaction. |
| `internal/storage` | `Storage` interface and the local disk backend. |
| `internal/db` | Schema migrations and queries for SQLite and Postgres. |
@@ -220,6 +222,64 @@ by the ledger dedupe key, so every failure path can call it without
coordination. The reconcile loop also sweeps for deliveries that ended
without a file and were never released.
## The cache
The cache has three layers, from fastest to most general:
1. **Telegram `file_id`**, per bot. Sending by `file_id` costs no upload.
2. **A stored copy** in Storage. It is the fallback when a `file_id` stops
working and the source for transcoding lower profiles.
3. **Link metadata**, in memory for 30 minutes, so the card for a link that
was just seen appears without running yt-dlp.
### One gate
```
request
|
resolve: cache key, takedown check, what the cache has
|
admit: size, duration, live, daily quota, daily volume <-- same for all
|
+--+----------------+-----------------+------------------+
| hit | same result | higher profile | nothing
| | in flight | is stored |
v v v v
job created attach to that derive job download job
as "delivering" job (ffmpeg) (yt-dlp)
| | | |
+-------------------+-----------------+------------------+
|
charge, count quota (one transaction with the job or the attachment)
|
deliver: by file_id, or upload from the stored copy
```
`service.Start` is the only way a file gets to a user, and admission comes
before the fork. This is what "the cache never bypasses limits" means in
code: there is no cache path that skips `admit`. For a hit, `admit` is given
the real size and duration of the cached file.
### Sharing and per-requester caps
Jobs carry a `cache_key`. A request for a key that is already queued or
running attaches a second delivery to that job. Every delivery stores its
own `max_size` and `max_duration`, and the file is checked against them
when it is delivered, so sharing never widens anyone's limits.
### Self-healing
Delivery tries the `file_id` first. If Telegram rejects it, the bot clears
it, uploads the stored copy and saves the new id. If there is no stored
copy, the entry is dropped and the same job is queued as a fresh download.
### Sweeper
Every ten minutes the cache deletes expired entries, evicts stored copies
when over budget (least recently and least often used first, keeping their
`file_id`), and removes stored files that no live job and no cache entry
references.
## Internationalization
Handlers never contain user-facing text. They call `i18n.Bundle` with a