PERF: remove superflous queries from initial page loads

stop doing expensive work to figure out discourse style sheet
This commit is contained in:
Sam 2014-11-11 15:32:44 +11:00
parent 23ad68678e
commit b2af49251d
3 changed files with 39 additions and 4 deletions

View File

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

View File

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

View File

@ -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
%[<link href="#{Rails.env.production? ? builder.stylesheet_relpath : builder.stylesheet_relpath_no_digest + '?body=1'}" media="all" rel="stylesheet" />].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 = %[<link href="#{Rails.env.production? ? builder.stylesheet_relpath : builder.stylesheet_relpath_no_digest + '?body=1'}" media="all" rel="stylesheet" />].html_safe
links[target] = tag
end
tag
end
end