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
This commit is contained in:
David Taylor 2020-05-01 16:44:50 +01:00
parent 5901717531
commit 4f885d7da2
No known key found for this signature in database
GPG Key ID: 46904C18B1D3F434
2 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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