feat(db): TTL + per-account count eviction for notes_cache#3
Merged
Conversation
これまで `cleanup_cache()` は no-op で、`notes_cache` テーブルは無制限に 肥大化する設計だった (コメントで "kept indefinitely" と明示)。長期利用で DB が GB オーダーまで成長し、起動時の DB open / mmap が重くなる。 eviction を 2 段階で実装: - **TTL**: `cached_at < now - 90 days` の行を削除。鮮度の落ちたデータを掃除。 - **Per-account hard cap**: アカウントごとに最新 50000 件を残し、それ以外を 削除 (window function で 1 クエリ)。ヘビー利用者でも account あたり ~250MB 程度で hard cap される。 加えて `auto_vacuum=INCREMENTAL` を保証し、起動時に少量 (最大 1000 ページ) ずつ free page をディスクに返却する。長期蓄積した未使用領域を段階的に解放。 - 既存 DB の場合、`Database::open` で初回のみ `VACUUM` を 1 度走らせて auto_vacuum モードを切替 (SQLite 仕様)。 - テスト用に `cleanup_cache_inner(per_account_limit, ttl_days)` を private に分離し、小さい閾値で挙動を pin できるようにした。 - 単体テスト 6 件追加 (TTL カット / cap / アカウント独立性 / no-op / auto_vacuum モード / incremental_vacuum_step)。 定数: - `PER_ACCOUNT_NOTE_LIMIT = 50_000` - `NOTES_TTL_DAYS = 90` - `INCREMENTAL_VACUUM_PAGES_PER_BOOT = 1000` Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
notes_cacheテーブルに TTL + per-account 件数上限の eviction を実装し、auto_vacuum=INCREMENTALで free page を段階的にディスクへ返却する。Why
これまで
cleanup_cache()は no-op で、notes_cacheは無制限に肥大化する設計だった (db.rs:497 で "kept indefinitely" と明示)。長期利用で DB が GB オーダーまで成長し、起動時の DB open / mmap が重くなる問題があった。ヘビーユーザー試算: 1000 ノート/日 × 5KB ≒ 5MB/日 → 1 年で 1.8GB + FTS5 trigram index で更に +30〜50%。mmap 上限 256MB を超えると性能劣化。
Design
Eviction (2 段階)
cached_at < now - 90 daysの行を削除。cached_atは INSERT/UPDATE で都度nowに上書きされるので「最後に観測されてから N 日経過」を意味する。ROW_NUMBER() OVER (PARTITION BY account_id ORDER BY cached_at DESC)) で 1 クエリで評価。notes_fts(FTS5 trigram) はcontent='notes_cache'で連動しており、AFTER DELETEトリガーで自動的に掃除される。VACUUM
auto_vacuum=INCREMENTALをDatabase::openで保証。既存 DB の場合は初回 VACUUM が 1 度走る (SQLite 仕様)。PRAGMA incremental_vacuum(1000)で最大 1000 ページを返却。コストは数ミリ秒オーダー。Constants
テスト用に
cleanup_cache_inner(per_account_limit, ttl_days)を private に分離し、小さい閾値で挙動を pin できるようにした。Test plan
cargo test --lib db::で 24 件 全 pass (新規 6 件)cleanup_removes_notes_older_than_ttlcleanup_caps_per_account_countcleanup_per_account_independentcleanup_no_op_when_under_limitsauto_vacuum_mode_is_incremental_after_openincremental_vacuum_step_runs_without_errorcargo build通過Out of scope
cache_stats()を表示 + 手動クリアボタン UI を別 PR で追加予定