Revert "PERF: Cache categories in Site model."

This reverts commit 7dc0f88acd.
This commit is contained in:
Alan Guo Xiang Tan
2021-06-17 15:17:49 +08:00
parent 4c3d2267b4
commit aa4f0aee67
7 changed files with 11 additions and 80 deletions

View File

@@ -28,42 +28,16 @@ class Site
UserField.order(:position).all
end
CATEGORIES_CACHE_KEY = "site_categories"
def self.clear_cache
Discourse.cache.delete(CATEGORIES_CACHE_KEY)
end
def self.all_categories_cache
# Categories do not change often so there is no need for us to run the
# same query and spend time creating ActiveRecord objects for every requests.
#
# Do note that any new association added to the eager loading needs a
# corresponding ActiveRecord callback to clear the categories cache.
Discourse.cache.fetch(CATEGORIES_CACHE_KEY, expires_in: 30.minutes) do
def categories
@categories ||= begin
categories = Category
.includes(:uploaded_logo, :uploaded_background, :tags, :tag_groups, :required_tag_group)
.includes(:uploaded_logo, :uploaded_background, :tags, :tag_groups)
.secured(@guardian)
.joins('LEFT JOIN topics t on t.id = categories.topic_id')
.select('categories.*, t.slug topic_slug')
.order(:position)
.to_a
ActiveModel::ArraySerializer.new(
categories,
each_serializer: SiteCategorySerializer
).as_json
end
end
def categories
@categories ||= begin
categories = []
self.class.all_categories_cache.each do |category|
if @guardian.can_see_serialized_category?(category_id: category["id"], read_restricted: category["read_restricted"])
categories << OpenStruct.new(category)
end
end
categories = categories.to_a
with_children = Set.new
categories.each do |c|