PERF: Defer setting of distributed cache in performance critical paths.

Setting a key/value pair in DistributedCache involves waiting on the
write to Redis to finish. In most cases, we don't need to wait on the
setting of the cache to finish. We just need to take our return value
and move on.
This commit is contained in:
Alan Guo Xiang Tan 2021-06-02 15:46:48 +08:00
parent 83211cff25
commit 8cfe203383
5 changed files with 25 additions and 9 deletions

View File

@ -155,10 +155,10 @@ class Theme < ActiveRecord::Base
end
def self.get_set_cache(key, &blk)
if @cache.hash.key? key.to_s
return @cache[key]
end
@cache[key] = blk.call
return @cache[key] if @cache[key]
value = blk.call
@cache.defer_set(key, value)
value
end
def self.theme_ids

View File

@ -11,4 +11,12 @@ class DistributedCache < MessageBus::DistributedCache
app_version: Discourse.git_version
)
end
# Defer setting of the key in the cache for performance critical path to avoid
# waiting on MessageBus to publish the message which involves writing to Redis.
def defer_set(k, v)
Scheduler::Defer.later("#{@key}_set") do
self[k] = v
end
end
end

View File

@ -62,7 +62,10 @@ module SiteIconManager
private
def self.get_set_cache(key)
@cache[key] ||= yield
return @cache[key] if @cache[key]
value = yield
@cache.defer_set(key, value)
value
end
def self.resolve_original(info)

View File

@ -88,14 +88,16 @@ class Stylesheet::Manager
builder.compile unless File.exists?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
end
cache[cache_key] = href
cache.defer_set(cache_key, href)
end
data[:theme_id] = theme_id if theme_id.present? && data[:theme_id].blank?
data[:new_href] = href
stylesheets << data
end
cache[array_cache_key] = stylesheets.freeze
cache.defer_set(array_cache_key, stylesheets.freeze)
stylesheets
end
end
@ -128,7 +130,7 @@ class Stylesheet::Manager
href = builder.stylesheet_path(current_hostname)
stylesheet[:new_href] = href
cache[cache_key] = stylesheet.freeze
cache.defer_set(cache_key, stylesheet.freeze)
stylesheet
end

View File

@ -478,7 +478,10 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
end
def self.get_set_cache(key)
cache[key] ||= yield
return cache[key] if cache[key]
value = yield
cache.defer_set(key, value)
value
end
def self.cache