PERF: Preload settings, groups and badge icons in SvgSprite.

Identified as a hot path in production. Preload it early instead of
executing the queries in a live request.
This commit is contained in:
Alan Guo Xiang Tan 2021-06-03 16:14:56 +08:00
parent eb2c399445
commit 58b30fb510
2 changed files with 22 additions and 9 deletions

View File

@ -930,6 +930,7 @@ module Discourse
JsLocaleHelper.load_translations(SiteSetting.default_locale)
Site.json_for(Guardian.new)
SvgSprite.preload
end
[

View File

@ -221,6 +221,12 @@ module SvgSprite
THEME_SPRITE_VAR_NAME = "icons-sprite"
def self.preload
settings_icons
group_icons
badge_icons
end
def self.custom_svg_sprites(theme_ids = [])
get_set_cache("custom_svg_sprites_#{Theme.transform_ids(theme_ids).join(',')}") do
custom_sprite_paths = Dir.glob("#{Rails.root}/plugins/*/svg-icons/*.svg")
@ -415,6 +421,7 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
end
def self.settings_icons
get_set_cache("settings_icons") do
# includes svg_icon_subset and any settings containing _icon (incl. plugin settings)
site_setting_icons = []
@ -426,18 +433,23 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
site_setting_icons
end
end
def self.plugin_icons
DiscoursePluginRegistry.svg_icons
end
def self.badge_icons
get_set_cache("badge_icons") do
Badge.pluck(:icon).uniq
end
end
def self.group_icons
get_set_cache("group_icons") do
Group.pluck(:flair_icon).uniq
end
end
def self.theme_icons(theme_ids)
return [] if theme_ids.blank?