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
+75
View File
@@ -0,0 +1,75 @@
-- +goose Up
-- The smart cache. One row per (bot, extractor, video, profile).
-- tg_file_id layer 1: resend instantly, valid for one bot token only
-- storage_key layer 2: stored copy, used for re-upload and transcoding
-- A row with status 'blocked' is a takedown: it has no file and stops the
-- key from being downloaded or cached again.
CREATE TABLE cache_entries (
bot_id BIGINT NOT NULL,
extractor TEXT NOT NULL,
video_id TEXT NOT NULL,
profile TEXT NOT NULL,
tg_file_id TEXT NOT NULL DEFAULT '',
tg_file_unique_id TEXT NOT NULL DEFAULT '',
storage_key TEXT NOT NULL DEFAULT '',
size BIGINT NOT NULL DEFAULT 0,
duration BIGINT NOT NULL DEFAULT 0,
title TEXT NOT NULL DEFAULT '',
uploader TEXT NOT NULL DEFAULT '',
file_ext TEXT NOT NULL DEFAULT '',
width INTEGER NOT NULL DEFAULT 0,
height INTEGER NOT NULL DEFAULT 0,
hits BIGINT NOT NULL DEFAULT 0,
last_hit BIGINT NOT NULL DEFAULT 0,
expires_at BIGINT NOT NULL DEFAULT 0,
private BOOLEAN NOT NULL DEFAULT FALSE,
status TEXT NOT NULL DEFAULT 'ready',
created_at BIGINT NOT NULL,
PRIMARY KEY (bot_id, extractor, video_id, profile)
);
CREATE INDEX cache_expiry_idx ON cache_entries (status, expires_at);
CREATE INDEX cache_lru_idx ON cache_entries (status, last_hit);
-- Owner blocklist: kind is 'site' (a host) or 'key' (extractor and video).
CREATE TABLE blocklist (
kind TEXT NOT NULL,
value TEXT NOT NULL,
reason TEXT NOT NULL DEFAULT '',
created_by BIGINT NOT NULL DEFAULT 0,
created_at BIGINT NOT NULL,
PRIMARY KEY (kind, value)
);
-- source_key: stored copy a derive job transcodes from.
-- tg_file_id: file_id a cache hit resends.
-- cacheable: whether the result may enter the cache.
-- cache_hit: the job was served from the cache and never saw a worker.
ALTER TABLE jobs ADD COLUMN source_key TEXT NOT NULL DEFAULT '';
ALTER TABLE jobs ADD COLUMN tg_file_id TEXT NOT NULL DEFAULT '';
ALTER TABLE jobs ADD COLUMN cacheable BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE jobs ADD COLUMN cache_hit BOOLEAN NOT NULL DEFAULT FALSE;
CREATE INDEX jobs_cache_key_idx ON jobs (cache_key, status);
-- Each requester keeps their own caps, because several requesters with
-- different limits can share one job.
ALTER TABLE deliveries ADD COLUMN max_size BIGINT NOT NULL DEFAULT 0;
ALTER TABLE deliveries ADD COLUMN max_duration BIGINT NOT NULL DEFAULT 0;
-- Playlist items are requests that wait their turn under a parent.
ALTER TABLE requests ADD COLUMN parent_id TEXT NOT NULL DEFAULT '';
ALTER TABLE requests ADD COLUMN profile TEXT NOT NULL DEFAULT '';
CREATE INDEX requests_waiting_idx ON requests (status, user_id, created_at);
-- +goose Down
DROP INDEX requests_waiting_idx;
ALTER TABLE requests DROP COLUMN profile;
ALTER TABLE requests DROP COLUMN parent_id;
ALTER TABLE deliveries DROP COLUMN max_duration;
ALTER TABLE deliveries DROP COLUMN max_size;
DROP INDEX jobs_cache_key_idx;
ALTER TABLE jobs DROP COLUMN cache_hit;
ALTER TABLE jobs DROP COLUMN cacheable;
ALTER TABLE jobs DROP COLUMN tg_file_id;
ALTER TABLE jobs DROP COLUMN source_key;
DROP TABLE blocklist;
DROP TABLE cache_entries;