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/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..b798c0f71 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.body.body) + text_message_presentation(message.body.body, fragment_cached: fragment_cached) end rescue Exception => e Sentry.capture_exception(e, extra: { message: message }) @@ -75,42 +75,58 @@ def message_presentation(message) "" end + # 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) + 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. - # 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) } + # 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) - 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?(" { 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 = body.to_html - if controller.perform_caching && !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..fbd6ed30f 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_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/_message.html.erb b/app/views/messages/_message.html.erb index a94a579c9..6dfef678f 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 %>

@@ -23,12 +22,11 @@ <%= 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 %> <% 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/_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/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..0554ee564 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -1 +1,4 @@ -<%= render partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> +<% 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 d2d485f1e..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 partial: "messages/message", collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <% 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 234aac0c5..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 partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <% 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 03aff66d3..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 partial: "messages/message", collection: @messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <% 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 %> 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/cache.yml b/config/cache.yml new file mode 100644 index 000000000..c1cfbd488 --- /dev/null +++ b/config/cache.yml @@ -0,0 +1,19 @@ +default: &default + store_options: + max_age: <%= 30.days.to_i %> + 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..dd428e8f0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -33,7 +33,25 @@ 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 database: storage/db/production.sqlite3 + + cache: + <<: *default + 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 diff --git a/config/environments/production.rb b/config/environments/production.rb index 369bb5bf0..729ad39c2 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 its own SQLite database. + config.cache_store = :solid_cache_store # Assets are cacheable config.public_file_server.headers = { 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/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/schema.rb b/db/schema.rb index ccb79c9f8..f77e62007 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -184,4 +184,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/self-hosting.md b/docs/self-hosting.md index 5398e0c5b..32d4b40c7 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -169,6 +169,10 @@ 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. 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. + ### Backups To back up your instance, back up the contents of the `/rails/storage` volume. @@ -182,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 \ @@ -190,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: @@ -207,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..daf9f1eef --- /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. Solid Cache's 256 MiB `max_size` is an estimated eviction target, not a hard limit on SQLite or WAL disk usage. + +## 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. 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 + +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/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..bd6bd0002 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,86 @@ 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 "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 + 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..4838a4451 100644 --- a/test/helpers/messages_helper_test.rb +++ b/test/helpers/messages_helper_test.rb @@ -1,16 +1,28 @@ require "test_helper" class MessagesHelperTest < ActionView::TestCase - test "plain text presentation 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) - FragmentCache.store.clear - message = Message.create! room: rooms(:pets), body: "A reusable safe body", creator: users(:jason) + previous_cache = Rails.cache + Rails.cache = ActiveSupport::Cache::MemoryStore.new + 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 assert_equal first, view.message_presentation(message) ensure - FragmentCache.store.clear + 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, fragment_cached: true), "Cached with the whole message" + ensure + 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 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