PERF: Cache categories query in `Stylesheet::Manager.color_scheme_digest`.
The query is being executed each time we try and generate the link path for a stylesheet within the duration of a reqeust. Categories are not updated that often so repeating this query multiple times a request is wasteful. At the time of this commit, there is a `publish_discourse_stylesheet` ActiveRecord callback on the `Category` model which clears the cache of `Stylesheet::Manager` each time a category is saved.
This commit is contained in:
parent
c809f722f7
commit
6a79864f14
|
@ -436,15 +436,21 @@ class Stylesheet::Manager
|
||||||
end
|
end
|
||||||
|
|
||||||
def color_scheme_digest
|
def color_scheme_digest
|
||||||
|
|
||||||
cs = @color_scheme || theme&.color_scheme
|
cs = @color_scheme || theme&.color_scheme
|
||||||
|
|
||||||
category_updated = Category.where("uploaded_background_id IS NOT NULL").pluck(:updated_at).map(&:to_i).sum
|
categories_updated = self.class.cache["categories_updated"] ||= begin
|
||||||
|
Category
|
||||||
|
.where("uploaded_background_id IS NOT NULL")
|
||||||
|
.pluck(:updated_at)
|
||||||
|
.map(&:to_i)
|
||||||
|
.sum
|
||||||
|
end
|
||||||
|
|
||||||
fonts = "#{SiteSetting.base_font}-#{SiteSetting.heading_font}"
|
fonts = "#{SiteSetting.base_font}-#{SiteSetting.heading_font}"
|
||||||
|
|
||||||
if cs || category_updated > 0
|
if cs || categories_updated > 0
|
||||||
theme_color_defs = theme&.resolve_baked_field(:common, :color_definitions)
|
theme_color_defs = theme&.resolve_baked_field(:common, :color_definitions)
|
||||||
Digest::SHA1.hexdigest "#{RailsMultisite::ConnectionManagement.current_db}-#{cs&.id}-#{cs&.version}-#{theme_color_defs}-#{Stylesheet::Manager.last_file_updated}-#{category_updated}-#{fonts}"
|
Digest::SHA1.hexdigest "#{RailsMultisite::ConnectionManagement.current_db}-#{cs&.id}-#{cs&.version}-#{theme_color_defs}-#{Stylesheet::Manager.last_file_updated}-#{categories_updated}-#{fonts}"
|
||||||
else
|
else
|
||||||
digest_string = "defaults-#{Stylesheet::Manager.last_file_updated}-#{fonts}"
|
digest_string = "defaults-#{Stylesheet::Manager.last_file_updated}-#{fonts}"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue