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 b6f95d652..8d8e1c202 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/bench/compare_http.rb b/bench/compare_http.rb index 519b016e8..51595d0a5 100644 --- a/bench/compare_http.rb +++ b/bench/compare_http.rb @@ -46,7 +46,7 @@ command.concat environment(RAILS_ENV: "production", SECRET_KEY_BASE: "isolated-benchmark-fixture-key", DISABLE_SSL: true, SKIP_TELEMETRY: true, RAILS_LOG_LEVEL: "fatal", WEB_CONCURRENCY: 1, JOB_CONCURRENCY: 1, RAILS_MAX_THREADS: 5, REDIS_URL: "redis://#{redis}:6379/0") - command.concat [ options[:image], "bundle", "exec", "puma", "-C", "config/puma.rb" ] + command.concat [ options[:image], "sh", "-c", "bin/rails db:prepare && exec bundle exec puma -C config/puma.rb" ] run(*command) deadline = clock + 45 until client.ready? diff --git a/config/cache.yml b/config/cache.yml new file mode 100644 index 000000000..654816ffc --- /dev/null +++ b/config/cache.yml @@ -0,0 +1,18 @@ +default: &default + store_options: + max_size: <%= 256.megabytes %> + namespace: <%= Rails.env %> + +development: + <<: *default + +test: + <<: *default + +performance: + database: cache + <<: *default + +production: + database: cache + <<: *default diff --git a/config/database.yml b/config/database.yml index 845eb70f2..7b56910f0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -32,8 +32,21 @@ performance: primary: <<: *default database: storage/db/performance.sqlite3 + cache: + <<: *default + database: storage/db/performance_cache.sqlite3 + migrations_paths: db/cache_migrate + pragmas: + wal_autocheckpoint: 1000 production: primary: <<: *default database: storage/db/production.sqlite3 + cache: + <<: *default + database: storage/db/production_cache.sqlite3 + migrations_paths: db/cache_migrate + # The checkpoint contender only checkpoints the primary database. + pragmas: + wal_autocheckpoint: 1000 diff --git a/config/environments/production.rb b/config/environments/production.rb index 369bb5bf0..5a17d3281 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -68,8 +68,7 @@ # Don't log any deprecations. config.active_support.report_deprecations = false - # Cache in memory for now - config.cache_store = :redis_cache_store + config.cache_store = :solid_cache_store # Assets are cacheable config.public_file_server.headers = { 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/docs/self-hosting.md b/docs/self-hosting.md index 5398e0c5b..92de9bc87 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -196,7 +196,7 @@ docker run --rm \ This gives you a `campfire-backup.tar.gz` in your current directory containing the database snapshot and all uploaded files. 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: +To restore, extract the archive back into a (stopped) instance's volume, and run the restore hook, which replaces the live database with the snapshot: ```sh docker run --rm \ @@ -205,8 +205,7 @@ docker run --rm \ --volume "$PWD":/backup \ ghcr.io/basecamp/once-campfire:latest \ 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 && + /hooks/post-restore && chown -R rails:rails /rails/storage" ``` diff --git a/hooks/post-restore b/hooks/post-restore index 0b9138d1e..58976c7bf 100755 --- a/hooks/post-restore +++ b/hooks/post-restore @@ -4,9 +4,12 @@ set -euo pipefail db_name="${RAILS_ENV:-production}.sqlite3" snapshot="/rails/storage/backups/$db_name" db="/rails/storage/db/$db_name" +cache_db="/rails/storage/db/${RAILS_ENV:-production}_cache.sqlite3" if [ -f "$snapshot" ]; then mkdir -p "$(dirname "$db")" cp "$snapshot" "$db" rm -f "$db-wal" "$db-shm" fi + +rm -f "$cache_db" "$cache_db-wal" "$cache_db-shm"