From 7a416257dfcd30bf25b6c683709fc3f2d2fba8f9 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 11 Mar 2022 17:16:56 +0200 Subject: [PATCH] FIX: Invalidate site settings cache in all instances (#16155) Previous cache implementation did not support multisite instances and the cache was invalidated only in the instance where the change took place. --- app/models/site_setting.rb | 40 ++++++++++++++----- .../initializers/014-track-setting-changes.rb | 4 -- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 5d496245318..a6641f967a9 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -101,23 +101,33 @@ class SiteSetting < ActiveRecord::Base :markdown_typographer_quotation_marks ] - def self.reset_cached_settings! - @blocked_attachment_content_types_regex = nil - @blocked_attachment_filenames_regex = nil - @allowed_unicode_username_regex = nil - end - def self.blocked_attachment_content_types_regex - @blocked_attachment_content_types_regex ||= Regexp.union(SiteSetting.blocked_attachment_content_types.split("|")) + current_db = RailsMultisite::ConnectionManagement.current_db + + @blocked_attachment_content_types_regex ||= {} + @blocked_attachment_content_types_regex[current_db] ||= begin + Regexp.union(SiteSetting.blocked_attachment_content_types.split("|")) + end end def self.blocked_attachment_filenames_regex - @blocked_attachment_filenames_regex ||= Regexp.union(SiteSetting.blocked_attachment_filenames.split("|")) + current_db = RailsMultisite::ConnectionManagement.current_db + + @blocked_attachment_filenames_regex ||= {} + @blocked_attachment_filenames_regex[current_db] ||= begin + Regexp.union(SiteSetting.blocked_attachment_filenames.split("|")) + end end def self.allowed_unicode_username_characters_regex - @allowed_unicode_username_regex ||= SiteSetting.allowed_unicode_username_characters.present? \ - ? Regexp.new(SiteSetting.allowed_unicode_username_characters) : nil + current_db = RailsMultisite::ConnectionManagement.current_db + + @allowed_unicode_username_regex ||= {} + @allowed_unicode_username_regex[current_db] ||= begin + if SiteSetting.allowed_unicode_username_characters.present? + Regexp.new(SiteSetting.allowed_unicode_username_characters) + end + end end # helpers for getting s3 settings that fallback to global @@ -246,6 +256,16 @@ class SiteSetting < ActiveRecord::Base send("#{new_method}=", args) end end + + protected + + def self.clear_cache! + super + + @blocked_attachment_content_types_regex = nil + @blocked_attachment_filenames_regex = nil + @allowed_unicode_username_regex = nil + end end # == Schema Information diff --git a/config/initializers/014-track-setting-changes.rb b/config/initializers/014-track-setting-changes.rb index ba916aba3bb..ae4705bb21c 100644 --- a/config/initializers/014-track-setting-changes.rb +++ b/config/initializers/014-track-setting-changes.rb @@ -45,10 +45,6 @@ DiscourseEvent.on(:site_setting_changed) do |name, old_value, new_value| SiteIconManager.ensure_optimized! end - if SiteSetting::WATCHED_SETTINGS.include?(name) - SiteSetting.reset_cached_settings! - end - # Make sure medium and high priority thresholds were calculated. if name == :reviewable_low_priority_threshold && Reviewable.min_score_for_priority(:medium) > 0 Reviewable.set_priorities(low: new_value)