From 2ff0da66a00963a5bbbebab547297ca6417d3bcf Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 11:26:28 +0000 Subject: [PATCH 01/13] Use Solid Cache for production Rails cache Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- Gemfile | 3 +++ Gemfile.lock | 5 +++++ config/cache.yml | 14 ++++++++++++++ config/environments/production.rb | 4 ++-- .../20261008120000_create_solid_cache_entries.rb | 15 +++++++++++++++ db/schema.rb | 14 ++++++++++++-- docs/development.md | 2 +- docs/self-hosting.md | 2 ++ 8 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 config/cache.yml create mode 100644 db/migrate/20261008120000_create_solid_cache_entries.rb diff --git a/Gemfile b/Gemfile index 1e602d0dc..81bb4ab5e 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,9 @@ gem "benchmark" gem "sqlite3" gem "redis", "~> 5.4" +# Caching +gem "solid_cache", "~> 1.0.10" + # Deployment gem "puma", "~> 8.0" diff --git a/Gemfile.lock b/Gemfile.lock index 50095f405..462bdcb57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -390,6 +390,10 @@ GEM rack-protection (= 4.2.1) rack-session (>= 2.0.0, < 3) tilt (~> 2.0) + solid_cache (1.0.10) + activejob (>= 7.2) + activerecord (>= 7.2) + railties (>= 7.2) sqlite3 (2.10.2-aarch64-linux-gnu) sqlite3 (2.10.2-arm64-darwin) sqlite3 (2.10.2-x86_64-darwin) @@ -463,6 +467,7 @@ DEPENDENCIES selenium-webdriver sentry-rails sentry-ruby + solid_cache (~> 1.0.10) sqlite3 stimulus-rails surfguard! diff --git a/config/cache.yml b/config/cache.yml new file mode 100644 index 000000000..633899aa8 --- /dev/null +++ b/config/cache.yml @@ -0,0 +1,14 @@ +default: &default + store_options: + max_age: <%= 30.days.to_i %> + max_size: <%= 64.megabytes %> + namespace: <%= Rails.env %> + +development: + <<: *default + +test: + <<: *default + +production: + <<: *default diff --git a/config/environments/production.rb b/config/environments/production.rb index 369bb5bf0..af8f1a648 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -68,8 +68,8 @@ # Don't log any deprecations. config.active_support.report_deprecations = false - # Cache in memory for now - config.cache_store = :redis_cache_store + # Persist the Rails cache in the primary SQLite database. + config.cache_store = :solid_cache_store # Assets are cacheable config.public_file_server.headers = { diff --git a/db/migrate/20261008120000_create_solid_cache_entries.rb b/db/migrate/20261008120000_create_solid_cache_entries.rb new file mode 100644 index 000000000..3a7fb5818 --- /dev/null +++ b/db/migrate/20261008120000_create_solid_cache_entries.rb @@ -0,0 +1,15 @@ +class CreateSolidCacheEntries < ActiveRecord::Migration[8.2] + def change + create_table :solid_cache_entries do |t| + t.binary :key, limit: 1024, null: false + t.binary :value, limit: 536_870_912, null: false + t.datetime :created_at, null: false + t.integer :key_hash, limit: 8, null: false + t.integer :byte_size, limit: 4, null: false + + t.index :byte_size + t.index [ :key_hash, :byte_size ] + t.index :key_hash, unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ccb79c9f8..acea79457 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.2].define(version: 2026_10_07_190000) do +ActiveRecord::Schema[8.2].define(version: 2026_10_08_120000) do create_table "accounts", force: :cascade do |t| t.datetime "created_at", null: false t.text "custom_styles" @@ -147,6 +147,17 @@ t.index ["user_id"], name: "index_sessions_on_user_id" end + create_table "solid_cache_entries", force: :cascade do |t| + t.binary "key", limit: 1024, null: false + t.binary "value", limit: 536870912, null: false + t.datetime "created_at", null: false + t.integer "key_hash", limit: 8, null: false + t.integer "byte_size", limit: 4, null: false + t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size" + t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size" + t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true + end + create_table "users", force: :cascade do |t| t.text "bio" t.string "bot_token" @@ -184,4 +195,3 @@ # Note that virtual tables may not work with other database engines. Be careful if changing database. create_virtual_table "message_search_index", "fts5", ["body", "tokenize=porter"] end - diff --git a/docs/development.md b/docs/development.md index c53e24360..1174ebc7e 100644 --- a/docs/development.md +++ b/docs/development.md @@ -28,7 +28,7 @@ You'll be able to access the app at http://localhost:3000. On first run you'll be guided through creating your admin account, and you can sign in with that account from then on. -Note that Campfire needs Redis (for Action Cable, caching, and background jobs), so if you've restarted your machine or stopped the container, `docker start campfire-redis` will bring it back. +Note that Campfire needs Redis in development (for Action Cable, the development cache, and background jobs), so if you've restarted your machine or stopped the container, `docker start campfire-redis` will bring it back. Production's Rails cache uses Solid Cache in the primary SQLite database; Redis is still used for Action Cable. ### Web Push notifications diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 5398e0c5b..0cdb6b3e5 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -169,6 +169,8 @@ docker pull ghcr.io/basecamp/once-campfire:latest Any pending database migrations run automatically when the container boots. +The Rails cache is stored in the primary SQLite database. The Solid Cache table is added by a normal database migration, so no extra volume or cache database needs to be configured. Existing Redis cache entries are not copied; the cache starts cold and repopulates as requests arrive. Redis is still required for Action Cable. The app's bounded per-worker response and fragment caches remain in memory and are separate from `Rails.cache`. + ### Backups To back up your instance, back up the contents of the `/rails/storage` volume. From e0567da031c08dfb7a3e7bda9f1d13226a8e9c89 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 11:28:18 +0000 Subject: [PATCH 02/13] Keep cache docs independent of queue migration Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- docs/development.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index 1174ebc7e..c53e24360 100644 --- a/docs/development.md +++ b/docs/development.md @@ -28,7 +28,7 @@ You'll be able to access the app at http://localhost:3000. On first run you'll be guided through creating your admin account, and you can sign in with that account from then on. -Note that Campfire needs Redis in development (for Action Cable, the development cache, and background jobs), so if you've restarted your machine or stopped the container, `docker start campfire-redis` will bring it back. Production's Rails cache uses Solid Cache in the primary SQLite database; Redis is still used for Action Cable. +Note that Campfire needs Redis (for Action Cable, caching, and background jobs), so if you've restarted your machine or stopped the container, `docker start campfire-redis` will bring it back. ### Web Push notifications From 0f20c96620c210b3f94903a22af8f8f0860d1ee9 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 11:36:05 +0000 Subject: [PATCH 03/13] Give Solid Cache its own SQLite database Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- config/cache.yml | 1 + config/database.yml | 6 ++ config/environments/production.rb | 2 +- db/cache_schema.rb | 12 ++++ ...261008120000_create_solid_cache_entries.rb | 15 ----- db/schema.rb | 13 +---- docs/self-hosting.md | 9 ++- docs/upgrades/solid-cache.md | 13 +++++ hooks/post-restore | 12 +++- test/scripts/solid_cache_restore_test.rb | 55 +++++++++++++++++++ 10 files changed, 104 insertions(+), 34 deletions(-) create mode 100644 db/cache_schema.rb delete mode 100644 db/migrate/20261008120000_create_solid_cache_entries.rb create mode 100644 docs/upgrades/solid-cache.md create mode 100644 test/scripts/solid_cache_restore_test.rb diff --git a/config/cache.yml b/config/cache.yml index 633899aa8..8d477dfb5 100644 --- a/config/cache.yml +++ b/config/cache.yml @@ -11,4 +11,5 @@ test: <<: *default production: + database: <%= ENV.fetch("DATABASE", "cache") %> <<: *default diff --git a/config/database.yml b/config/database.yml index 845eb70f2..35fcd9c96 100644 --- a/config/database.yml +++ b/config/database.yml @@ -37,3 +37,9 @@ production: primary: <<: *default database: storage/db/production.sqlite3 + + cache: + <<: *default + database: storage/db/production_cache.sqlite3 + migrations_paths: db/cache_migrate + schema_dump: cache_schema.rb diff --git a/config/environments/production.rb b/config/environments/production.rb index af8f1a648..729ad39c2 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -68,7 +68,7 @@ # Don't log any deprecations. config.active_support.report_deprecations = false - # Persist the Rails cache in the primary SQLite database. + # Persist the Rails cache in its own SQLite database. config.cache_store = :solid_cache_store # Assets are cacheable diff --git a/db/cache_schema.rb b/db/cache_schema.rb new file mode 100644 index 000000000..81a410d18 --- /dev/null +++ b/db/cache_schema.rb @@ -0,0 +1,12 @@ +ActiveRecord::Schema[7.2].define(version: 1) do + create_table "solid_cache_entries", force: :cascade do |t| + t.binary "key", limit: 1024, null: false + t.binary "value", limit: 536870912, null: false + t.datetime "created_at", null: false + t.integer "key_hash", limit: 8, null: false + t.integer "byte_size", limit: 4, null: false + t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size" + t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size" + t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true + end +end diff --git a/db/migrate/20261008120000_create_solid_cache_entries.rb b/db/migrate/20261008120000_create_solid_cache_entries.rb deleted file mode 100644 index 3a7fb5818..000000000 --- a/db/migrate/20261008120000_create_solid_cache_entries.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateSolidCacheEntries < ActiveRecord::Migration[8.2] - def change - create_table :solid_cache_entries do |t| - t.binary :key, limit: 1024, null: false - t.binary :value, limit: 536_870_912, null: false - t.datetime :created_at, null: false - t.integer :key_hash, limit: 8, null: false - t.integer :byte_size, limit: 4, null: false - - t.index :byte_size - t.index [ :key_hash, :byte_size ] - t.index :key_hash, unique: true - end - end -end diff --git a/db/schema.rb b/db/schema.rb index acea79457..f77e62007 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.2].define(version: 2026_10_08_120000) do +ActiveRecord::Schema[8.2].define(version: 2026_10_07_190000) do create_table "accounts", force: :cascade do |t| t.datetime "created_at", null: false t.text "custom_styles" @@ -147,17 +147,6 @@ t.index ["user_id"], name: "index_sessions_on_user_id" end - create_table "solid_cache_entries", force: :cascade do |t| - t.binary "key", limit: 1024, null: false - t.binary "value", limit: 536870912, null: false - t.datetime "created_at", null: false - t.integer "key_hash", limit: 8, null: false - t.integer "byte_size", limit: 4, null: false - t.index ["byte_size"], name: "index_solid_cache_entries_on_byte_size" - t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size" - t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true - end - create_table "users", force: :cascade do |t| t.text "bio" t.string "bot_token" diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 0cdb6b3e5..7fad86feb 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -169,7 +169,9 @@ docker pull ghcr.io/basecamp/once-campfire:latest Any pending database migrations run automatically when the container boots. -The Rails cache is stored in the primary SQLite database. The Solid Cache table is added by a normal database migration, so no extra volume or cache database needs to be configured. Existing Redis cache entries are not copied; the cache starts cold and repopulates as requests arrive. Redis is still required for Action Cable. The app's bounded per-worker response and fragment caches remain in memory and are separate from `Rails.cache`. +Production `Rails.cache` uses Solid Cache in its own SQLite database, `storage/db/production_cache.sqlite3`. Startup's normal `db:prepare` creates it from the checked-in cache schema. Existing Redis entries are not copied; the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. The bounded per-worker response and fragment caches remain in memory and are separate from `Rails.cache`. + +See the [Solid Cache upgrade notes](upgrades/solid-cache.md) for backup and restore behavior. ### Backups @@ -192,10 +194,10 @@ docker run --rm \ --volume campfire:/rails/storage \ --volume "$PWD":/backup \ ghcr.io/basecamp/once-campfire:latest \ - tar czf "/backup/campfire-backup.tar.gz" -C /rails storage + tar czf "/backup/campfire-backup.tar.gz" --exclude='storage/db/production_cache.sqlite3*' -C /rails storage ``` -This gives you a `campfire-backup.tar.gz` in your current directory containing the database snapshot and all uploaded files. +This gives you a `campfire-backup.tar.gz` in your current directory containing the database snapshot and all uploaded files. The disposable Solid Cache database is excluded and is rebuilt empty after a restore. Copy it somewhere safe, ideally off the machine. To restore, extract the archive back into a (stopped) instance's volume, and replace the live database with the snapshot: @@ -209,6 +211,7 @@ docker run --rm \ bash -c "tar xzf /backup/campfire-backup.tar.gz -C /rails && cp /rails/storage/backups/production.sqlite3 /rails/storage/db/production.sqlite3 && rm -f /rails/storage/db/production.sqlite3-wal /rails/storage/db/production.sqlite3-shm && + rm -f /rails/storage/db/production_cache.sqlite3 /rails/storage/db/production_cache.sqlite3-wal /rails/storage/db/production_cache.sqlite3-shm /rails/storage/db/production_cache.sqlite3-journal && chown -R rails:rails /rails/storage" ``` diff --git a/docs/upgrades/solid-cache.md b/docs/upgrades/solid-cache.md new file mode 100644 index 000000000..be5979333 --- /dev/null +++ b/docs/upgrades/solid-cache.md @@ -0,0 +1,13 @@ +# Migrating the Rails cache to Solid Cache + +Production `Rails.cache` moves from Redis to Solid Cache in a dedicated SQLite database at `storage/db/production_cache.sqlite3`. The normal startup `db:prepare` creates the database and loads `db/cache_schema.rb`; no cache data needs to be migrated. + +## Upgrade + +Deploy the release normally. Existing Redis entries are not copied, so the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. Campfire's bounded per-worker response and fragment caches are separate in-memory stores and are unchanged. + +## Backups and restores + +The cache database is disposable: `script/admin/prepare-backup` intentionally does not snapshot it, and the self-hosting backup command excludes its SQLite file and sidecars. During restore, `hooks/post-restore` removes any cache database left in the volume, including its WAL/SHM/journal files. The next `db:prepare` creates a clean cache database, avoiding entries from a different point in time than the restored application database. + +No Redis-to-Solid-Cache data export or maintenance window is required. Rolling back to a release that uses Redis does not require a database rollback; the Solid Cache database is unused by that release. diff --git a/hooks/post-restore b/hooks/post-restore index 0b9138d1e..a2a5dab93 100755 --- a/hooks/post-restore +++ b/hooks/post-restore @@ -1,9 +1,15 @@ #!/bin/bash set -euo pipefail -db_name="${RAILS_ENV:-production}.sqlite3" -snapshot="/rails/storage/backups/$db_name" -db="/rails/storage/db/$db_name" +env="${RAILS_ENV:-production}" +app_root="${APP_ROOT:-/rails}" +db_name="$env.sqlite3" +snapshot="$app_root/storage/backups/$db_name" +db="$app_root/storage/db/$db_name" +cache_db="$app_root/storage/db/${env}_cache.sqlite3" + +# Cache entries are disposable and may belong to a different point in time than the restored app DB. +rm -f "$cache_db" "$cache_db-wal" "$cache_db-shm" "$cache_db-journal" if [ -f "$snapshot" ]; then mkdir -p "$(dirname "$db")" diff --git a/test/scripts/solid_cache_restore_test.rb b/test/scripts/solid_cache_restore_test.rb new file mode 100644 index 000000000..7fb31a6fd --- /dev/null +++ b/test/scripts/solid_cache_restore_test.rb @@ -0,0 +1,55 @@ +require "test_helper" +require "fileutils" +require "open3" +require "tmpdir" + +class SolidCacheRestoreTest < ActiveSupport::TestCase + setup do + @app_root = Dir.mktmpdir("campfire-solid-cache-restore") + end + + teardown do + FileUtils.remove_entry(@app_root) + end + + test "restores the primary database and removes disposable cache database and sidecars" do + write_file("storage/backups/production.sqlite3", "restored primary") + write_file("storage/db/production.sqlite3", "current primary") + write_file("storage/db/production_cache.sqlite3", "stale cache") + write_file("storage/db/production_cache.sqlite3-wal", "stale cache wal") + write_file("storage/db/production_cache.sqlite3-shm", "stale cache shm") + write_file("storage/db/production_cache.sqlite3-journal", "stale cache journal") + + _output, status = run_hook + + assert status.success? + assert_equal "restored primary", read_file("storage/db/production.sqlite3") + refute File.exist?(path("storage/db/production_cache.sqlite3")) + refute File.exist?(path("storage/db/production_cache.sqlite3-wal")) + refute File.exist?(path("storage/db/production_cache.sqlite3-shm")) + refute File.exist?(path("storage/db/production_cache.sqlite3-journal")) + end + + private + def run_hook + Open3.capture2e( + { "APP_ROOT" => @app_root, "RAILS_ENV" => "production" }, + "bash", + Rails.root.join("hooks/post-restore").to_s + ) + end + + def write_file(relative_path, contents) + file_path = path(relative_path) + FileUtils.mkdir_p(File.dirname(file_path)) + File.write(file_path, contents) + end + + def read_file(relative_path) + File.read(path(relative_path)) + end + + def path(relative_path) + File.join(@app_root, relative_path) + end +end From 883a1f2e19bfbb16dcd9b8c6a4e83d1219c36657 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 11:37:26 +0000 Subject: [PATCH 04/13] Clarify that cache data is excluded from backups Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- docs/self-hosting.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 7fad86feb..2f8918927 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -186,7 +186,7 @@ docker exec campfire script/admin/prepare-backup (If you're using Docker Compose, replace `docker exec campfire` with `docker compose exec web`) -Then archive the whole storage volume to a file on the host: +Then archive the storage volume to a file on the host, excluding the disposable cache database: ```sh docker run --rm \ From fffc6db753672e02c5b7ff4ecf6d527b1d2eff8e Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 12:20:15 +0000 Subject: [PATCH 05/13] Keep WAL auto-checkpointing for Solid Cache DB Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- config/database.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/database.yml b/config/database.yml index 35fcd9c96..33b7c383d 100644 --- a/config/database.yml +++ b/config/database.yml @@ -43,3 +43,7 @@ production: database: storage/db/production_cache.sqlite3 migrations_paths: db/cache_migrate schema_dump: cache_schema.rb + # The background checkpoint contender tracks the primary database; keep + # SQLite's normal checkpointing for cache writes. + pragmas: + wal_autocheckpoint: 1000 From 3b33f50e0f9cb1b38646c62c20d4a17c850cc982 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Thu, 8 Oct 2026 12:29:18 +0000 Subject: [PATCH 06/13] Raise Solid Cache default capacity to 256 MiB Amp-Thread-ID: https://ampcode.com/threads/T-01a11acc-6153-7316-a06c-fcf2d713cd64 Co-authored-by: Amp --- config/cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/cache.yml b/config/cache.yml index 8d477dfb5..1d369b772 100644 --- a/config/cache.yml +++ b/config/cache.yml @@ -1,7 +1,7 @@ default: &default store_options: max_age: <%= 30.days.to_i %> - max_size: <%= 64.megabytes %> + max_size: <%= 256.megabytes %> namespace: <%= Rails.env %> development: From e369cd69c269ba578324fdde6c851b27ff0fa786 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Fri, 9 Oct 2026 07:04:55 +0000 Subject: [PATCH 07/13] Configure Solid Cache for performance environment Amp-Thread-ID: https://ampcode.com/threads/T-01a11f69-87a0-75ed-9ac9-3862140a226a --- config/cache.yml | 4 ++++ config/database.yml | 8 ++++++++ docs/upgrades/solid-cache.md | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/cache.yml b/config/cache.yml index 1d369b772..f80e8951b 100644 --- a/config/cache.yml +++ b/config/cache.yml @@ -10,6 +10,10 @@ development: test: <<: *default +performance: + database: cache + <<: *default + production: database: <%= ENV.fetch("DATABASE", "cache") %> <<: *default diff --git a/config/database.yml b/config/database.yml index 33b7c383d..dd428e8f0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -33,6 +33,14 @@ performance: <<: *default database: storage/db/performance.sqlite3 + cache: + <<: *default + database: storage/db/performance_cache.sqlite3 + migrations_paths: db/cache_migrate + schema_dump: cache_schema.rb + pragmas: + wal_autocheckpoint: 1000 + production: primary: <<: *default diff --git a/docs/upgrades/solid-cache.md b/docs/upgrades/solid-cache.md index be5979333..24a123195 100644 --- a/docs/upgrades/solid-cache.md +++ b/docs/upgrades/solid-cache.md @@ -1,6 +1,6 @@ # Migrating the Rails cache to Solid Cache -Production `Rails.cache` moves from Redis to Solid Cache in a dedicated SQLite database at `storage/db/production_cache.sqlite3`. The normal startup `db:prepare` creates the database and loads `db/cache_schema.rb`; no cache data needs to be migrated. +Production `Rails.cache` moves from Redis to Solid Cache in a dedicated SQLite database at `storage/db/production_cache.sqlite3`. The normal startup `db:prepare` creates the database and loads `db/cache_schema.rb`; no cache data needs to be migrated. Solid Cache's 256 MiB `max_size` is an estimated eviction target, not a hard limit on SQLite or WAL disk usage. ## Upgrade From 8c8551b005cf0560544706027f6751cb0f345a4c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 12:40:15 +0000 Subject: [PATCH 08/13] Name the production cache database directly The Solid Cache generator evaluates ENV.fetch("DATABASE") once, when it writes cache.yml. Copied verbatim, it ran on every boot, so an unrelated DATABASE variable in the host environment would redirect the cache. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01N8uPpsqMRJrp8jqATFJW45 --- config/cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/cache.yml b/config/cache.yml index f80e8951b..c1cfbd488 100644 --- a/config/cache.yml +++ b/config/cache.yml @@ -15,5 +15,5 @@ performance: <<: *default production: - database: <%= ENV.fetch("DATABASE", "cache") %> + database: cache <<: *default From 449693ece4836b042b7e38551efc91960bc40d1a Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Fri, 9 Oct 2026 10:35:26 +0000 Subject: [PATCH 09/13] Use native shared fragments and one bounded local hot-path store Build on the Solid Cache migration without sacrificing observer-validated response and authorization reuse. Replace custom FIFO bookkeeping with Rails MemoryStore and move safe content-keyed collections to Rails.cache. Remove private Jbuilder hooks and keep unsafe nested JSON uncached. Amp-Thread-ID: https://ampcode.com/threads/T-01a11fe5-588c-71af-905f-84af46bd6951 --- README.md | 12 +- app/controllers/application_controller.rb | 2 +- app/controllers/concerns/cached_responses.rb | 32 +---- app/controllers/concerns/fragment_caching.rb | 13 ++ .../messages/by_bots_controller.rb | 1 + app/controllers/users/sidebars_controller.rb | 2 +- app/helpers/messages_helper.rb | 41 +++---- app/helpers/rooms_helper.rb | 2 +- app/models/fragment_cache.rb | 20 ---- app/models/message/pagination.rb | 6 +- app/models/record_cache.rb | 15 ++- app/models/response_cache.rb | 35 ++---- app/views/messages/_actions.html.erb | 2 +- app/views/messages/_message.html.erb | 4 +- app/views/messages/_message.json.jbuilder | 24 ++-- .../messages/boosts/_boost.json.jbuilder | 14 +-- app/views/messages/index.html.erb | 2 +- .../rooms/refreshes/show.turbo_stream.erb | 2 +- app/views/rooms/show.html.erb | 2 +- app/views/searches/index.html.erb | 2 +- app/views/users/_user.json.jbuilder | 6 +- .../users/sidebars/rooms/_direct.html.erb | 4 +- app/views/users/sidebars/show.html.erb | 2 +- bench/README.md | 3 +- bench/message_hot_paths.rb | 6 +- config/initializers/fragment_cache.rb | 24 ---- config/initializers/version.rb | 7 ++ docs/self-hosting.md | 2 +- docs/upgrades/solid-cache.md | 2 +- test/controllers/cached_responses_test.rb | 4 +- test/controllers/fragment_cache_test.rb | 113 ++++++++++++++---- test/helpers/messages_helper_test.rb | 5 +- test/models/record_cache_test.rb | 26 ++-- test/models/response_cache_test.rb | 11 +- 34 files changed, 226 insertions(+), 222 deletions(-) create mode 100644 app/controllers/concerns/fragment_caching.rb delete mode 100644 app/models/fragment_cache.rb delete mode 100644 config/initializers/fragment_cache.rb diff --git a/README.md b/README.md index a73d72e64..bd8a83aa2 100644 --- a/README.md +++ b/README.md @@ -49,11 +49,13 @@ If you'd rather run the Docker image yourself, you can read more about that in t > that people have someone to contact if they need help with their account. If that bothers you, put in any > email address you want and create yourself a new admin account. -Authenticated room, message, sidebar and search pages use a bounded 64 MiB cache per worker. -Set `CAMPFIRE_RESPONSE_CACHE_MB=0` to disable it. Every request still checks authentication -and room access; commits from any SQLite writer invalidate complete cached HTML and gzip bodies. -Native HTML, JSON and stream fragments have a separate 64 MiB memory limit per worker; -shared rate limits retain their existing store. +Authenticated room, message, sidebar and search pages and authorization snapshots share +one approximately 64 MiB Rails MemoryStore budget per worker. Set `CAMPFIRE_RESPONSE_CACHE_MB=0` +to disable this local cache. Every request checks authentication and room access against the +captured SQLite generation; commits from any writer invalidate local snapshots and complete HTML/gzip responses. +Native HTML/stream fragments and shared rate limits use `Rails.cache` (Solid Cache in production). +Message fragments fingerprint their rendered inputs, so unrelated commits retain reuse and SQL edits +without timestamp changes remain visible. Attachment/direct-room fragments and JSON render fresh. ## Other implementations diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b73e59242..f93432e0e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,4 @@ class ApplicationController < ActionController::Base - include AllowBrowser, Authentication, Authorization, BlockBannedRequests, SetCurrentRequest, SetPlatform, TrackedRoomVisit, VersionHeaders, CachedResponses, InlineScriptHashes + include AllowBrowser, Authentication, Authorization, BlockBannedRequests, SetCurrentRequest, SetPlatform, TrackedRoomVisit, VersionHeaders, CachedResponses, InlineScriptHashes, FragmentCaching include Turbo::Streams::Broadcasts, Turbo::Streams::StreamName end diff --git a/app/controllers/concerns/cached_responses.rb b/app/controllers/concerns/cached_responses.rb index b17e29a9b..a11fd4027 100644 --- a/app/controllers/concerns/cached_responses.rb +++ b/app/controllers/concerns/cached_responses.rb @@ -9,39 +9,13 @@ module CachedResponses prepend_before_action :capture_response_cache_version end - def perform_caching - return false unless super && @response_cache_version.present? && !FragmentCache.transaction_open? - - # Check again after authentication, before the first native fragment lookup. - # Later renders retain this immutable namespace and cannot poison a new epoch. - unless defined?(@fragment_cache_snapshot_valid) - @fragment_cache_snapshot_valid = @response_cache_version == ResponseCache.instance.version - end - @fragment_cache_snapshot_valid - end - - # Keep the class store (including shared rate limits) and Rails.cache unchanged. - def cache_store - FragmentCache.store - end - - def combined_fragment_cache_key(key) - @fragment_cache_context ||= [ - request.base_url, request.script_name, request.format.to_s, I18n.locale, - Current.user&.id, (Digest::SHA256.hexdigest(Current.session.token) if Current.session) - ].freeze - version = @response_cache_version unless Array(key).flatten.any? { |part| part.is_a?(FragmentCache::ContentKey) } - super([ version, @fragment_cache_context, key ]) - end - private def read_record_cache_version @response_cache_version if request.get? || request.head? end def capture_response_cache_version - # Capture for native HTML/JSON/stream renders too, even with page reuse off. - # Detached renderers do not run callbacks and therefore render uncached. + # One immutable snapshot for authorization records and completed responses. @response_cache_version = ResponseCache.instance.version end @@ -91,7 +65,7 @@ def cache_completed_response(key, original_session, encoding) end response.headers["Content-Encoding"] = "gzip" if encoding == "gzip" response.headers["Vary"] = (response.headers["Vary"].to_s.split(/,\s*/) | [ "Accept-Encoding" ]).join(", ") - headers = response.headers.slice(*CACHE_HEADERS).to_h.freeze + headers = response.headers.slice(*CACHE_HEADERS).to_h.transform_values { |value| value.dup.freeze }.freeze entry = { body: body.freeze, headers: headers }.freeze ResponseCache.instance.write(key, @response_cache_version, entry) entry @@ -103,7 +77,7 @@ def cacheable_read_request? !authenticated_by.bot_key? && flash.empty? && !request.headers["If-None-Match"] && !request.headers["If-Modified-Since"] && !Rails.application.config.content_security_policy_nonce_generator && - !FragmentCache.transaction_open? + !ActiveRecord::Base.connection_pool.with_connection(&:transaction_open?) end def response_cache_key(encoding) diff --git a/app/controllers/concerns/fragment_caching.rb b/app/controllers/concerns/fragment_caching.rb new file mode 100644 index 000000000..fb416681e --- /dev/null +++ b/app/controllers/concerns/fragment_caching.rb @@ -0,0 +1,13 @@ +module FragmentCaching + extend ActiveSupport::Concern + + included do + before_action { @rendering_request = true } + fragment_cache_key { [ Rails.configuration.x.presentation_cache_version, request.base_url, request.script_name, I18n.locale ] } + end + + # Detached broadcasts and uncommitted renders must not populate shared caches. + def perform_caching + super && @rendering_request && !ActiveRecord::Base.connection_pool.with_connection(&:transaction_open?) + end +end diff --git a/app/controllers/messages/by_bots_controller.rb b/app/controllers/messages/by_bots_controller.rb index d652afd05..da12c6f04 100644 --- a/app/controllers/messages/by_bots_controller.rb +++ b/app/controllers/messages/by_bots_controller.rb @@ -10,6 +10,7 @@ class Messages::ByBotsController < MessagesController def index @messages = find_paged_messages + @messages.preload_associations(@messages) set_pagination_headers end diff --git a/app/controllers/users/sidebars_controller.rb b/app/controllers/users/sidebars_controller.rb index b9ab61efd..4c1d531c0 100644 --- a/app/controllers/users/sidebars_controller.rb +++ b/app/controllers/users/sidebars_controller.rb @@ -4,7 +4,7 @@ class Users::SidebarsController < ApplicationController def show visible_memberships = Current.user.memberships.visible - @direct_memberships = visible_memberships.with_direct_rooms + @direct_memberships = visible_memberships.with_direct_rooms.preload(room: :users) @other_memberships = visible_memberships.with_ordered_room.without_direct_rooms @direct_placeholder_users = find_direct_placeholder_users diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index 6bef743e9..5f7623a42 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -75,31 +75,26 @@ def message_presentation(message) "" end - # These controls contain URLs and static markup, but no viewer or token state. - # Keep them across database commits; attachment controls still render afresh. - def cache_message_actions(message, &block) - return capture(&block) unless controller.perform_caching && !message.attachment? - - key = fragment_name_with_digest([ - "message-actions-v1", request.base_url, request.script_name, I18n.locale, message.id, message.room_id - ], nil) - FragmentCache.store.fetch(key) { capture(&block) } + def render_messages(messages) + cached = false + if controller.perform_caching + messages.preload_associations(messages) if messages.respond_to?(:preload_associations) + # Native collection caching cannot bypass individual entries. Attachments + # and direct-room names have additional dependencies, so render those fresh. + if messages.all? { |message| !message.room.direct? && !message.attachment? && !message.body.body&.to_html.to_s.include?("(message) { message_fragment_cache_key(message) } + end + end + render partial: "messages/message", collection: messages, cached: cached end def message_fragment_cache_key(message) - return message unless controller.perform_caching && - %i[room creator rich_text_body boosts attachment_attachment].all? { |name| message.association(name).loaded? } - - room = message.room - body = message.body.body - return message if room.direct? || message.attachment? || !body || body.to_html.include?(" { last_page_of(PAGE_SIZE) } scope :first_page, -> { Page.load(ordered, :first, PAGE_SIZE) } diff --git a/app/models/record_cache.rb b/app/models/record_cache.rb index 1db362d13..80744d80c 100644 --- a/app/models/record_cache.rb +++ b/app/models/record_cache.rb @@ -2,22 +2,21 @@ # The observer must still see the captured epoch after lookup and admission. class RecordCache def self.fetch(key, version) - return yield unless version && ResponseCache.instance.budget.positive? && !FragmentCache.transaction_open? + cache = ResponseCache.instance + return yield unless version && cache.budget.positive? && !ActiveRecord::Base.connection_pool.with_connection(&:transaction_open?) - key = ActiveSupport::Cache.expand_cache_key([ "record-snapshot-v1", version, key ]) + key = ActiveSupport::Cache.expand_cache_key([ "record-snapshot-v1", key ]) return yield if key.bytesize > ResponseCache::MAX_KEY_BYTES - if snapshot = FragmentCache.store.read(key) - if ResponseCache.instance.version == version - return ActiveSupport::JSON.decode(snapshot).map { |name, attributes| name.constantize.instantiate(attributes) } - end + if entry = cache.read(key, version) + return ActiveSupport::JSON.decode(entry[:body]).map { |name, attributes| name.constantize.instantiate(attributes) } end records = yield - if records.all? { |record| record&.persisted? && !record.changed? } && ResponseCache.instance.version == version + if records.all? { |record| record&.persisted? && !record.changed? } # Raw database values retain timestamp precision and native enum binding. snapshot = ActiveSupport::JSON.encode(records.map { |record| [ record.class.name, record.attributes_before_type_cast ] }) - FragmentCache.store.write(key, snapshot) if snapshot.bytesize <= ResponseCache::MAX_ENTRY_BYTES + cache.write(key, version, { body: snapshot.freeze, headers: {}.freeze }.freeze) end records end diff --git a/app/models/response_cache.rb b/app/models/response_cache.rb index 200d16400..c1baf4298 100644 --- a/app/models/response_cache.rb +++ b/app/models/response_cache.rb @@ -6,21 +6,19 @@ class ResponseCache MAX_ENTRY_BYTES = 1.megabyte MAX_KEY_BYTES = 2.kilobytes + attr_reader :budget + def self.instance INSTANCE end - def initialize + def initialize(budget: [ ENV.fetch("CAMPFIRE_RESPONSE_CACHE_MB", "64").to_i, 0 ].max.megabytes) + @budget = budget + @store = ActiveSupport::Cache::MemoryStore.new(size: budget, coder: nil) @mutex = Mutex.new - @entries = {} - @bytes = 0 @render_locks = Array.new(16) { Mutex.new } end - def budget - [ ENV.fetch("CAMPFIRE_RESPONSE_CACHE_MB", "64").to_i, 0 ].max.megabytes - end - def version @mutex.synchronize { current_version } rescue SQLite3::Exception @@ -30,7 +28,10 @@ def version def read(key, version) @mutex.synchronize do - @entries[key]&.first if current_version == version + if current_version == version + entry = @store.read(key) + entry if current_version == version + end end rescue SQLite3::Exception clear @@ -49,14 +50,7 @@ def write(key, version, entry) @mutex.synchronize do return unless current_version == version - return if @entries.key?(key) - - while @entries.any? && @bytes + size > budget - _, (_, removed_size) = @entries.shift - @bytes -= removed_size - end - @entries[key] = [ entry, size ] - @bytes += size + @store.write(key, entry, unless_exist: true) end rescue SQLite3::Exception clear @@ -64,8 +58,7 @@ def write(key, version, entry) def clear @mutex.synchronize do - @entries.clear - @bytes = 0 + @store.clear @observer&.close @observer = @database = @version = nil end @@ -75,8 +68,7 @@ def clear def current_version database = File.expand_path(ActiveRecord::Base.connection_db_config.database) if @database != database || !@observer - @entries.clear - @bytes = 0 + @store.clear @observer&.close @observer = nil @observer = SQLite3::Database.new(database, readonly: true) @@ -87,8 +79,7 @@ def current_version version = @observer.get_first_value("PRAGMA data_version") if @version != version - @entries.clear - @bytes = 0 + @store.clear @version = version end [ @database, @namespace, @version ] diff --git a/app/views/messages/_actions.html.erb b/app/views/messages/_actions.html.erb index 8948783f3..d9e7bde5f 100644 --- a/app/views/messages/_actions.html.erb +++ b/app/views/messages/_actions.html.erb @@ -1,6 +1,6 @@ <%# Be sure to check/update messages/_template.html.erb when changing this file %> -<%= cache_message_actions(message) do %> +<% cache_if !message.attachment?, [ "message-actions-v2", message.id, message.room_id, url ] do %>
<%= tag.details class: "position-relative", data: { controller: "popup", action: "keydown.esc->popup#close toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb index a94a579c9..2b5cb7450 100644 --- a/app/views/messages/_message.html.erb +++ b/app/views/messages/_message.html.erb @@ -1,7 +1,6 @@ <%# Be sure to check/update messages/_template.html.erb when changing this file %> -<%# Bump this version when the message presentation filters change what they emit. Editing this line changes the template digest, which busts BOTH this fragment cache and the collection cache that keys on this partial's digest (helper Ruby changes alone don't). %> -<% cache [ message_fragment_cache_key(message), "presentation-v8" ] do %> +<%# Full-message HTML is cached once, by the collection renderer. Bump message_fragment_cache_key when presentation helpers change. %> <%= message_tag message do %>

<%= local_datetime_tag message.created_at, style: :date %>

@@ -31,4 +30,3 @@
<% end %> -<% end %> diff --git a/app/views/messages/_message.json.jbuilder b/app/views/messages/_message.json.jbuilder index e8abfa606..f692ec871 100644 --- a/app/views/messages/_message.json.jbuilder +++ b/app/views/messages/_message.json.jbuilder @@ -1,18 +1,16 @@ -json.cache! message do - json.(message, :id) +json.(message, :id) - json.created_at message.created_at.utc +json.created_at message.created_at.utc - json.body do - json.plain_text message.plain_text_body - json.html message.body.to_s - end - - json.creator message.creator, partial: "users/user", as: :user +json.body do + json.plain_text message.plain_text_body + json.html message.body.to_s +end - json.room do - json.id message.room_id - end +json.creator message.creator, partial: "users/user", as: :user - json.url room_message_url(message.room, message) +json.room do + json.id message.room_id end + +json.url room_message_url(message.room, message) diff --git a/app/views/messages/boosts/_boost.json.jbuilder b/app/views/messages/boosts/_boost.json.jbuilder index c3392b686..0d64b088a 100644 --- a/app/views/messages/boosts/_boost.json.jbuilder +++ b/app/views/messages/boosts/_boost.json.jbuilder @@ -1,12 +1,10 @@ -json.cache! boost do - json.(boost, :id, :content) +json.(boost, :id, :content) - json.created_at boost.created_at.utc +json.created_at boost.created_at.utc - json.booster boost.booster, partial: "users/user", as: :user +json.booster boost.booster, partial: "users/user", as: :user - json.message do - json.id boost.message_id - json.url room_message_url(boost.message.room, boost.message) - end +json.message do + json.id boost.message_id + json.url room_message_url(boost.message.room, boost.message) end diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index 6ec11a39a..456a51347 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -1 +1 @@ -<%= render partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> +<%= render_messages @messages %> diff --git a/app/views/rooms/refreshes/show.turbo_stream.erb b/app/views/rooms/refreshes/show.turbo_stream.erb index d2d485f1e..ed0b0796a 100644 --- a/app/views/rooms/refreshes/show.turbo_stream.erb +++ b/app/views/rooms/refreshes/show.turbo_stream.erb @@ -1,5 +1,5 @@ <%= turbo_stream.append dom_id(@room, :messages) do %> - <%= render partial: "messages/message", collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <%= render_messages @new_messages %> <% end if @new_messages.any? %> <% @updated_messages.each do |message| %> diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index 234aac0c5..a4142d2fb 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -14,7 +14,7 @@ <%= messages_tag(@room) do %> <%= render "rooms/show/invitation", room: @room %> - <%= render partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <%= render_messages @messages %> <% end %> <%= turbo_stream_from @room, :messages, channel: "RoomMessagesChannel" %> diff --git a/app/views/searches/index.html.erb b/app/views/searches/index.html.erb index 03aff66d3..8309582bd 100644 --- a/app/views/searches/index.html.erb +++ b/app/views/searches/index.html.erb @@ -54,7 +54,7 @@ <%= search_results_tag do %> - <%= render partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <%= render_messages @messages %> <% end %> diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder index 442530243..20cb812e9 100644 --- a/app/views/users/_user.json.jbuilder +++ b/app/views/users/_user.json.jbuilder @@ -1,5 +1,3 @@ -json.cache! user do - json.(user, :id, :name, :role) +json.(user, :id, :name, :role) - json.avatar_url fresh_user_avatar_url(user) -end +json.avatar_url fresh_user_avatar_url(user) diff --git a/app/views/users/sidebars/rooms/_direct.html.erb b/app/views/users/sidebars/rooms/_direct.html.erb index 7047f461a..abe7be9d9 100644 --- a/app/views/users/sidebars/rooms/_direct.html.erb +++ b/app/views/users/sidebars/rooms/_direct.html.erb @@ -1,5 +1,4 @@ -<% cache membership do %> - <% members = membership.room.users.without(membership.user).presence || [ membership.user ] %> + <% members = membership.room.users.reject { |user| user.id == membership.user_id }.presence || [ Current.user ] %> <%= link_to_room membership.room, class: [ "direct", "unread": membership.unread? ], id: dom_id(membership.room, :list), data: { sorted_list_number: membership.room.updated_at.to_fs(:epoch) } do %> @@ -28,4 +27,3 @@ <% end %> -<% end %> diff --git a/app/views/users/sidebars/show.html.erb b/app/views/users/sidebars/show.html.erb index 709c31010..a7d706549 100644 --- a/app/views/users/sidebars/show.html.erb +++ b/app/views/users/sidebars/show.html.erb @@ -20,7 +20,7 @@ <% end %>
- <%= render partial: "users/sidebars/rooms/direct", collection: @direct_memberships, as: :membership, cached: true %> + <%= render partial: "users/sidebars/rooms/direct", collection: @direct_memberships, as: :membership %>
diff --git a/bench/README.md b/bench/README.md index 820495245..98393a865 100644 --- a/bench/README.md +++ b/bench/README.md @@ -17,7 +17,8 @@ Use `--image` to override `campfire-reference:app`, and `--cpus` to override ser The rendering probe checks exact response bodies, selected headers and unread payloads, and records timing, queries and allocations with MemoryStore, frozen time and fixture-only -CSRF disabling. Unread fanout excludes adapter I/O. +CSRF disabling. Cold iterations clear all actual stores, including legacy private fragment +stores when comparing older revisions. Unread fanout excludes adapter I/O. The HTTP driver uses production Puma/Redis with one worker and five threads. Ruby threads each maintain a keep-alive connection, request uncompressed responses, and consume the diff --git a/bench/message_hot_paths.rb b/bench/message_hot_paths.rb index 4e5ba3704..286059ddc 100644 --- a/bench/message_hot_paths.rb +++ b/bench/message_hot_paths.rb @@ -38,7 +38,11 @@ 2.times { client.get path } times, counts, allocations, hashes, headers = [], [], [], [], [] Integer(ENV.fetch("BENCH_ITERATIONS", "20")).times do - Rails.cache.clear if cache == "cold" + if cache == "cold" + Rails.cache.clear + ResponseCache.instance.clear if defined?(ResponseCache) + FragmentCache.store.clear if defined?(FragmentCache) + end queries.clear start_allocations = GC.stat(:total_allocated_objects) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) diff --git a/config/initializers/fragment_cache.rb b/config/initializers/fragment_cache.rb deleted file mode 100644 index 5c2ae41a3..000000000 --- a/config/initializers/fragment_cache.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "jbuilder/jbuilder_template" - -# Jbuilder 2.14.1 uses Rails.cache directly; use the same bounded store as ERB -# while retaining its native keys and controller instrumentation. -module BoundedJbuilderFragments - private - def _read_fragment_cache(key, options = nil) - @context.controller.instrument_fragment_cache :read_fragment, key do - @context.controller.cache_store.read(key, options) - end - end - - def _write_fragment_cache(key, options = nil) - @context.controller.instrument_fragment_cache :write_fragment, key do - yield.tap { |value| @context.controller.cache_store.write(key, value, options) } - end - end -end - -JbuilderTemplate.prepend(BoundedJbuilderFragments) - -Rails.application.config.to_prepare do - ActionView::PartialRenderer.collection_cache = FragmentCache.store -end diff --git a/config/initializers/version.rb b/config/initializers/version.rb index 8fb85e36a..13ecaf06f 100644 --- a/config/initializers/version.rb +++ b/config/initializers/version.rb @@ -1,2 +1,9 @@ Rails.application.config.app_version = ENV["APP_VERSION"].presence || ENV["GIT_REVISION"].presence || "0" Rails.application.config.git_revision = ENV["GIT_REVISION"] + +# Official builds share fragments across workers. Unversioned builds start cold +# on boot rather than reuse HTML from a different helper/asset/signing deployment. +Rails.application.config.x.presentation_cache_version = [ + ENV["GIT_REVISION"].presence || SecureRandom.hex(16), + Digest::SHA256.hexdigest(Rails.application.secret_key_base) +] diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 2f8918927..32d4b40c7 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -169,7 +169,7 @@ docker pull ghcr.io/basecamp/once-campfire:latest Any pending database migrations run automatically when the container boots. -Production `Rails.cache` uses Solid Cache in its own SQLite database, `storage/db/production_cache.sqlite3`. Startup's normal `db:prepare` creates it from the checked-in cache schema. Existing Redis entries are not copied; the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. The bounded per-worker response and fragment caches remain in memory and are separate from `Rails.cache`. +Production `Rails.cache` uses Solid Cache in its own SQLite database, `storage/db/production_cache.sqlite3`. Startup's normal `db:prepare` creates it from the checked-in cache schema. Existing Redis entries are not copied; the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. Native view fragments use this shared store; complete responses and authorization snapshots share a separate, approximately 64 MiB in-memory budget per worker. Official images namespace presentation caches by `GIT_REVISION` and signing key. Set an immutable `GIT_REVISION` for custom builds to share fragments across workers; unversioned builds use a fresh boot namespace instead. See the [Solid Cache upgrade notes](upgrades/solid-cache.md) for backup and restore behavior. diff --git a/docs/upgrades/solid-cache.md b/docs/upgrades/solid-cache.md index 24a123195..daf9f1eef 100644 --- a/docs/upgrades/solid-cache.md +++ b/docs/upgrades/solid-cache.md @@ -4,7 +4,7 @@ Production `Rails.cache` moves from Redis to Solid Cache in a dedicated SQLite d ## Upgrade -Deploy the release normally. Existing Redis entries are not copied, so the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. Campfire's bounded per-worker response and fragment caches are separate in-memory stores and are unchanged. +Deploy the release normally. Existing Redis entries are not copied, so the cache starts cold and repopulates as requests arrive. Redis remains required for Action Cable. Native presentation fragments and rate limits use Solid Cache. Complete responses and authorization snapshots remain local, sharing one approximately 64 MiB Rails MemoryStore budget per worker. SQLite commits invalidate this local cache; native message fragments use content-derived keys rather than process-local generations. No application-data migration is needed for these cache changes. ## Backups and restores diff --git a/test/controllers/cached_responses_test.rb b/test/controllers/cached_responses_test.rb index 9168f3bf2..bd1fbda14 100644 --- a/test/controllers/cached_responses_test.rb +++ b/test/controllers/cached_responses_test.rb @@ -201,9 +201,11 @@ class CachedResponsesTest < ActionDispatch::IntegrationTest end get user_sidebar_url(:me) Users::SidebarsController.any_instance.unstub(:set_version_headers) - ResponseCache.instance.expects(:write).once get user_sidebar_url(:me) assert_includes response.body, "During authentication" + fresh = response.body + get user_sidebar_url(:me) + assert_equal fresh, response.body end private diff --git a/test/controllers/fragment_cache_test.rb b/test/controllers/fragment_cache_test.rb index 484bbf2a0..8760c2c9e 100644 --- a/test/controllers/fragment_cache_test.rb +++ b/test/controllers/fragment_cache_test.rb @@ -9,10 +9,15 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest @previous_caching = ActionController::Base.perform_caching @previous_message_caching = MessagesController.perform_caching @class_store = ApplicationController.cache_store + @message_store = MessagesController.cache_store + @collection_store = ActionView::PartialRenderer.collection_cache @global_store = Rails.cache ActionController::Base.perform_caching = true MessagesController.perform_caching = true - FragmentCache.store.clear + Rails.cache = ActiveSupport::Cache::MemoryStore.new + ApplicationController.cache_store = MessagesController.cache_store = Rails.cache + ActionView::PartialRenderer.collection_cache = Rails.cache + ResponseCache.instance.stubs(:budget).returns(0) ResponseCache.instance.clear @room = rooms(:watercooler) @message = @room.messages.ordered.last @@ -21,36 +26,42 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest teardown do ActionController::Base.perform_caching = @previous_caching MessagesController.perform_caching = @previous_message_caching - FragmentCache.store.clear + Rails.cache = @global_store + ApplicationController.cache_store = @class_store + MessagesController.cache_store = @message_store + ActionView::PartialRenderer.collection_cache = @collection_store ResponseCache.instance.clear end - test "native collection caching uses the bounded store without replacing shared stores" do - assert_same FragmentCache.store, ApplicationController.new.cache_store - assert_same FragmentCache.store, ActionView::PartialRenderer.collection_cache - assert_same @class_store, ApplicationController.cache_store - assert_same @global_store, Rails.cache - ResponseCache.instance.stubs(:budget).returns(0) + test "native collection fragments are shared between different authenticated viewers" do get room_messages_url(@room) assert_response :success + first = response.body + sign_in :jason hits = [] ActiveSupport::Notifications.subscribed(->(event) { hits.concat(event.payload[:hits]) }, "cache_read_multi.active_support") do get room_messages_url(@room) end assert_response :success assert_not_empty hits + assert_equal first, response.body token = users(:david).sessions.order(:id).last.token assert_not_includes hits.join, token end - test "disabled page caching retains fresh native fragments after foreign leaf writes" do - ResponseCache.instance.stubs(:budget).returns(0) + test "native fragments remain fresh after foreign leaf writes" do get room_messages_url(@room) assert_response :success foreign_write("UPDATE action_text_rich_texts SET body = ? WHERE record_type = 'Message' AND record_id = ?", "foreign disabled body", @message.id) get room_messages_url(@room) assert_response :success assert_includes response.body, "foreign disabled body" + foreign_write("UPDATE rooms SET name = ? WHERE id = ?", "Fresh room label", @room.id) + foreign_write("UPDATE users SET name = ? WHERE id = ?", "Fresh booster", users(:bender).id) + get room_messages_url(@room) + assert_response :success + assert_includes response.body, "Fresh room label" + assert_select "##{dom_id(boosts(:fourth_by_bender))} a[title^=?]", "Fresh booster" end test "conditional pagination invalidates warmed native fragments without timestamp changes" do @@ -75,7 +86,6 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest end test "a commit in another room retains content-validated message collection hits" do - ResponseCache.instance.stubs(:budget).returns(0) get room_messages_url(@room) assert_response :success foreign_write("UPDATE rooms SET name = ? WHERE id = ?", "Unrelated room rename", rooms(:pets).id) @@ -89,8 +99,7 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest assert_equal @room.messages.count, messages[:cache_hits] end - test "a foreign commit after capture bypasses old native fragment lookups" do - ResponseCache.instance.stubs(:budget).returns(0) + test "a foreign commit after authentication renders current fragment dependencies" do get room_messages_url(@room) assert_response :success MessagesController.any_instance.stubs(:set_version_headers).with do @@ -117,18 +126,14 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest assert_includes response.body, "Foreign refreshed boost" end - test "Jbuilder fragments use the bounded store and do not create HTML token state" do + test "bot JSON reads current leaf rows and URLs without creating HTML token state" do api = open_session { |session| session.host! "once.campfire.test" } path = room_bot_messages_path(@room, users(:bender).bot_key) api.get path assert_equal 200, api.response.status assert_not api.cookies["_campfire_session"] - hits = [] - ActiveSupport::Notifications.subscribed(->(event) { hits << event.payload[:key] if event.payload[:hit] }, "cache_read.active_support") do - api.get path - end + api.get path assert_equal 200, api.response.status - assert_not_empty hits foreign_write("UPDATE action_text_rich_texts SET body = ? WHERE record_type = 'Message' AND record_id = ?", "foreign JSON body", @message.id) foreign_write("UPDATE users SET name = ? WHERE id = ?", "Foreign JSON creator", @message.creator_id) api.get path @@ -139,16 +144,12 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest api.get path json = JSON.parse(api.response.body).find { |message| message["id"] == @message.id } assert_includes json["url"], "another.campfire.test:8081" - assert_same @global_store, Rails.cache + assert_not api.cookies["_campfire_session"] end test "detached broadcasts bypass fragments without a pre-render model snapshot" do get room_messages_url(@room) foreign_write("UPDATE action_text_rich_texts SET body = ? WHERE record_type = 'Message' AND record_id = ?", "foreign broadcast body", @message.id) - FragmentCache.store.expects(:read).never - FragmentCache.store.expects(:write).never - FragmentCache.store.expects(:read_multi).never - FragmentCache.store.expects(:write_multi).never @message.reload.broadcast_create assert_rendered_turbo_stream_broadcast @room, :messages, action: "append", target: [ @room, :messages ] do |stream| assert_includes stream.to_html, "foreign broadcast body" @@ -160,6 +161,70 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest assert_not ApplicationController.new.perform_caching end + test "presentation deployment changes invalidate shared fragments" do + get room_messages_url(@room) + previous_version = Rails.configuration.x.presentation_cache_version + Rails.configuration.x.presentation_cache_version = "new-presentation" + collections = [] + ActiveSupport::Notifications.subscribed(->(event) { collections << event.payload }, "render_collection.action_view") do + get room_messages_url(@room) + end + assert_response :success + rendered = collections.find { |payload| payload[:identifier].end_with?("messages/_message.html.erb") } + assert_equal 0, rendered[:cache_hits] + ensure + Rails.configuration.x.presentation_cache_version = previous_version + end + + test "shared fragments separate URL origins" do + get room_messages_url(@room) + host! "another.campfire.test:8081" + post session_path, params: { email_address: users(:david).email_address, password: "secret123456" } + assert_response :redirect + get room_messages_path(@room) + assert_response :success + assert_includes response.body, "http://another.campfire.test:8081/rooms/" + assert_not_includes response.body, "http://once.campfire.test/rooms/" + end + + test "shared fragments separate mounted URL prefixes" do + get room_messages_url(@room) + get room_messages_path(@room), env: { "SCRIPT_NAME" => "/campfire" } + assert_response :success + assert_includes response.body, "http://once.campfire.test/campfire/rooms/" + assert_select "form[action=?]", "/campfire/messages/#{@message.id}/boosts" + end + + test "direct sidebar participants stay fresh without membership timestamp changes" do + get user_sidebar_url(:me) + foreign_write("UPDATE users SET name = ? WHERE id = ?", "Renamed Jason", users(:jason).id) + get user_sidebar_url(:me) + assert_response :success + assert_select "##{dom_id(rooms(:david_and_jason), :list)}", text: /Renamed/ + end + + test "mixed attachment collections read current blob metadata" do + message = @room.messages.create! creator: users(:david), attachment: fixture_file_upload("moon.jpg", "image/jpeg") + get room_messages_url(@room) + assert_response :success + foreign_write("UPDATE active_storage_blobs SET filename = ? WHERE id = ?", "renamed-moon.jpg", message.attachment.blob.id) + get room_messages_url(@room) + assert_response :success + assert_select "##{dom_id(message)}", text: /renamed-moon.jpg/ + end + + test "renders inside primary transactions do not populate shared fragments" do + writes = [] + ActiveRecord::Base.transaction do + ActiveSupport::Notifications.subscribed(->(event) { writes << event.payload[:key] }, /cache_write.*\.active_support/) do + get room_messages_url(@room) + end + assert_response :success + assert_empty writes + raise ActiveRecord::Rollback + end + end + private def foreign_write(sql, *bindings) SQLite3::Database.new(ActiveRecord::Base.connection_db_config.database) do |database| diff --git a/test/helpers/messages_helper_test.rb b/test/helpers/messages_helper_test.rb index 83902f550..e652b8d75 100644 --- a/test/helpers/messages_helper_test.rb +++ b/test/helpers/messages_helper_test.rb @@ -3,14 +3,15 @@ class MessagesHelperTest < ActionView::TestCase test "plain text presentation is reused by content rather than database epoch" do view.controller.stubs(:perform_caching).returns(true) - FragmentCache.store.clear + previous_cache = Rails.cache + Rails.cache = ActiveSupport::Cache::MemoryStore.new message = Message.create! room: rooms(:pets), body: "A reusable safe body", creator: users(:jason) first = view.message_presentation(message) assert_includes first, "A reusable safe body" ContentFilters::TextMessagePresentationFilters.expects(:apply).never assert_equal first, view.message_presentation(message) ensure - FragmentCache.store.clear + Rails.cache = previous_cache end test "message_presentation neutralizes unsafe URI schemes in links" do diff --git a/test/models/record_cache_test.rb b/test/models/record_cache_test.rb index 5ee99befd..221bdd3fe 100644 --- a/test/models/record_cache_test.rb +++ b/test/models/record_cache_test.rb @@ -11,10 +11,8 @@ class RecordCacheTest < ActiveSupport::TestCase end ActiveRecord::Base.stubs(:connection_db_config).returns(Struct.new(:database).new(@database)) ActiveRecord::Base.connection.stubs(:transaction_open?).returns(false) - @cache = ResponseCache.new - @cache.stubs(:budget).returns(4096) + @cache = ResponseCache.new(budget: 4096) ResponseCache.stubs(:instance).returns(@cache) - FragmentCache.stubs(:store).returns(ActiveSupport::Cache::MemoryStore.new(size: 64.kilobytes)) @record = User.instantiate(users(:david).attributes_before_type_cast.merge( "created_at" => "2026-10-08 12:34:56.123456", "updated_at" => "2026-10-08 12:34:57.654321", "status" => 2, "role" => 1)) @@ -58,19 +56,15 @@ class RecordCacheTest < ActiveSupport::TestCase test "observer is rechecked after a snapshot lookup" do version = @cache.version RecordCache.fetch("user", version) { [ @record ] } - store = FragmentCache.store - original_read = store.method(:read) - commit = method(:foreign_commit) - store.define_singleton_method(:read) do |*arguments| - original_read.call(*arguments).tap { |snapshot| commit.call if snapshot } - end fresh = User.instantiate(@record.attributes_before_type_cast.merge("name" => "Committed during lookup")) - assert_same fresh, RecordCache.fetch("user", version) { [ fresh ] }.first + subscriber = ->(event) { foreign_commit if event.payload[:hit] } + ActiveSupport::Notifications.subscribed(subscriber, "cache_read.active_support") do + assert_same fresh, RecordCache.fetch("user", version) { [ fresh ] }.first + end end test "a commit while loading records prevents admission under the old epoch" do version = @cache.version - FragmentCache.store.expects(:write).never assert_same @record, RecordCache.fetch("user", version) { foreign_commit; [ @record ] }.first replacement = User.instantiate(@record.attributes_before_type_cast.merge("name" => "Fresh load")) assert_same replacement, RecordCache.fetch("user", version) { [ replacement ] }.first @@ -79,7 +73,6 @@ class RecordCacheTest < ActiveSupport::TestCase test "disabled budget missing epoch and open transactions always query and never admit" do version = @cache.version RecordCache.fetch("user", version) { [ @record ] } - FragmentCache.store.expects(:write).never @cache.stubs(:budget).returns(0) assert_same @record, RecordCache.fetch("user", version) { [ @record ] }.first @cache.stubs(:budget).returns(4096) @@ -89,6 +82,15 @@ class RecordCacheTest < ActiveSupport::TestCase assert_same @record, RecordCache.fetch("transaction-only", version) { [ @record ] }.first end + test "page pressure evicts snapshots within the same local budget" do + version = @cache.version + RecordCache.fetch("user", version) { [ @record ] } + 6.times { |index| @cache.write("page-#{index}", version, { body: "x" * 800, headers: {} }) } + fresh = User.instantiate(@record.attributes_before_type_cast.merge("name" => "Reloaded after eviction")) + assert_same fresh, RecordCache.fetch("user", version) { [ fresh ] }.first + assert_equal fresh.name, RecordCache.fetch("user", version) { flunk "fresh snapshot missing" }.first.name + end + private def foreign_commit SQLite3::Database.new(@database) { |database| database.execute("INSERT INTO changes_for_test VALUES ('committed')") } diff --git a/test/models/response_cache_test.rb b/test/models/response_cache_test.rb index aeba88124..57c125dd6 100644 --- a/test/models/response_cache_test.rb +++ b/test/models/response_cache_test.rb @@ -10,8 +10,7 @@ class ResponseCacheTest < ActiveSupport::TestCase database.execute("CREATE TABLE values_for_test (value TEXT)") end ActiveRecord::Base.stubs(:connection_db_config).returns(Struct.new(:database).new(@database)) - @cache = ResponseCache.new - @cache.stubs(:budget).returns(1024) + @cache = ResponseCache.new(budget: 1024) end teardown do @@ -31,9 +30,10 @@ class ResponseCacheTest < ActiveSupport::TestCase assert_equal "fresh", @cache.read("page", @cache.version)[:body] end - test "byte budget evicts oldest and bypasses oversized entries and keys" do + test "byte budget evicts entries and rejects oversized entries without flushing useful pages" do + @cache.clear + @cache = ResponseCache.new(budget: 512) version = @cache.version - @cache.stubs(:budget).returns(512) @cache.write("first", version, entry("a" * 100)) @cache.write("second", version, entry("b" * 100)) assert_nil @cache.read("first", version) @@ -42,6 +42,7 @@ class ResponseCacheTest < ActiveSupport::TestCase assert_nil @cache.read("oversized", version) @cache.write("k" * 2049, version, entry("small")) assert_nil @cache.read("k" * 2049, version) + assert_equal "b" * 100, @cache.read("second", version)[:body] end test "clearing the observer gives later pages a distinct namespace" do @@ -83,6 +84,6 @@ class ResponseCacheTest < ActiveSupport::TestCase private def entry(body) - { body: body, marker: "unexposed-marker", headers: {} } + { body: body.freeze, headers: {}.freeze }.freeze end end From d10c5a5307668fef61a4f583b3a11bc35eb96fdf Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 12:50:50 +0000 Subject: [PATCH 10/13] Cache message runs around attachments and mentions One attachment, mention or other embedded attachment on a page bypassed the collection cache for all of its messages. Mentions are common, so many real pages rendered entirely uncached. Render consecutive runs instead: cacheable runs keep their content-keyed hits, and only the messages with further dependencies render fresh. Local production-mode probe, 60 requests with an unrelated commit before each read, p50 over two runs: an 80-message room with a mention in every eighth message 119 -> 73 ms; a search matching those messages 257 -> 129 ms; a room without mentions unchanged (50 -> 49 ms). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01N8uPpsqMRJrp8jqATFJW45 --- app/helpers/messages_helper.rb | 24 ++++++++++++++---------- test/controllers/fragment_cache_test.rb | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index 5f7623a42..9a93649f7 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -76,16 +76,16 @@ def message_presentation(message) end def render_messages(messages) - cached = false - if controller.perform_caching - messages.preload_associations(messages) if messages.respond_to?(:preload_associations) - # Native collection caching cannot bypass individual entries. Attachments - # and direct-room names have additional dependencies, so render those fresh. - if messages.all? { |message| !message.room.direct? && !message.attachment? && !message.body.body&.to_html.to_s.include?("(message) { message_fragment_cache_key(message) } - end - end - render partial: "messages/message", collection: messages, cached: cached + return render(partial: "messages/message", collection: messages) unless controller.perform_caching + + messages.preload_associations(messages) if messages.respond_to?(:preload_associations) + # Native collection caching cannot bypass individual entries, so render + # consecutive runs: attachments, mentions and direct-room names have further + # dependencies and render fresh without costing their neighbours a cache hit. + cached = ->(message) { message_fragment_cache_key(message) } + safe_join messages.chunk_while { |a, b| message_fragment_cacheable?(a) == message_fragment_cacheable?(b) }.map { |run| + render partial: "messages/message", collection: run, cached: (cached if message_fragment_cacheable?(run.first)) + } end def message_fragment_cache_key(message) @@ -98,6 +98,10 @@ def message_fragment_cache_key(message) end private + def message_fragment_cacheable?(message) + !message.room.direct? && !message.attachment? && !message.body.body&.to_html.to_s.include?(" { auto_link_text h(ContentFilters::TextMessagePresentationFilters.apply(body)) } diff --git a/test/controllers/fragment_cache_test.rb b/test/controllers/fragment_cache_test.rb index 8760c2c9e..bd6bd0002 100644 --- a/test/controllers/fragment_cache_test.rb +++ b/test/controllers/fragment_cache_test.rb @@ -213,6 +213,22 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest assert_select "##{dom_id(message)}", text: /renamed-moon.jpg/ end + test "an attachment on the page keeps its neighbours cached" do + message = @room.messages.create! creator: users(:david), attachment: fixture_file_upload("moon.jpg", "image/jpeg") + get room_messages_url(@room) + assert_response :success + first = response.body + + hits = [] + ActiveSupport::Notifications.subscribed(->(event) { hits.concat(event.payload[:hits]) }, "cache_read_multi.active_support") do + get room_messages_url(@room) + end + assert_response :success + assert_equal first, response.body + assert_equal @room.messages.count - 1, hits.size + assert_select "##{dom_id(message)}", text: /moon.jpg/ + end + test "renders inside primary transactions do not populate shared fragments" do writes = [] ActiveRecord::Base.transaction do From eb053851e0d8ce31ba053df019a3bfabe9982c32 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 14:09:03 +0000 Subject: [PATCH 11/13] Speed up cold message renders Serialize each message body once per render: the fragment key, the cacheability check and the presentation each walked the rich text document. Collection-cached messages no longer also write the text presentation and message controls as separate Solid Cache entries; those nested fragments remain for messages rendered fresh, such as direct rooms and mentions. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01N8uPpsqMRJrp8jqATFJW45 --- app/helpers/messages_helper.rb | 22 ++++++++++++++++------ app/views/messages/_actions.html.erb | 3 ++- test/helpers/messages_helper_test.rb | 15 +++++++++++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index 9a93649f7..ebdf9628b 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -66,7 +66,7 @@ def message_presentation(message) when "sound" message_sound_presentation(message) else - text_message_presentation(message.body.body) + text_message_presentation(message) end rescue Exception => e Sentry.capture_exception(e, extra: { message: message }) @@ -92,23 +92,33 @@ def message_fragment_cache_key(message) # Read actual presentation inputs, not just updated_at: SQL writers can edit # leaf rows without touching their parents. Unrelated commits retain reuse. dependencies = [ message.attributes, message.room.attributes.slice("id", "type", "name"), - message.creator.attributes.slice("id", "name", "bio", "updated_at"), message.body.body&.to_html, + message.creator.attributes.slice("id", "name", "bio", "updated_at"), message_body_html(message), message.boosts.sort_by(&:created_at).map { |boost| [ boost.attributes, boost.booster.attributes.slice("id", "name", "bio", "updated_at") ] } ] [ "message-presentation-v9", Digest::SHA256.hexdigest(ActiveSupport::JSON.encode(dependencies)) ] end private def message_fragment_cacheable?(message) - !message.room.direct? && !message.attachment? && !message.body.body&.to_html.to_s.include?(" { auto_link_text h(ContentFilters::TextMessagePresentationFilters.apply(body)) } + # Collection-cached messages already store this inside their fragment, so + # only messages rendered fresh, such as in direct rooms, cache it separately. # Embedded attachments render database-backed metadata and signed URLs. # Plain text HTML depends only on its content, even after a foreign edit. - html = body.to_html - if controller.perform_caching && !html.include?(" -<% cache_if !message.attachment?, [ "message-actions-v2", message.id, message.room_id, url ] do %> +<%# Collection-cached messages already include these controls in their fragment. %> +<% cache_if !message.attachment? && !message_fragment_cacheable?(message), [ "message-actions-v2", message.id, message.room_id, url ] do %>
<%= tag.details class: "position-relative", data: { controller: "popup", action: "keydown.esc->popup#close toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> diff --git a/test/helpers/messages_helper_test.rb b/test/helpers/messages_helper_test.rb index e652b8d75..b52c26b7d 100644 --- a/test/helpers/messages_helper_test.rb +++ b/test/helpers/messages_helper_test.rb @@ -1,11 +1,11 @@ require "test_helper" class MessagesHelperTest < ActionView::TestCase - test "plain text presentation is reused by content rather than database epoch" do + test "plain text presentation in direct rooms is reused by content rather than database epoch" do view.controller.stubs(:perform_caching).returns(true) previous_cache = Rails.cache Rails.cache = ActiveSupport::Cache::MemoryStore.new - message = Message.create! room: rooms(:pets), body: "A reusable safe body", creator: users(:jason) + message = Message.create! room: rooms(:david_and_jason), body: "A reusable safe body", creator: users(:jason) first = view.message_presentation(message) assert_includes first, "A reusable safe body" ContentFilters::TextMessagePresentationFilters.expects(:apply).never @@ -14,6 +14,17 @@ class MessagesHelperTest < ActionView::TestCase Rails.cache = previous_cache end + test "collection-cached messages do not cache their text presentation separately" do + view.controller.stubs(:perform_caching).returns(true) + previous_cache = Rails.cache + Rails.cache = ActiveSupport::Cache::MemoryStore.new + message = Message.create! room: rooms(:pets), body: "Cached with the whole message", creator: users(:jason) + Rails.cache.expects(:fetch).never + assert_includes view.message_presentation(message), "Cached with the whole message" + ensure + Rails.cache = previous_cache + end + test "message_presentation neutralizes unsafe URI schemes in links" do message = Message.create! room: rooms(:pets), body: '', client_message_id: "0015", creator: users(:jason) From 7d356f4ab0799fb5328b289e824cce37a60e2785 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 16:53:58 +0000 Subject: [PATCH 12/13] Pass fragment caching to message partials as a local Replace the helper's instance-variable memo with an explicit fragment_cached local: render_messages decides cacheability once per message and the partials read it, so no view state lives in helper instance variables. Ahead-of-time compilers such as Roundhouse lower partial locals but treat view instance variables as controller state. Cold renders stay as fast as with the memo. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01N8uPpsqMRJrp8jqATFJW45 --- app/helpers/messages_helper.rb | 30 ++++++++++------------- app/views/messages/_actions.html.erb | 2 +- app/views/messages/_message.html.erb | 4 +-- app/views/messages/_presentation.html.erb | 2 +- test/helpers/messages_helper_test.rb | 4 +-- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index ebdf9628b..1c915fa34 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -59,14 +59,14 @@ def message_timestamp(message, **attributes) local_datetime_tag message.created_at, **attributes end - def message_presentation(message) + def message_presentation(message, fragment_cached: false) case message.content_type when "attachment" message_attachment_presentation(message) when "sound" message_sound_presentation(message) else - text_message_presentation(message) + text_message_presentation(message.body.body, fragment_cached: fragment_cached) end rescue Exception => e Sentry.capture_exception(e, extra: { message: message }) @@ -83,8 +83,10 @@ def render_messages(messages) # consecutive runs: attachments, mentions and direct-room names have further # dependencies and render fresh without costing their neighbours a cache hit. cached = ->(message) { message_fragment_cache_key(message) } - safe_join messages.chunk_while { |a, b| message_fragment_cacheable?(a) == message_fragment_cacheable?(b) }.map { |run| - render partial: "messages/message", collection: run, cached: (cached if message_fragment_cacheable?(run.first)) + runs = messages.map { |message| [ message, message_fragment_cacheable?(message) ] }.chunk_while { |a, b| a.last == b.last } + safe_join runs.map { |run| + fragment_cached = run.first.last + render partial: "messages/message", collection: run.map(&:first), cached: (cached if fragment_cached), locals: { fragment_cached: fragment_cached } } end @@ -92,33 +94,27 @@ def message_fragment_cache_key(message) # Read actual presentation inputs, not just updated_at: SQL writers can edit # leaf rows without touching their parents. Unrelated commits retain reuse. dependencies = [ message.attributes, message.room.attributes.slice("id", "type", "name"), - message.creator.attributes.slice("id", "name", "bio", "updated_at"), message_body_html(message), + message.creator.attributes.slice("id", "name", "bio", "updated_at"), message.body.body&.to_html, message.boosts.sort_by(&:created_at).map { |boost| [ boost.attributes, boost.booster.attributes.slice("id", "name", "bio", "updated_at") ] } ] [ "message-presentation-v9", Digest::SHA256.hexdigest(ActiveSupport::JSON.encode(dependencies)) ] end private def message_fragment_cacheable?(message) - !message.room.direct? && !message.attachment? && !message_body_html(message).to_s.include?(" { auto_link_text h(ContentFilters::TextMessagePresentationFilters.apply(body)) } # Collection-cached messages already store this inside their fragment, so # only messages rendered fresh, such as in direct rooms, cache it separately. + return render.call if fragment_cached || !controller.perform_caching + # Embedded attachments render database-backed metadata and signed URLs. # Plain text HTML depends only on its content, even after a foreign edit. - html = message_body_html(message).to_s - if controller.perform_caching && !message_fragment_cacheable?(message) && !html.include?(" <%# Collection-cached messages already include these controls in their fragment. %> -<% cache_if !message.attachment? && !message_fragment_cacheable?(message), [ "message-actions-v2", message.id, message.room_id, url ] do %> +<% cache_if !message.attachment? && !local_assigns[:fragment_cached], [ "message-actions-v2", message.id, message.room_id, url ] do %>
<%= tag.details class: "position-relative", data: { controller: "popup", action: "keydown.esc->popup#close toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb index 2b5cb7450..6dfef678f 100644 --- a/app/views/messages/_message.html.erb +++ b/app/views/messages/_message.html.erb @@ -22,9 +22,9 @@ <%= link_to room_display_name(message.room, for_user: nil), room_at_message_path(message.room, message), target: "_top", data: { reply_target: "link" } %> - <%= render "messages/actions", message: message, url: room_at_message_url(message.room, message) %> + <%= render "messages/actions", message: message, url: room_at_message_url(message.room, message), fragment_cached: local_assigns[:fragment_cached] %>
- <%= render "messages/presentation", message: message %> + <%= render "messages/presentation", message: message, fragment_cached: local_assigns[:fragment_cached] %> <%= render "messages/boosts/boosts", message: message %>
diff --git a/app/views/messages/_presentation.html.erb b/app/views/messages/_presentation.html.erb index b24b9ea97..473c6bbaa 100644 --- a/app/views/messages/_presentation.html.erb +++ b/app/views/messages/_presentation.html.erb @@ -1,3 +1,3 @@
- <%= message_presentation(message) %> + <%= message_presentation(message, fragment_cached: local_assigns[:fragment_cached]) %>
diff --git a/test/helpers/messages_helper_test.rb b/test/helpers/messages_helper_test.rb index b52c26b7d..4838a4451 100644 --- a/test/helpers/messages_helper_test.rb +++ b/test/helpers/messages_helper_test.rb @@ -1,7 +1,7 @@ require "test_helper" class MessagesHelperTest < ActionView::TestCase - test "plain text presentation in direct rooms is reused by content rather than database epoch" do + test "plain text presentation rendered outside a cached fragment is reused by content rather than database epoch" do view.controller.stubs(:perform_caching).returns(true) previous_cache = Rails.cache Rails.cache = ActiveSupport::Cache::MemoryStore.new @@ -20,7 +20,7 @@ class MessagesHelperTest < ActionView::TestCase Rails.cache = ActiveSupport::Cache::MemoryStore.new message = Message.create! room: rooms(:pets), body: "Cached with the whole message", creator: users(:jason) Rails.cache.expects(:fetch).never - assert_includes view.message_presentation(message), "Cached with the whole message" + assert_includes view.message_presentation(message, fragment_cached: true), "Cached with the whole message" ensure Rails.cache = previous_cache end From 49e1b2d588cc4bfdf155fd9a1446e83db8640c4c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 17:01:09 +0000 Subject: [PATCH 13/13] Render message runs from views so they compile ahead of time Roundhouse lowers a collection render only where it appears in a template, and drops a statement-form `cache_if` block. Built through Roundhouse, the helper-side `render_messages` emitted a single nil message instead of the page, and `_actions` emitted nothing at all. Message pages now iterate `message_runs` in the view and render each run with the same collection cache and `fragment_cached` local. The action controls cache through `cache_message_actions`, an output helper with a block, under the same presentation namespace as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01BMME5CgsJpvz4dB74xsCEs --- app/helpers/messages_helper.rb | 33 ++++++++++++------- app/views/messages/_actions.html.erb | 3 +- app/views/messages/index.html.erb | 5 ++- .../rooms/refreshes/show.turbo_stream.erb | 5 ++- app/views/rooms/show.html.erb | 5 ++- app/views/searches/index.html.erb | 5 ++- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index 1c915fa34..b798c0f71 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -75,19 +75,30 @@ def message_presentation(message, fragment_cached: false) "" end - def render_messages(messages) - return render(partial: "messages/message", collection: messages) unless controller.perform_caching + # Native collection caching cannot bypass individual entries, so pages render + # in consecutive runs: attachments, mentions and direct-room names have further + # dependencies and render fresh without costing their neighbours a cache hit. + def message_runs(messages) + return [ messages ] unless controller.perform_caching messages.preload_associations(messages) if messages.respond_to?(:preload_associations) - # Native collection caching cannot bypass individual entries, so render - # consecutive runs: attachments, mentions and direct-room names have further - # dependencies and render fresh without costing their neighbours a cache hit. - cached = ->(message) { message_fragment_cache_key(message) } - runs = messages.map { |message| [ message, message_fragment_cacheable?(message) ] }.chunk_while { |a, b| a.last == b.last } - safe_join runs.map { |run| - fragment_cached = run.first.last - render partial: "messages/message", collection: run.map(&:first), cached: (cached if fragment_cached), locals: { fragment_cached: fragment_cached } - } + messages.map { |message| [ message, message_fragment_cacheable?(message) ] } + .chunk_while { |a, b| a.last == b.last } + .map { |run| run.map(&:first) } + end + + def message_run_fragment_cached?(run) + controller.perform_caching && message_fragment_cacheable?(run.first) + end + + # These controls contain URLs and static markup, but no viewer or token state. + # Collection-cached messages already include them in their fragment. + def cache_message_actions(message, url, fragment_cached: false, &block) + if controller.perform_caching && !fragment_cached && !message.attachment? + Rails.cache.fetch([ "message-actions-v3", Rails.configuration.x.presentation_cache_version, I18n.locale, message.id, message.room_id, url ]) { capture(&block) } + else + capture(&block) + end end def message_fragment_cache_key(message) diff --git a/app/views/messages/_actions.html.erb b/app/views/messages/_actions.html.erb index 25084836f..fbd6ed30f 100644 --- a/app/views/messages/_actions.html.erb +++ b/app/views/messages/_actions.html.erb @@ -1,7 +1,6 @@ <%# Be sure to check/update messages/_template.html.erb when changing this file %> -<%# Collection-cached messages already include these controls in their fragment. %> -<% cache_if !message.attachment? && !local_assigns[:fragment_cached], [ "message-actions-v2", message.id, message.room_id, url ] do %> +<%= cache_message_actions(message, url, fragment_cached: local_assigns[:fragment_cached]) do %>
<%= tag.details class: "position-relative", data: { controller: "popup", action: "keydown.esc->popup#close toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index 456a51347..0554ee564 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -1 +1,4 @@ -<%= render_messages @messages %> +<% message_runs(@messages).each do |run| %> + <% fragment_cached = message_run_fragment_cached?(run) %> + <%= render partial: "messages/message", collection: run, cached: (->(message) { message_fragment_cache_key(message) } if fragment_cached), locals: { fragment_cached: fragment_cached } %> +<% end %> diff --git a/app/views/rooms/refreshes/show.turbo_stream.erb b/app/views/rooms/refreshes/show.turbo_stream.erb index ed0b0796a..189c07660 100644 --- a/app/views/rooms/refreshes/show.turbo_stream.erb +++ b/app/views/rooms/refreshes/show.turbo_stream.erb @@ -1,5 +1,8 @@ <%= turbo_stream.append dom_id(@room, :messages) do %> - <%= render_messages @new_messages %> + <% message_runs(@new_messages).each do |run| %> + <% fragment_cached = message_run_fragment_cached?(run) %> + <%= render partial: "messages/message", collection: run, cached: (->(message) { message_fragment_cache_key(message) } if fragment_cached), locals: { fragment_cached: fragment_cached } %> + <% end %> <% end if @new_messages.any? %> <% @updated_messages.each do |message| %> diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index a4142d2fb..4fdb78430 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -14,7 +14,10 @@ <%= messages_tag(@room) do %> <%= render "rooms/show/invitation", room: @room %> - <%= render_messages @messages %> + <% message_runs(@messages).each do |run| %> + <% fragment_cached = message_run_fragment_cached?(run) %> + <%= render partial: "messages/message", collection: run, cached: (->(message) { message_fragment_cache_key(message) } if fragment_cached), locals: { fragment_cached: fragment_cached } %> + <% end %> <% end %> <%= turbo_stream_from @room, :messages, channel: "RoomMessagesChannel" %> diff --git a/app/views/searches/index.html.erb b/app/views/searches/index.html.erb index 8309582bd..f89d74c1d 100644 --- a/app/views/searches/index.html.erb +++ b/app/views/searches/index.html.erb @@ -54,7 +54,10 @@
<%= search_results_tag do %> - <%= render_messages @messages %> + <% message_runs(@messages).each do |run| %> + <% fragment_cached = message_run_fragment_cached?(run) %> + <%= render partial: "messages/message", collection: run, cached: (->(message) { message_fragment_cache_key(message) } if fragment_cached), locals: { fragment_cached: fragment_cached } %> + <% end %> <% end %>