PERF: optimise featuring of topics and users

Previously this would delete all features and recreate, causing
uneeded network traffic and db pain
This commit is contained in:
Sam
2014-01-30 10:32:20 +11:00
parent 29c3767b7f
commit 43ceaae7ba
3 changed files with 31 additions and 12 deletions
+12 -5
View File
@@ -5,18 +5,25 @@ class CategoryFeaturedTopic < ActiveRecord::Base
# Populates the category featured topics
def self.feature_topics
transaction do
Category.all.each do |c|
feature_topics_for(c)
CategoryFeaturedUser.feature_users_in(c)
current = {}
CategoryFeaturedTopic.select(:topic_id, :category_id).order(:rank).each do |f|
(current[f.category_id] ||= []) << f.topic_id
end
Category.select(:id, :topic_id).all.each do |c|
feature_topics_for(c, current[c.id] || [])
CategoryFeaturedUser.feature_users_in(c.id)
end
end
end
def self.feature_topics_for(c)
def self.feature_topics_for(c, existing=nil)
return if c.blank?
query = TopicQuery.new(self.fake_admin, per_page: SiteSetting.category_featured_topics, except_topic_ids: [c.topic_id], visible: true)
results = query.list_category(c).topic_ids.to_a
results = query.list_category(c).topic_ids
return if results == existing
CategoryFeaturedTopic.transaction do
CategoryFeaturedTopic.delete_all(category_id: c.id)