mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 05:25:52 +08:00
PERF: introduce fragment caches in site serializer
This commit is contained in:
parent
edfd870249
commit
181ab89485
@ -193,7 +193,11 @@ SQL
|
|||||||
end
|
end
|
||||||
|
|
||||||
def topic_url
|
def topic_url
|
||||||
topic_only_relative_url.try(:relative_url)
|
if has_attribute?("topic_slug")
|
||||||
|
Topic.relative_url(topic_id, topic_slug)
|
||||||
|
else
|
||||||
|
topic_only_relative_url.try(:relative_url)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def description_text
|
def description_text
|
||||||
|
@ -15,6 +15,13 @@ class Group < ActiveRecord::Base
|
|||||||
after_save :update_primary_group
|
after_save :update_primary_group
|
||||||
after_save :update_title
|
after_save :update_title
|
||||||
|
|
||||||
|
after_save :expire_cache
|
||||||
|
after_destroy :expire_cache
|
||||||
|
|
||||||
|
def expire_cache
|
||||||
|
ApplicationSerializer.expire_cache_fragment!("group_names")
|
||||||
|
end
|
||||||
|
|
||||||
validate :name_format_validator
|
validate :name_format_validator
|
||||||
validates_uniqueness_of :name, case_sensitive: false
|
validates_uniqueness_of :name, case_sensitive: false
|
||||||
|
|
||||||
|
@ -1,7 +1,17 @@
|
|||||||
require_dependency 'enum'
|
require_dependency 'enum'
|
||||||
|
require_dependency 'distributed_cache'
|
||||||
|
|
||||||
class PostActionType < ActiveRecord::Base
|
class PostActionType < ActiveRecord::Base
|
||||||
|
after_save :expire_cache
|
||||||
|
after_destroy :expire_cache
|
||||||
|
|
||||||
|
def expire_cache
|
||||||
|
ApplicationSerializer.expire_cache_fragment!("post_action_types")
|
||||||
|
ApplicationSerializer.expire_cache_fragment!("post_action_flag_types")
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def ordered
|
def ordered
|
||||||
order('position asc')
|
order('position asc')
|
||||||
end
|
end
|
||||||
|
@ -13,14 +13,6 @@ class Site
|
|||||||
SiteSetting
|
SiteSetting
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_action_types
|
|
||||||
PostActionType.ordered
|
|
||||||
end
|
|
||||||
|
|
||||||
def topic_flag_types
|
|
||||||
post_action_types.where(name_key: ['inappropriate', 'spam', 'notify_moderators'])
|
|
||||||
end
|
|
||||||
|
|
||||||
def notification_types
|
def notification_types
|
||||||
Notification.types
|
Notification.types
|
||||||
end
|
end
|
||||||
@ -29,10 +21,6 @@ class Site
|
|||||||
TrustLevel.all
|
TrustLevel.all
|
||||||
end
|
end
|
||||||
|
|
||||||
def groups
|
|
||||||
@groups ||= Group.order(:name).map { |g| { id: g.id, name: g.name } }
|
|
||||||
end
|
|
||||||
|
|
||||||
def user_fields
|
def user_fields
|
||||||
UserField.all
|
UserField.all
|
||||||
end
|
end
|
||||||
@ -41,7 +29,8 @@ class Site
|
|||||||
@categories ||= begin
|
@categories ||= begin
|
||||||
categories = Category
|
categories = Category
|
||||||
.secured(@guardian)
|
.secured(@guardian)
|
||||||
.includes(:topic_only_relative_url)
|
.joins('JOIN topics t on t.id = categories.topic_id')
|
||||||
|
.select('categories.*, t.slug topic_slug')
|
||||||
.order(:position)
|
.order(:position)
|
||||||
|
|
||||||
categories = categories.to_a
|
categories = categories.to_a
|
||||||
@ -53,7 +42,9 @@ class Site
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id))
|
allowed_topic_create_ids =
|
||||||
|
@guardian.anonymous? ? [] : Category.topic_create_allowed(@guardian).pluck(:id)
|
||||||
|
allowed_topic_create = Set.new(allowed_topic_create_ids)
|
||||||
|
|
||||||
by_id = {}
|
by_id = {}
|
||||||
|
|
||||||
|
@ -736,12 +736,16 @@ class Topic < ActiveRecord::Base
|
|||||||
self.class.url id, slug, post_number
|
self.class.url id, slug, post_number
|
||||||
end
|
end
|
||||||
|
|
||||||
def relative_url(post_number=nil)
|
def self.relative_url(id, slug, post_number=nil)
|
||||||
url = "#{Discourse.base_uri}/t/#{slug}/#{id}"
|
url = "#{Discourse.base_uri}/t/#{slug}/#{id}"
|
||||||
url << "/#{post_number}" if post_number.to_i > 1
|
url << "/#{post_number}" if post_number.to_i > 1
|
||||||
url
|
url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relative_url(post_number=nil)
|
||||||
|
Topic.relative_url(id, slug, post_number)
|
||||||
|
end
|
||||||
|
|
||||||
def unsubscribe_url
|
def unsubscribe_url
|
||||||
"#{url}/unsubscribe"
|
"#{url}/unsubscribe"
|
||||||
end
|
end
|
||||||
|
@ -12,15 +12,35 @@ class SiteSerializer < ApplicationSerializer
|
|||||||
:is_readonly,
|
:is_readonly,
|
||||||
:disabled_plugins,
|
:disabled_plugins,
|
||||||
:user_field_max_length,
|
:user_field_max_length,
|
||||||
:suppressed_from_homepage_category_ids
|
:suppressed_from_homepage_category_ids,
|
||||||
|
:post_action_types,
|
||||||
|
:topic_flag_types
|
||||||
|
|
||||||
has_many :categories, serializer: BasicCategorySerializer, embed: :objects
|
has_many :categories, serializer: BasicCategorySerializer, embed: :objects
|
||||||
has_many :post_action_types, embed: :objects
|
|
||||||
has_many :topic_flag_types, serializer: TopicFlagTypeSerializer, embed: :objects
|
|
||||||
has_many :trust_levels, embed: :objects
|
has_many :trust_levels, embed: :objects
|
||||||
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
|
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
|
||||||
has_many :user_fields, embed: :objects, serialzer: UserFieldSerializer
|
has_many :user_fields, embed: :objects, serialzer: UserFieldSerializer
|
||||||
|
|
||||||
|
def groups
|
||||||
|
cache_fragment("group_names") do
|
||||||
|
Group.order(:name).pluck(:id,:name).map { |id,name| { id: id, name: name } }.as_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def post_action_types
|
||||||
|
cache_fragment("post_action_types") do
|
||||||
|
ActiveModel::ArraySerializer.new(PostActionType.ordered).as_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def topic_flag_types
|
||||||
|
cache_fragment("post_action_flag_types") do
|
||||||
|
flags = PostActionType.ordered.where(name_key: ['inappropriate', 'spam', 'notify_moderators'])
|
||||||
|
ActiveModel::ArraySerializer.new(flags, each_serializer: TopicFlagTypeSerializer).as_json
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
def default_archetype
|
def default_archetype
|
||||||
Archetype.default
|
Archetype.default
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user