diff --git a/app/models/theme.rb b/app/models/theme.rb index 5d44c530c76..8d3cd01c9ba 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -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) diff --git a/app/serializers/application_serializer.rb b/app/serializers/application_serializer.rb index 95abe6277f3..700ac78600b 100644 --- a/app/serializers/application_serializer.rb +++ b/app/serializers/application_serializer.rb @@ -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) diff --git a/lib/distributed_cache.rb b/lib/distributed_cache.rb index 833a9aeca9a..2597c0a3e56 100644 --- a/lib/distributed_cache.rb +++ b/lib/distributed_cache.rb @@ -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 diff --git a/lib/site_icon_manager.rb b/lib/site_icon_manager.rb index cd7d95de3e8..10f859c6422 100644 --- a/lib/site_icon_manager.rb +++ b/lib/site_icon_manager.rb @@ -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) diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb index d630b3984e9..f40e21200b4 100644 --- a/lib/stylesheet/manager.rb +++ b/lib/stylesheet/manager.rb @@ -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) diff --git a/lib/svg_sprite/svg_sprite.rb b/lib/svg_sprite/svg_sprite.rb index a522207e7d8..79bef31f366 100644 --- a/lib/svg_sprite/svg_sprite.rb +++ b/lib/svg_sprite/svg_sprite.rb @@ -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