PERF: Defer setting of distributed cache in more spots.

See follow up commit for rational.

Follow-up to 8cfe203383
This commit is contained in:
Alan Guo Xiang Tan 2021-06-03 14:02:40 +08:00
parent cd9941e0ca
commit a8667b5454
6 changed files with 16 additions and 18 deletions

View File

@ -155,10 +155,7 @@ class Theme < ActiveRecord::Base
end
def self.get_set_cache(key, &blk)
return @cache[key] if @cache[key]
value = blk.call
@cache.defer_set(key, value)
value
@cache.defer_get_set(key, &blk)
end
def self.theme_ids
@ -287,7 +284,7 @@ class Theme < ActiveRecord::Base
target = target.to_sym
val = resolve_baked_field(theme_ids, target, field)
(@cache[cache_key] = val || "").html_safe
get_set_cache(cache_key) { val || "" }.html_safe
end
def self.lookup_modifier(theme_ids, modifier_name)

View File

@ -24,8 +24,8 @@ class ApplicationSerializer < ActiveModel::Serializer
protected
def cache_fragment(name)
ApplicationSerializer.fragment_cache[name] ||= yield
def cache_fragment(name, &block)
ApplicationSerializer.fragment_cache.defer_get_set(name, &block)
end
def cache_anon_fragment(name, &blk)

View File

@ -19,4 +19,11 @@ class DistributedCache < MessageBus::DistributedCache
self[k] = v
end
end
def defer_get_set(k, &block)
return self[k] if self[k]
value = block.call
self.defer_set(k, value)
value
end
end

View File

@ -61,11 +61,8 @@ module SiteIconManager
private
def self.get_set_cache(key)
return @cache[key] if @cache[key]
value = yield
@cache.defer_set(key, value)
value
def self.get_set_cache(key, &block)
@cache.defer_get_set(key, &block)
end
def self.resolve_original(info)

View File

@ -440,7 +440,7 @@ class Stylesheet::Manager
def color_scheme_digest
cs = @color_scheme || theme&.color_scheme
categories_updated = self.class.cache["categories_updated"] ||= begin
categories_updated = self.class.cache.defer_get_set("categories_updated") do
Category
.where("uploaded_background_id IS NOT NULL")
.pluck(:updated_at)

View File

@ -490,11 +490,8 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
icon_name
end
def self.get_set_cache(key)
return cache[key] if cache[key]
value = yield
cache.defer_set(key, value)
value
def self.get_set_cache(key, &block)
cache.defer_get_set(key, &block)
end
def self.cache