Permalink
Browse files

PERF: introduce fragment caches in site serializer

  • Loading branch information...
SamSaffron committed Sep 28, 2015
1 parent edfd870 commit 181ab8948558aebd9b38c1db474a88ba74b3d3c0
Showing with 55 additions and 19 deletions.
  1. +5 −1 app/models/category.rb
  2. +7 −0 app/models/group.rb
  3. +10 −0 app/models/post_action_type.rb
  4. +5 −14 app/models/site.rb
  5. +5 −1 app/models/topic.rb
  6. +23 −3 app/serializers/site_serializer.rb
@@ -193,7 +193,11 @@ def create_category_definition
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
@@ -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

@@ -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
@@ -13,14 +13,6 @@ def site_setting
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 @@ def trust_levels
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 @@ def categories
@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 @@ def categories
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 = {}

@@ -736,12 +736,16 @@ def url(post_number = nil)
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
@@ -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

0 comments on commit 181ab89

Please sign in to comment.