From b2af49251d90d3b2a29baecb16951eaf7579c1c1 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 11 Nov 2014 15:32:44 +1100 Subject: [PATCH] PERF: remove superflous queries from initial page loads stop doing expensive work to figure out discourse style sheet --- app/models/category.rb | 6 ++++++ app/models/color_scheme.rb | 5 +++++ lib/sass/discourse_stylesheets.rb | 32 +++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index a47ef4a68cd..dad287a2afb 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -38,6 +38,8 @@ class Category < ActiveRecord::Base after_destroy :publish_categories_list after_update :rename_category_definition, if: :name_changed? + after_save :publish_discourse_stylesheet + has_one :category_search_data belongs_to :parent_category, class_name: 'Category' has_many :subcategories, class_name: 'Category', foreign_key: 'parent_category_id' @@ -362,6 +364,10 @@ SQL topic.update_column(:title, I18n.t("category.topic_prefix", category: name)) end end + + def publish_discourse_stylesheet + MessageBus.publish("/discourse_stylesheet", self.name) + end end # == Schema Information diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb index 38e5a471296..f79db403c92 100644 --- a/app/models/color_scheme.rb +++ b/app/models/color_scheme.rb @@ -9,6 +9,7 @@ class ColorScheme < ActiveRecord::Base scope :current_version, ->{ where(versioned_id: nil) } after_destroy :destroy_versions + after_save :publish_discourse_stylesheet validates_associated :color_scheme_colors @@ -93,6 +94,10 @@ class ColorScheme < ActiveRecord::Base ColorScheme.where(versioned_id: self.id).destroy_all end + def publish_discourse_stylesheet + MessageBus.publish("/discourse_stylesheet", self.name) + end + end # == Schema Information diff --git a/lib/sass/discourse_stylesheets.rb b/lib/sass/discourse_stylesheets.rb index 29bf3baa9c9..0c1008e7669 100644 --- a/lib/sass/discourse_stylesheets.rb +++ b/lib/sass/discourse_stylesheets.rb @@ -7,13 +7,37 @@ class DiscourseStylesheets MANIFEST_FULL_PATH = "#{MANIFEST_DIR}/stylesheet-manifest" @lock = Mutex.new + @links = {} + + def self.ensure_subscribed! + unless @subscribed + @lock.synchronize do + MessageBus.subscribe("/discourse_stylesheet") do |message| + @lock.synchronize do + @links[message.site_id] = nil + end + end + @subscribed = true + end + end + end def self.stylesheet_link_tag(target = :desktop) - builder = self.new(target) + ensure_subscribed! @lock.synchronize do - builder.compile unless File.exists?(builder.stylesheet_fullpath) - builder.ensure_digestless_file - %[].html_safe + links = (@links[RailsMultisite::ConnectionManagement.current_db] ||= {}) + tag = links[target] + + if !tag + builder = self.new(target) + builder.compile unless File.exists?(builder.stylesheet_fullpath) + builder.ensure_digestless_file + tag = %[].html_safe + + links[target] = tag + end + + tag end end