From 51afa579f727fb4763cdb54b38cd62aa2cd04b57 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 18 Feb 2022 22:30:20 +0000 Subject: [PATCH] DEV: Use the theme cache helper for settings The previous Discourse.cache usage was different to how other theme-related caching is handled, and also requires reaching out to redis every time. The common theme cache is held in memory (as a DistributedCache) --- app/models/theme.rb | 13 ++----------- app/models/theme_field.rb | 1 - app/models/theme_translation_override.rb | 1 - spec/models/theme_spec.rb | 2 +- 4 files changed, 3 insertions(+), 14 deletions(-) diff --git a/app/models/theme.rb b/app/models/theme.rb index a0b30073e80..b0887074016 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -91,7 +91,6 @@ class Theme < ActiveRecord::Base update_javascript_cache! remove_from_cache! - clear_cached_settings! DB.after_commit { ColorScheme.hex_cache.clear } notify_theme_change(with_scheme: notify_with_scheme) @@ -135,7 +134,6 @@ class Theme < ActiveRecord::Base after_destroy do remove_from_cache! - clear_cached_settings! if SiteSetting.default_theme_id == self.id Theme.clear_default! end @@ -529,13 +527,13 @@ class Theme < ActiveRecord::Base end def cached_settings - Discourse.cache.fetch("settings_for_theme_#{self.id}", expires_in: 30.minutes) do + Theme.get_set_cache "settings_for_theme_#{self.id}" do build_settings_hash end end def cached_default_settings - Discourse.cache.fetch("default_settings_for_theme_#{self.id}", expires_in: 30.minutes) do + Theme.get_set_cache "default_settings_for_theme_#{self.id}" do settings_hash = {} self.settings.each do |setting| settings_hash[setting.name] = setting.default @@ -561,13 +559,6 @@ class Theme < ActiveRecord::Base hash end - def clear_cached_settings! - DB.after_commit do - Discourse.cache.delete("settings_for_theme_#{self.id}") - Discourse.cache.delete("default_settings_for_theme_#{self.id}") - end - end - def included_settings hash = {} diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb index 162d80b1e48..22c03f61728 100644 --- a/app/models/theme_field.rb +++ b/app/models/theme_field.rb @@ -378,7 +378,6 @@ class ThemeField < ActiveRecord::Base DB.after_commit { Stylesheet::Manager.clear_theme_cache! } elsif settings_field? validate_yaml! - theme.clear_cached_settings! DB.after_commit { CSP::Extension.clear_theme_extensions_cache! } DB.after_commit { SvgSprite.expire_cache } self.value_baked = "baked" diff --git a/app/models/theme_translation_override.rb b/app/models/theme_translation_override.rb index ec639c2731f..a0d45cadd65 100644 --- a/app/models/theme_translation_override.rb +++ b/app/models/theme_translation_override.rb @@ -5,7 +5,6 @@ class ThemeTranslationOverride < ActiveRecord::Base after_commit do theme.theme_fields.where(target_id: Theme.targets[:translations]).update_all(value_baked: nil) - theme.clear_cached_settings! theme.remove_from_cache! end end diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb index d5bddee7820..6fbe8d14b6a 100644 --- a/spec/models/theme_spec.rb +++ b/spec/models/theme_spec.rb @@ -512,7 +512,7 @@ HTML theme.save! Upload.find(upload.id).destroy - theme.clear_cached_settings! + theme.remove_from_cache! json = JSON.parse(cached_settings(theme.id)) expect(json).to be_empty