DEV: Make every DistributedCache lazily instantiated (#23147)

This commit is contained in:
Daniel Waterworth 2023-08-18 10:59:11 -05:00 committed by GitHub
parent 87ebbec9b2
commit 91f560a521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 9 deletions

View File

@ -205,14 +205,16 @@ class Category < ActiveRecord::Base
# Allows us to skip creating the category definition topic in tests. # Allows us to skip creating the category definition topic in tests.
attr_accessor :skip_category_definition attr_accessor :skip_category_definition
@topic_id_cache = DistributedCache.new("category_topic_ids") def self.topic_id_cache
@topic_id_cache ||= DistributedCache.new("category_topic_ids")
end
def self.topic_ids def self.topic_ids
@topic_id_cache.defer_get_set("ids") { Set.new(Category.pluck(:topic_id).compact) } topic_id_cache.defer_get_set("ids") { Set.new(Category.pluck(:topic_id).compact) }
end end
def self.reset_topic_ids_cache def self.reset_topic_ids_cache
@topic_id_cache.clear topic_id_cache.clear
end end
def reset_topic_ids_cache def reset_topic_ids_cache

View File

@ -6,14 +6,16 @@ class Developer < ActiveRecord::Base
after_save :rebuild_cache after_save :rebuild_cache
after_destroy :rebuild_cache after_destroy :rebuild_cache
@id_cache = DistributedCache.new("developer_ids") def self.id_cache
@id_cache ||= DistributedCache.new("developer_ids")
end
def self.user_ids def self.user_ids
@id_cache.defer_get_set("ids") { Set.new(Developer.pluck(:user_id)) } id_cache.defer_get_set("ids") { Set.new(Developer.pluck(:user_id)) }
end end
def self.rebuild_cache def self.rebuild_cache
@id_cache.clear id_cache.clear
end end
def rebuild_cache def rebuild_cache

View File

@ -10,7 +10,9 @@ class Theme < ActiveRecord::Base
attr_accessor :child_components attr_accessor :child_components
@cache = DistributedCache.new("theme:compiler:#{BASE_COMPILER_VERSION}") def self.cache
@cache ||= DistributedCache.new("theme:compiler:#{BASE_COMPILER_VERSION}")
end
belongs_to :user belongs_to :user
belongs_to :color_scheme belongs_to :color_scheme
@ -202,7 +204,7 @@ class Theme < ActiveRecord::Base
end end
def self.get_set_cache(key, &blk) def self.get_set_cache(key, &blk)
@cache.defer_get_set(key, &blk) cache.defer_get_set(key, &blk)
end end
def self.theme_ids def self.theme_ids
@ -369,7 +371,7 @@ class Theme < ActiveRecord::Base
end end
def self.clear_cache! def self.clear_cache!
@cache.clear cache.clear
end end
def self.targets def self.targets