PERF: Defer setting of distributed cache in more spots.

PERF: Defer setting of distributed cache in more spots.

See follow up commit for rational.

Follow-up to 8cfe203383367cc7a3cf31b3c3960b7b7aac92ef

diff --git a/app/models/theme.rb b/app/models/theme.rb
index 5d44c53..8d3cd01 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 95abe62..700ac78 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 833a9ae..2597c0a 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 cd7d95d..10f859c 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 d630b39..f40e212 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 a522207..79bef31 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

GitHub sha: a8667b54

This commit appears in #13263 which was approved by eviltrout. It was merged by tgxworld.