Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ gem "benchmark"
gem "sqlite3"
gem "redis", "~> 5.4"

# Caching
gem "solid_cache", "~> 1.0.10"

# Deployment
gem "puma", "~> 8.0"

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -463,6 +467,7 @@ DEPENDENCIES
selenium-webdriver
sentry-rails
sentry-ruby
solid_cache (~> 1.0.10)
sqlite3
stimulus-rails
surfguard!
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 3 additions & 29 deletions app/controllers/concerns/cached_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/concerns/fragment_caching.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/controllers/messages/by_bots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Messages::ByBotsController < MessagesController

def index
@messages = find_paged_messages
@messages.preload_associations(@messages)
set_pagination_headers
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/sidebars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 42 additions & 26 deletions app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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?("<action-text-attachment") ||
message.boosts.any? { |boost| !boost.association(:booster).loaded? }

# Collection caching preloads these rows before deriving keys. Read their
# actual content: foreign SQL writers need not touch Rails timestamps.
dependencies = [ message.attributes, room.attributes.slice("id", "type", "name"), message.creator.attributes,
body.to_html, message.boosts.sort_by(&:created_at).map { |boost| [ boost.attributes, boost.booster.attributes ] } ]
FragmentCache::ContentKey.new(Digest::SHA256.hexdigest(ActiveSupport::JSON.encode(dependencies)))
# 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.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 text_message_presentation(body)
def message_fragment_cacheable?(message)
!message.room.direct? && !message.attachment? && !message.body.body&.to_html.to_s.include?("<action-text-attachment")
end

def text_message_presentation(body, fragment_cached: false)
render = -> { 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?("<action-text-attachment") && html.bytesize <= ResponseCache::MAX_ENTRY_BYTES
FragmentCache.store.fetch([ "text-presentation-v2", I18n.locale, Digest::SHA256.hexdigest(html) ]) { render.call }
if !html.include?("<action-text-attachment") && html.bytesize <= ResponseCache::MAX_ENTRY_BYTES
Rails.cache.fetch([ "text-presentation-v3", Rails.configuration.x.presentation_cache_version, I18n.locale, Digest::SHA256.hexdigest(html) ]) { render.call }
else
render.call
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/rooms_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def composer_form_tag(room, &)

def room_display_name(room, for_user: Current.user)
if room.direct?
room.users.without(for_user).pluck(:name).to_sentence.presence || for_user&.name
room.users.reject { |user| user.id == for_user&.id }.map(&:name).to_sentence.presence || for_user&.name
else
room.name
end
Expand Down
20 changes: 0 additions & 20 deletions app/models/fragment_cache.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/models/message/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Message::Pagination

PAGE_SIZE = 40

# Expose Rails' collection-preloading hook while preserving Array pagination and
# validators. The renderer passes only cache misses to preload_associations.
# Expose Rails' collection-preloading hook while preserving Array pagination.
# Content-keyed renders preload all presentation inputs before validating hits.
class Page < Array
def self.load(relation, direction, size)
new(relation.skip_preloading!.public_send(direction, size), relation)
Expand All @@ -25,7 +25,7 @@ def preload_associations(records)
end

included do
# Keep presentation data lazy until the collection cache knows which messages missed.
# Defer presentation loading until rendering or content-key derivation.
scope :last_page, -> { last_page_of(PAGE_SIZE) }
scope :first_page, -> { Page.load(ordered, :first, PAGE_SIZE) }

Expand Down
15 changes: 7 additions & 8 deletions app/models/record_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading