mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Cache categories in Site model take 2.
Follow-up to aa4f0aee67.
Fixed the security problem in the previous attempt.
This commit is contained in:
@@ -91,6 +91,7 @@ class Category < ActiveRecord::Base
|
||||
after_commit :trigger_category_created_event, on: :create
|
||||
after_commit :trigger_category_updated_event, on: :update
|
||||
after_commit :trigger_category_destroyed_event, on: :destroy
|
||||
after_commit :clear_site_cache
|
||||
|
||||
after_save_commit :index_search
|
||||
|
||||
@@ -957,6 +958,10 @@ class Category < ActiveRecord::Base
|
||||
|
||||
result.map { |row| [row.group_id, row.permission_type] }
|
||||
end
|
||||
|
||||
def clear_site_cache
|
||||
Site.clear_cache
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
class CategoryTag < ActiveRecord::Base
|
||||
belongs_to :category
|
||||
belongs_to :tag
|
||||
|
||||
after_commit do
|
||||
Site.clear_cache
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
class CategoryTagGroup < ActiveRecord::Base
|
||||
belongs_to :category
|
||||
belongs_to :tag_group
|
||||
|
||||
after_commit do
|
||||
Site.clear_cache
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
||||
@@ -28,16 +28,42 @@ class Site
|
||||
UserField.order(:position).all
|
||||
end
|
||||
|
||||
def categories
|
||||
@categories ||= begin
|
||||
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
|
||||
categories = Category
|
||||
.includes(:uploaded_logo, :uploaded_background, :tags, :tag_groups)
|
||||
.secured(@guardian)
|
||||
.includes(:uploaded_logo, :uploaded_background, :tags, :tag_groups, :required_tag_group)
|
||||
.joins('LEFT JOIN topics t on t.id = categories.topic_id')
|
||||
.select('categories.*, t.slug topic_slug')
|
||||
.order(:position)
|
||||
.to_a
|
||||
|
||||
categories = categories.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
|
||||
|
||||
with_children = Set.new
|
||||
categories.each do |c|
|
||||
|
||||
Reference in New Issue
Block a user