From 4f885d7da2b6e1cdfb006a653e3dbf0029dd051f Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 1 May 2020 16:44:50 +0100 Subject: [PATCH] FIX: Clear theme caches after database transaction has committed This was causing unusual side effects on high-traffic sites, where the cache would be rebuilt before the transaction had been committed --- app/models/theme.rb | 16 +++++++++++----- app/models/theme_field.rb | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/models/theme.rb b/app/models/theme.rb index 69f6c2899ea..1b15a145517 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -68,7 +68,7 @@ class Theme < ActiveRecord::Base remove_from_cache! clear_cached_settings! - ColorScheme.hex_cache.clear + DB.after_commit { ColorScheme.hex_cache.clear } notify_theme_change(with_scheme: notify_with_scheme) end @@ -270,7 +270,9 @@ class Theme < ActiveRecord::Base end def self.clear_cache! - @cache.clear + DB.after_commit do + @cache.clear + end end def self.targets @@ -302,8 +304,10 @@ class Theme < ActiveRecord::Base end def notify_theme_change(with_scheme: false) - theme_ids = Theme.transform_ids([id]) - self.class.notify_theme_change(theme_ids, with_scheme: with_scheme) + DB.after_commit do + theme_ids = Theme.transform_ids([id]) + self.class.notify_theme_change(theme_ids, with_scheme: with_scheme) + end end def self.refresh_message_for_targets(targets, theme_ids) @@ -474,7 +478,9 @@ class Theme < ActiveRecord::Base end def clear_cached_settings! - Discourse.cache.delete("settings_for_theme_#{self.id}") + DB.after_commit do + Discourse.cache.delete("settings_for_theme_#{self.id}") + end end def included_settings diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb index 575490008ff..ee417e353d9 100644 --- a/app/models/theme_field.rb +++ b/app/models/theme_field.rb @@ -317,23 +317,23 @@ class ThemeField < ActiveRecord::Base self.value_baked, self.error = translation_field? ? process_translation : process_html(self.value) self.error = nil unless self.error.present? self.compiler_version = COMPILER_VERSION - CSP::Extension.clear_theme_extensions_cache! + DB.after_commit { CSP::Extension.clear_theme_extensions_cache! } elsif extra_js_field? self.value_baked, self.error = process_extra_js(self.value) self.error = nil unless self.error.present? self.compiler_version = COMPILER_VERSION elsif basic_scss_field? ensure_scss_compiles! - Stylesheet::Manager.clear_theme_cache! + DB.after_commit { Stylesheet::Manager.clear_theme_cache! } elsif settings_field? validate_yaml! theme.clear_cached_settings! - CSP::Extension.clear_theme_extensions_cache! - SvgSprite.expire_cache + DB.after_commit { CSP::Extension.clear_theme_extensions_cache! } + DB.after_commit { SvgSprite.expire_cache } self.value_baked = "baked" self.compiler_version = COMPILER_VERSION elsif svg_sprite_field? - SvgSprite.expire_cache + DB.after_commit { SvgSprite.expire_cache } self.error = nil self.value_baked = "baked" self.compiler_version = COMPILER_VERSION