PERF: introduce fragment caches in site serializer

This commit is contained in:
Sam 2015-09-28 16:43:38 +10:00
parent edfd870249
commit 181ab89485
6 changed files with 55 additions and 19 deletions

View File

@ -193,7 +193,11 @@ SQL
end
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
def description_text

View File

@ -15,6 +15,13 @@ class Group < ActiveRecord::Base
after_save :update_primary_group
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
validates_uniqueness_of :name, case_sensitive: false

View File

@ -1,7 +1,17 @@
require_dependency 'enum'
require_dependency 'distributed_cache'
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
def ordered
order('position asc')
end

View File

@ -13,14 +13,6 @@ class Site
SiteSetting
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
Notification.types
end
@ -29,10 +21,6 @@ class Site
TrustLevel.all
end
def groups
@groups ||= Group.order(:name).map { |g| { id: g.id, name: g.name } }
end
def user_fields
UserField.all
end
@ -41,7 +29,8 @@ class Site
@categories ||= begin
categories = Category
.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)
categories = categories.to_a
@ -53,7 +42,9 @@ class Site
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 = {}

View File

@ -736,12 +736,16 @@ class Topic < ActiveRecord::Base
self.class.url id, slug, post_number
end
def relative_url(post_number=nil)
def self.relative_url(id, slug, post_number=nil)
url = "#{Discourse.base_uri}/t/#{slug}/#{id}"
url << "/#{post_number}" if post_number.to_i > 1
url
end
def relative_url(post_number=nil)
Topic.relative_url(id, slug, post_number)
end
def unsubscribe_url
"#{url}/unsubscribe"
end

View File

@ -12,15 +12,35 @@ class SiteSerializer < ApplicationSerializer
:is_readonly,
:disabled_plugins,
: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 :post_action_types, embed: :objects
has_many :topic_flag_types, serializer: TopicFlagTypeSerializer, embed: :objects
has_many :trust_levels, embed: :objects
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
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
Archetype.default
end