mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 16:41:08 +08:00
PERF: use distributed cache for site text and category slugs
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
require_dependency 'sass/discourse_sass_compiler'
|
||||
require_dependency 'distributed_cache'
|
||||
|
||||
class DiscourseStylesheets
|
||||
|
||||
@ -7,35 +8,22 @@ 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
|
||||
def self.cache
|
||||
@cache ||= DistributedCache.new("discourse_stylesheet")
|
||||
end
|
||||
|
||||
def self.stylesheet_link_tag(target = :desktop)
|
||||
ensure_subscribed!
|
||||
tag = cache[target]
|
||||
return tag if tag
|
||||
|
||||
@lock.synchronize do
|
||||
links = (@links[RailsMultisite::ConnectionManagement.current_db] ||= {})
|
||||
tag = links[target]
|
||||
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
|
||||
|
||||
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
|
||||
cache[target] = tag
|
||||
|
||||
tag
|
||||
end
|
||||
|
Reference in New Issue
Block a user