PERF: use distributed cache for site text and category slugs

This commit is contained in:
Sam
2014-11-12 09:44:32 +11:00
parent c55fa9d5c8
commit a97f2eee05
5 changed files with 43 additions and 61 deletions
+11 -23
View File
@@ -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
+1 -1
View File
@@ -289,7 +289,7 @@ module SiteSettingExtension
protected
def clear_cache!
MessageBus.publish '/text_for', 'site_settings'
SiteText.text_for_cache.clear
Rails.cache.delete(SiteSettingExtension.client_settings_cache_key)
end
+6 -29
View File
@@ -15,39 +15,16 @@ module SiteTextClassMethods
@types << SiteTextType.new(text_type, format, opts)
end
def text_for_cache
@text_for_cache ||= DistributedCache.new("text_for_cache")
end
def text_for(text_type, replacements=nil)
text = nil
text = cached_text_for(text_type) if replacements.blank?
text = text_for_cache[text_type] if replacements.blank?
text ||= uncached_text_for(text_type, replacements)
end
def cached_text_for(text_type)
ensure_subscribed!
@mutex.synchronize do
cache = @text_for_cache[RailsMultisite::ConnectionManagement.current_db]
cache[text_type] if cache
end
end
def store_cached_text_for(text_type, result)
ensure_subscribed!
@mutex.synchronize do
cache = (@text_for_cache[RailsMultisite::ConnectionManagement.current_db] ||= {})
cache[text_type] = result
end
end
def ensure_subscribed!
return if @subscribed
@mutex.synchronize do
MessageBus.subscribe("/text_for") do |message|
@mutex.synchronize do
@text_for_cache[message.site_id] = nil
end
end
end
end
def uncached_text_for(text_type, replacements=nil)
store_cache = replacements.blank?
@@ -71,7 +48,7 @@ module SiteTextClassMethods
if store_cache
result.freeze
store_cached_text_for(text_type, result)
text_for_cache[text_type] = result
end
result